-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(jellyfish-crypto): add fromAddress to convert eth address into s…
…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
1 parent
26a7b82
commit 5086a59
Showing
3 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters