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
  • Loading branch information
lykalabrada committed Jul 31, 2023
1 parent 89b2dd1 commit 7836dc3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/jellyfish-crypto/__tests__/eth.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { OP_CODES } from '@defichain/jellyfish-transaction'
import { Eth } from '../src'

const keypair = {
Expand All @@ -18,3 +19,13 @@ it('should convert evm address to checksum address', () => {
const checksumEvmAddr = Eth.fromPubKeyUncompressed(pubKeyUncompressed)
expect(checksumEvmAddr).toStrictEqual(keypair.checksumEvmAddr)
})

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))
]
})
})
22 changes: 22 additions & 0 deletions packages/jellyfish-crypto/src/eth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import createKeccakHash from 'keccak'
import { KECCAK256 } from './hash'
import { OP_CODES, Script } from '@defichain/jellyfish-transaction'

// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md
function toChecksumAddress (address: string): string {
Expand All @@ -23,6 +24,12 @@ function toAddress (hash: Buffer): string {
return `0x${sliced}`
}

function validateAddress (address: string): boolean {

Check warning on line 27 in packages/jellyfish-crypto/src/eth.ts

View check run for this annotation

Codecov / codecov/patch

packages/jellyfish-crypto/src/eth.ts#L27

Added line #L27 was not covered by tests
// 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)

Check warning on line 30 in packages/jellyfish-crypto/src/eth.ts

View check run for this annotation

Codecov / codecov/patch

packages/jellyfish-crypto/src/eth.ts#L29-L30

Added lines #L29 - L30 were not covered by tests
}

export const Eth = {
/**
* @param {Buffer} uncompressed pubKey to format into Eth
Expand All @@ -35,5 +42,20 @@ export const Eth = {
const sub = pubKeyUncompressed.subarray(1, 65)
const hash = KECCAK256(sub)
return toChecksumAddress(toAddress(hash))
},
/**
* @param {string} address to convert into Script
* @return {Script} Script parsed from address
*/
fromAddress (address: string): Script | undefined {

Check warning on line 50 in packages/jellyfish-crypto/src/eth.ts

View check run for this annotation

Codecov / codecov/patch

packages/jellyfish-crypto/src/eth.ts#L50

Added line #L50 was not covered by tests
if (!validateAddress(address)) {
return undefined

Check warning on line 52 in packages/jellyfish-crypto/src/eth.ts

View check run for this annotation

Codecov / codecov/patch

packages/jellyfish-crypto/src/eth.ts#L52

Added line #L52 was not covered by tests
}
return {

Check warning on line 54 in packages/jellyfish-crypto/src/eth.ts

View check run for this annotation

Codecov / codecov/patch

packages/jellyfish-crypto/src/eth.ts#L54

Added line #L54 was not covered by tests
stack: [
OP_CODES.OP_16,
OP_CODES.OP_PUSHDATA_HEX_LE(address.substring(2))
]
}
}
}

0 comments on commit 7836dc3

Please sign in to comment.