diff --git a/packages/jellyfish-address/__tests__/eth.test.ts b/packages/jellyfish-address/__tests__/eth.test.ts index f755e5c8b4..6ce7a69d2a 100644 --- a/packages/jellyfish-address/__tests__/eth.test.ts +++ b/packages/jellyfish-address/__tests__/eth.test.ts @@ -1,4 +1,4 @@ -import { OP_CODES } from '@defichain/jellyfish-transaction' +import { OP_CODES, Script } from '@defichain/jellyfish-transaction' import { Eth } from '../src' const keypair = { @@ -12,7 +12,7 @@ it('should convert evm address to script', () => { expect(evmScript).toStrictEqual({ stack: [ OP_CODES.OP_16, - OP_CODES.OP_PUSHDATA_HEX_LE(keypair.evmAddr.substring(2)) + OP_CODES.OP_PUSHDATA_HEX_BE(keypair.evmAddr.substring(2)) ] }) }) @@ -21,3 +21,25 @@ it('should return undefined script for invalid eth address', () => { const evmScript = Eth.fromAddress('0xabc123') expect(evmScript).toStrictEqual(undefined) }) + +it('should convert evm script to address', () => { + const script: Script = { + stack: [ + OP_CODES.OP_16, + OP_CODES.OP_PUSHDATA_HEX_BE(keypair.evmAddr.substring(2)) + ] + } + const evmAddress = Eth.fromScript(script) + expect(evmAddress).toStrictEqual(keypair.evmAddr.substring(2)) +}) + +it('should return undefined address for invalid evm script', () => { + const script: Script = { + stack: [ + OP_CODES.OP_0, + OP_CODES.OP_PUSHDATA_HEX_BE(keypair.evmAddr.substring(2)) + ] + } + const evmAddress = Eth.fromScript(script) + expect(evmAddress).toStrictEqual(undefined) +}) diff --git a/packages/jellyfish-address/src/eth.ts b/packages/jellyfish-address/src/eth.ts index 1633d945d7..02dee21a28 100644 --- a/packages/jellyfish-address/src/eth.ts +++ b/packages/jellyfish-address/src/eth.ts @@ -1,4 +1,4 @@ -import { OP_CODES, Script } from '@defichain/jellyfish-transaction' +import { OP_CODES, OP_PUSHDATA, Script } from '@defichain/jellyfish-transaction' function validateAddress (address: string): boolean { // https://github.com/ethers-io/ethers.js/blob/5210b68a7837654c6b84207a45e1e573d9472d1a/src.ts/address/address.ts#L123 @@ -6,6 +6,12 @@ function validateAddress (address: string): boolean { return regex.test(address) } +function validateScript (script: Script): boolean { + return script.stack.length === 2 && + script.stack[0].type === OP_CODES.OP_16.type && + script.stack[1].type === 'OP_PUSHDATA' && (script.stack[1] as OP_PUSHDATA).length() === 20 +} + export const Eth = { /** * @param {string} address to convert into Script @@ -18,8 +24,22 @@ export const Eth = { return { stack: [ OP_CODES.OP_16, - OP_CODES.OP_PUSHDATA_HEX_LE(address.substring(2)) + OP_CODES.OP_PUSHDATA_HEX_BE(address.substring(2)) ] } + }, + + /** + * EVM Script is LE in DVM, revert back to BE as what Ethereum behave + * + * @param {Script} script to convert into address + * @returns {string} address parsed from script + */ + fromScript (script: Script): string | undefined { + if (!validateScript(script)) { + return undefined + } + const { hex } = (script.stack[1] as OP_PUSHDATA) + return Buffer.from(hex, 'hex').reverse().toString('hex') } } diff --git a/packages/jellyfish-transaction-builder/__tests__/provider.mock.ts b/packages/jellyfish-transaction-builder/__tests__/provider.mock.ts index e0bc2597ae..4a90806fa5 100644 --- a/packages/jellyfish-transaction-builder/__tests__/provider.mock.ts +++ b/packages/jellyfish-transaction-builder/__tests__/provider.mock.ts @@ -4,7 +4,7 @@ import { EllipticPairProvider, FeeRateProvider, Prevout, PrevoutProvider } from import { MasterNodeRegTestContainer } from '@defichain/testcontainers' import { OP_CODES, Script } from '@defichain/jellyfish-transaction' import { randomEllipticPair } from './test.utils' -import { ListUnspentQueryOptions } from '@defichain/jellyfish-api-core/dist/category/wallet' +import { ListUnspentQueryOptions } from '@defichain/jellyfish-api-core/src/category/wallet' export class MockFeeRateProvider implements FeeRateProvider { constructor ( diff --git a/packages/jellyfish-transaction-builder/src/provider.ts b/packages/jellyfish-transaction-builder/src/provider.ts index b40909f051..e1d24636b8 100644 --- a/packages/jellyfish-transaction-builder/src/provider.ts +++ b/packages/jellyfish-transaction-builder/src/provider.ts @@ -1,7 +1,7 @@ import BigNumber from 'bignumber.js' import { EllipticPair } from '@defichain/jellyfish-crypto' import { Vout } from '@defichain/jellyfish-transaction' -import { ListUnspentQueryOptions } from '@defichain/jellyfish-api-core/dist/category/wallet' +import { ListUnspentQueryOptions } from '@defichain/jellyfish-api-core/src/category/wallet' export interface FeeRateProvider { /** diff --git a/packages/jellyfish-transaction-builder/src/txn/txn_builder.ts b/packages/jellyfish-transaction-builder/src/txn/txn_builder.ts index cad7ca553a..17acfabd8e 100644 --- a/packages/jellyfish-transaction-builder/src/txn/txn_builder.ts +++ b/packages/jellyfish-transaction-builder/src/txn/txn_builder.ts @@ -15,7 +15,7 @@ import { calculateFeeP2WPKH } from './txn_fee' import { TxnBuilderError, TxnBuilderErrorType } from './txn_builder_error' import { EllipticPair } from '@defichain/jellyfish-crypto' import { Network } from '@defichain/jellyfish-network' -import { ListUnspentQueryOptions } from '@defichain/jellyfish-api-core/dist/category/wallet' +import { ListUnspentQueryOptions } from '@defichain/jellyfish-api-core/src/category/wallet' const MAX_FEE_RATE = new BigNumber('0.00100000') diff --git a/packages/jellyfish-transaction-builder/src/txn/txn_builder_account.ts b/packages/jellyfish-transaction-builder/src/txn/txn_builder_account.ts index d80d0bb9cd..7050960945 100644 --- a/packages/jellyfish-transaction-builder/src/txn/txn_builder_account.ts +++ b/packages/jellyfish-transaction-builder/src/txn/txn_builder_account.ts @@ -5,7 +5,7 @@ import { } from '@defichain/jellyfish-transaction' import { P2WPKHTxnBuilder } from './txn_builder' import { TxnBuilderError, TxnBuilderErrorType } from './txn_builder_error' -import { ListUnspentQueryOptions } from '@defichain/jellyfish-api-core/dist/category/wallet' +import { ListUnspentQueryOptions } from '@defichain/jellyfish-api-core/src/category/wallet' export class TxnBuilderAccount extends P2WPKHTxnBuilder { /** diff --git a/packages/jellyfish-wallet/src/wallet_account.ts b/packages/jellyfish-wallet/src/wallet_account.ts index ff915baa72..b2ba950d60 100644 --- a/packages/jellyfish-wallet/src/wallet_account.ts +++ b/packages/jellyfish-wallet/src/wallet_account.ts @@ -55,7 +55,7 @@ export abstract class WalletAccount implements WalletEllipticPair { return { stack: [ OP_CODES.OP_16, - OP_CODES.OP_PUSHDATA_HEX_LE(Eth.fromPubKeyUncompressed(pubKeyUncompressed).substring(2)) + OP_CODES.OP_PUSHDATA_HEX_BE(Eth.fromPubKeyUncompressed(pubKeyUncompressed).substring(2)) ] } }