Skip to content

Commit

Permalink
feat(jellyfish-crypto): add fromAddress to convert eth address into s…
Browse files Browse the repository at this point in the history
…cript (#2134)

<!--  Thanks for sending a pull request! -->

#### What this PR does / why we need it:
To get EVM script for an ETH address that is not owned by the same
wallet. Will be used on the `transferDomain` transaction builder that
only accepts script as address

#### 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)`.
-->
Fixes DFC-170

#### Additional comments?:
  • Loading branch information
lykalabrada committed Aug 7, 2023
1 parent 26a7b82 commit 5086a59
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
23 changes: 23 additions & 0 deletions packages/jellyfish-address/__tests__/eth.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { OP_CODES } from '@defichain/jellyfish-transaction'
import { Eth } from '../src'

const keypair = {
evmAddr: '0x0a06de8abc3f15359ec0dfe32394c8b8f09e828f',
checksumEvmAddr: '0x0a06DE8AbC3f15359EC0dfe32394C8B8f09e828F',
pubKeyUncompressed: '04e60942751bc776912cdc8cf11aa3ce33ce3ef6882ff93a9fafa0b968e6b926293a6913f9efae6362ce8ffd0b8a4ae45c3a6ccafacbab2192991125277d6710db'
}

it('should convert evm address to script', () => {
const evmScript = Eth.fromAddress(keypair.evmAddr)
expect(evmScript).toStrictEqual({
stack: [
OP_CODES.OP_16,
OP_CODES.OP_PUSHDATA_HEX_LE(keypair.evmAddr.substring(2))
]
})
})

it('should return undefined script for invalid eth address', () => {
const evmScript = Eth.fromAddress('0xabc123')
expect(evmScript).toStrictEqual(undefined)
})
25 changes: 25 additions & 0 deletions packages/jellyfish-address/src/eth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { OP_CODES, 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)
}

export const Eth = {
/**
* @param {string} address to convert into Script
* @return {Script} Script parsed from address
*/
fromAddress (address: string): Script | undefined {
if (!validateAddress(address)) {
return undefined
}
return {
stack: [
OP_CODES.OP_16,
OP_CODES.OP_PUSHDATA_HEX_LE(address.substring(2))
]
}
}
}
1 change: 1 addition & 0 deletions packages/jellyfish-address/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export * from './p2pkh'
export * from './p2sh'
export * from './p2wpkh'
export * from './p2wsh'
export * from './eth'

/**
* When insist to use the "network" decoded from raw address, instead of passing one based on running application environment
Expand Down

0 comments on commit 5086a59

Please sign in to comment.