Skip to content

Commit

Permalink
feat(jellyfish-address, jellyfish-wallet): add eth.fromScript (#2157)
Browse files Browse the repository at this point in the history
<!--  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?:
  • Loading branch information
canonbrother committed Sep 26, 2023
1 parent d2076b8 commit 8baa393
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 9 deletions.
26 changes: 24 additions & 2 deletions packages/jellyfish-address/__tests__/eth.test.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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))
]
})
})
Expand All @@ -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)
})
24 changes: 22 additions & 2 deletions packages/jellyfish-address/src/eth.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
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
const regex: RegExp = /^0x[a-fA-F0-9]{40}$/gm
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
Expand All @@ -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')
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion packages/jellyfish-transaction-builder/src/provider.ts
Original file line number Diff line number Diff line change
@@ -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 {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down
2 changes: 1 addition & 1 deletion packages/jellyfish-wallet/src/wallet_account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
]
}
}
Expand Down

0 comments on commit 8baa393

Please sign in to comment.