Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jellyfish-crypto): add fromAddress to convert eth address into script #2134

Merged
merged 5 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 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,18 @@ 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))
]
})
})

it('should return undefined script for invalid eth address', () => {
const evmScript = Eth.fromAddress('0xabc123')
expect(evmScript).toStrictEqual(undefined)
})
1 change: 1 addition & 0 deletions packages/jellyfish-crypto/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"build": "tsc -b ./tsconfig.build.json"
},
"dependencies": {
"@defichain/jellyfish-transaction": "^0.0.0",
thedoublejay marked this conversation as resolved.
Show resolved Hide resolved
"bech32": "^2.0.0",
"bip66": "^1.1.5",
"browserify-aes": "^1.2.0",
Expand Down
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 @@
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 @@
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))
]
}
}
}