-
Notifications
You must be signed in to change notification settings - Fork 37
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, jellyfish-transaction-builder): construct evm …
…script (#2105) <!-- Thanks for sending a pull request! --> #### What this PR does / why we need it: To construct evm script for txn builder - add uncompressed pubkey to support evm addr - using keccak to generate evm addr from uncompressed pubkey #### 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 # #### Additional comments?:
- Loading branch information
1 parent
b97bcd9
commit cf27d7e
Showing
21 changed files
with
297 additions
and
39 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,20 @@ | ||
import { Eth } from '../src' | ||
|
||
const keypair = { | ||
evmAddr: '0x0a06de8abc3f15359ec0dfe32394c8b8f09e828f', | ||
checksumEvmAddr: '0x0a06DE8AbC3f15359EC0dfe32394C8B8f09e828F', | ||
pubKeyUncompressed: '04e60942751bc776912cdc8cf11aa3ce33ce3ef6882ff93a9fafa0b968e6b926293a6913f9efae6362ce8ffd0b8a4ae45c3a6ccafacbab2192991125277d6710db' | ||
} | ||
|
||
it('should reject invalid length uncompressed public key', () => { | ||
expect(() => { | ||
// @ts-expect_error | ||
Eth.fromPubKeyUncompressed(Buffer.from(keypair.pubKeyUncompressed, 'hex').subarray(1)) | ||
}).toThrow('InvalidUncompressedPubKeyLength') | ||
}) | ||
|
||
it('should convert evm address to checksum address', () => { | ||
const pubKeyUncompressed = Buffer.from(keypair.pubKeyUncompressed, 'hex') | ||
const checksumEvmAddr = Eth.fromPubKeyUncompressed(pubKeyUncompressed) | ||
expect(checksumEvmAddr).toStrictEqual(keypair.checksumEvmAddr) | ||
}) |
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
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
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
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,39 @@ | ||
import createKeccakHash from 'keccak' | ||
import { KECCAK256 } from './hash' | ||
|
||
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md | ||
function toChecksumAddress (address: string): string { | ||
address = address.toLowerCase().replace('0x', '') | ||
const hash = createKeccakHash('keccak256').update(address).digest('hex') | ||
let checksum = '0x' | ||
|
||
for (let i = 0; i < address.length; i += 1) { | ||
parseInt(hash[i], 16) >= 8 | ||
? checksum += address[i].toUpperCase() | ||
: checksum += address[i] | ||
} | ||
|
||
return checksum | ||
} | ||
|
||
function toAddress (hash: Buffer): string { | ||
const hex = hash.toString('hex') | ||
// grab the last 20 bytes (40 chars) | ||
const sliced = hex.slice(hex.length - 40) | ||
return `0x${sliced}` | ||
} | ||
|
||
export const Eth = { | ||
/** | ||
* @param {Buffer} uncompressed pubKey to format into Eth | ||
* @return {string} eth encoded address | ||
*/ | ||
fromPubKeyUncompressed (pubKeyUncompressed: Buffer): string { | ||
if (pubKeyUncompressed.length !== 65) { | ||
throw new Error('InvalidUncompressedPubKeyLength') | ||
} | ||
const sub = pubKeyUncompressed.subarray(1, 65) | ||
const hash = KECCAK256(sub) | ||
return toChecksumAddress(toAddress(hash)) | ||
} | ||
} |
Oops, something went wrong.