Skip to content

Commit 8baa393

Browse files
authored
feat(jellyfish-address, jellyfish-wallet): add eth.fromScript (#2157)
<!-- Thanks for sending a pull request! --> #### What this PR does / why we need it: add `eth.fromScript(evmScript) -> evmAddr` and "PR pool" some fixes #### Which issue(s) does this PR fixes?: <!-- (Optional) Automatically closes linked issue when PR is merged. Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`. --> - [x] use `src` on import [e63af92](e63af92) - [x] fix missing eth BE [291fd1f](291fd1f) - [x] add eth.fromScript [35aa1e5](35aa1e5) #### Additional comments?:
1 parent d2076b8 commit 8baa393

File tree

7 files changed

+51
-9
lines changed

7 files changed

+51
-9
lines changed

packages/jellyfish-address/__tests__/eth.test.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { OP_CODES } from '@defichain/jellyfish-transaction'
1+
import { OP_CODES, Script } from '@defichain/jellyfish-transaction'
22
import { Eth } from '../src'
33

44
const keypair = {
@@ -12,7 +12,7 @@ it('should convert evm address to script', () => {
1212
expect(evmScript).toStrictEqual({
1313
stack: [
1414
OP_CODES.OP_16,
15-
OP_CODES.OP_PUSHDATA_HEX_LE(keypair.evmAddr.substring(2))
15+
OP_CODES.OP_PUSHDATA_HEX_BE(keypair.evmAddr.substring(2))
1616
]
1717
})
1818
})
@@ -21,3 +21,25 @@ it('should return undefined script for invalid eth address', () => {
2121
const evmScript = Eth.fromAddress('0xabc123')
2222
expect(evmScript).toStrictEqual(undefined)
2323
})
24+
25+
it('should convert evm script to address', () => {
26+
const script: Script = {
27+
stack: [
28+
OP_CODES.OP_16,
29+
OP_CODES.OP_PUSHDATA_HEX_BE(keypair.evmAddr.substring(2))
30+
]
31+
}
32+
const evmAddress = Eth.fromScript(script)
33+
expect(evmAddress).toStrictEqual(keypair.evmAddr.substring(2))
34+
})
35+
36+
it('should return undefined address for invalid evm script', () => {
37+
const script: Script = {
38+
stack: [
39+
OP_CODES.OP_0,
40+
OP_CODES.OP_PUSHDATA_HEX_BE(keypair.evmAddr.substring(2))
41+
]
42+
}
43+
const evmAddress = Eth.fromScript(script)
44+
expect(evmAddress).toStrictEqual(undefined)
45+
})

packages/jellyfish-address/src/eth.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
import { OP_CODES, Script } from '@defichain/jellyfish-transaction'
1+
import { OP_CODES, OP_PUSHDATA, Script } from '@defichain/jellyfish-transaction'
22

33
function validateAddress (address: string): boolean {
44
// https://github.com/ethers-io/ethers.js/blob/5210b68a7837654c6b84207a45e1e573d9472d1a/src.ts/address/address.ts#L123
55
const regex: RegExp = /^0x[a-fA-F0-9]{40}$/gm
66
return regex.test(address)
77
}
88

9+
function validateScript (script: Script): boolean {
10+
return script.stack.length === 2 &&
11+
script.stack[0].type === OP_CODES.OP_16.type &&
12+
script.stack[1].type === 'OP_PUSHDATA' && (script.stack[1] as OP_PUSHDATA).length() === 20
13+
}
14+
915
export const Eth = {
1016
/**
1117
* @param {string} address to convert into Script
@@ -18,8 +24,22 @@ export const Eth = {
1824
return {
1925
stack: [
2026
OP_CODES.OP_16,
21-
OP_CODES.OP_PUSHDATA_HEX_LE(address.substring(2))
27+
OP_CODES.OP_PUSHDATA_HEX_BE(address.substring(2))
2228
]
2329
}
30+
},
31+
32+
/**
33+
* EVM Script is LE in DVM, revert back to BE as what Ethereum behave
34+
*
35+
* @param {Script} script to convert into address
36+
* @returns {string} address parsed from script
37+
*/
38+
fromScript (script: Script): string | undefined {
39+
if (!validateScript(script)) {
40+
return undefined
41+
}
42+
const { hex } = (script.stack[1] as OP_PUSHDATA)
43+
return Buffer.from(hex, 'hex').reverse().toString('hex')
2444
}
2545
}

packages/jellyfish-transaction-builder/__tests__/provider.mock.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { EllipticPairProvider, FeeRateProvider, Prevout, PrevoutProvider } from
44
import { MasterNodeRegTestContainer } from '@defichain/testcontainers'
55
import { OP_CODES, Script } from '@defichain/jellyfish-transaction'
66
import { randomEllipticPair } from './test.utils'
7-
import { ListUnspentQueryOptions } from '@defichain/jellyfish-api-core/dist/category/wallet'
7+
import { ListUnspentQueryOptions } from '@defichain/jellyfish-api-core/src/category/wallet'
88

99
export class MockFeeRateProvider implements FeeRateProvider {
1010
constructor (

packages/jellyfish-transaction-builder/src/provider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import BigNumber from 'bignumber.js'
22
import { EllipticPair } from '@defichain/jellyfish-crypto'
33
import { Vout } from '@defichain/jellyfish-transaction'
4-
import { ListUnspentQueryOptions } from '@defichain/jellyfish-api-core/dist/category/wallet'
4+
import { ListUnspentQueryOptions } from '@defichain/jellyfish-api-core/src/category/wallet'
55

66
export interface FeeRateProvider {
77
/**

packages/jellyfish-transaction-builder/src/txn/txn_builder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { calculateFeeP2WPKH } from './txn_fee'
1515
import { TxnBuilderError, TxnBuilderErrorType } from './txn_builder_error'
1616
import { EllipticPair } from '@defichain/jellyfish-crypto'
1717
import { Network } from '@defichain/jellyfish-network'
18-
import { ListUnspentQueryOptions } from '@defichain/jellyfish-api-core/dist/category/wallet'
18+
import { ListUnspentQueryOptions } from '@defichain/jellyfish-api-core/src/category/wallet'
1919

2020
const MAX_FEE_RATE = new BigNumber('0.00100000')
2121

packages/jellyfish-transaction-builder/src/txn/txn_builder_account.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
} from '@defichain/jellyfish-transaction'
66
import { P2WPKHTxnBuilder } from './txn_builder'
77
import { TxnBuilderError, TxnBuilderErrorType } from './txn_builder_error'
8-
import { ListUnspentQueryOptions } from '@defichain/jellyfish-api-core/dist/category/wallet'
8+
import { ListUnspentQueryOptions } from '@defichain/jellyfish-api-core/src/category/wallet'
99

1010
export class TxnBuilderAccount extends P2WPKHTxnBuilder {
1111
/**

packages/jellyfish-wallet/src/wallet_account.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export abstract class WalletAccount implements WalletEllipticPair {
5555
return {
5656
stack: [
5757
OP_CODES.OP_16,
58-
OP_CODES.OP_PUSHDATA_HEX_LE(Eth.fromPubKeyUncompressed(pubKeyUncompressed).substring(2))
58+
OP_CODES.OP_PUSHDATA_HEX_BE(Eth.fromPubKeyUncompressed(pubKeyUncompressed).substring(2))
5959
]
6060
}
6161
}

0 commit comments

Comments
 (0)