-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: CAIP 10 support for bip122 & cosmos (#205)
* blockchainAccountId (CAIP - 10) * update test code * remove crypto-js * remove ripemd160 * remove Buffer Buffer -> u8a * add Ripemd160.test.ts This contains a typescript implementation of RIPEMD160: https://homes.esat.kuleuven.be/~bosselae/ripemd160.html
- Loading branch information
Showing
9 changed files
with
407 additions
and
9 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
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,70 @@ | ||
// https://homes.esat.kuleuven.be/~bosselae/ripemd160.html | ||
|
||
import * as u8a from 'uint8arrays' | ||
import { Ripemd160 } from '../blockchains/utils/ripemd160' | ||
|
||
describe('Ripemd160', () => { | ||
it('message: "" (empty string)', () => { | ||
expect.assertions(1) | ||
const rawString = '' | ||
const expectedHash = '9c1185a5c5e9fc54612808977ee8f548b2258d31' | ||
const actualHash = u8a.toString(new Ripemd160().update(u8a.fromString(rawString, 'ascii')).digest(), 'hex') | ||
return expect(actualHash).toBe(expectedHash) | ||
}) | ||
it('message: "a"', () => { | ||
expect.assertions(1) | ||
const rawString = 'a' | ||
const expectedHash = '0bdc9d2d256b3ee9daae347be6f4dc835a467ffe' | ||
const actualHash = u8a.toString(new Ripemd160().update(u8a.fromString(rawString, 'ascii')).digest(), 'hex') | ||
return expect(actualHash).toBe(expectedHash) | ||
}) | ||
it('message: "abc"', () => { | ||
expect.assertions(1) | ||
const rawString = 'abc' | ||
const expectedHash = '8eb208f7e05d987a9b044a8e98c6b087f15a0bfc' | ||
const actualHash = u8a.toString(new Ripemd160().update(u8a.fromString(rawString, 'ascii')).digest(), 'hex') | ||
return expect(actualHash).toBe(expectedHash) | ||
}) | ||
it('message: "message digest"', () => { | ||
expect.assertions(1) | ||
const rawString = 'message digest' | ||
const expectedHash = '5d0689ef49d2fae572b881b123a85ffa21595f36' | ||
const actualHash = u8a.toString(new Ripemd160().update(u8a.fromString(rawString, 'ascii')).digest(), 'hex') | ||
return expect(actualHash).toBe(expectedHash) | ||
}) | ||
it('message: "abcdefghijklmnopqrstuvwxyz"', () => { | ||
expect.assertions(1) | ||
const rawString = 'abcdefghijklmnopqrstuvwxyz' | ||
const expectedHash = 'f71c27109c692c1b56bbdceb5b9d2865b3708dbc' | ||
const actualHash = u8a.toString(new Ripemd160().update(u8a.fromString(rawString, 'ascii')).digest(), 'hex') | ||
return expect(actualHash).toBe(expectedHash) | ||
}) | ||
it('message: "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"', () => { | ||
expect.assertions(1) | ||
const rawString = 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq' | ||
const expectedHash = '12a053384a9c0c88e405a06c27dcf49ada62eb2b' | ||
const actualHash = u8a.toString(new Ripemd160().update(u8a.fromString(rawString, 'ascii')).digest(), 'hex') | ||
return expect(actualHash).toBe(expectedHash) | ||
}) | ||
it('message: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"', () => { | ||
expect.assertions(1) | ||
const rawString = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' | ||
const expectedHash = 'b0e20b6e3116640286ed3a87a5713079b21f5189' | ||
const actualHash = u8a.toString(new Ripemd160().update(u8a.fromString(rawString, 'ascii')).digest(), 'hex') | ||
return expect(actualHash).toBe(expectedHash) | ||
}) | ||
it('message: 8 times "1234567890"', () => { | ||
expect.assertions(1) | ||
const rawString = '1234567890'.repeat(8) | ||
const expectedHash = '9b752e45573d4b39f4dbd3323cab82bf63326bfb' | ||
const actualHash = u8a.toString(new Ripemd160().update(u8a.fromString(rawString, 'ascii')).digest(), 'hex') | ||
return expect(actualHash).toBe(expectedHash) | ||
}) | ||
it('message: 1 million times "a"', () => { | ||
expect.assertions(1) | ||
const rawString = 'a'.repeat(1000000) | ||
const expectedHash = '52783243c1697bdbe16d37f97f68f08325dc1528' | ||
const actualHash = u8a.toString(new Ripemd160().update(u8a.fromString(rawString, 'ascii')).digest(), 'hex') | ||
return expect(actualHash).toBe(expectedHash) | ||
}) | ||
}); |
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,15 @@ | ||
import * as u8a from 'uint8arrays' | ||
import { bytesToBase58 } from '../util' | ||
import { sha256 } from '../Digest' | ||
import { Ripemd160 } from './utils/ripemd160' | ||
|
||
export const publicKeyToAddress = (publicKey: string): string => { | ||
const publicKeyBuffer = u8a.fromString(publicKey, 'hex') | ||
const publicKeyHash = new Ripemd160().update(sha256(publicKeyBuffer)).digest() | ||
const step1 = '00' + u8a.toString(publicKeyHash, 'hex') | ||
const step2 = sha256(u8a.fromString(step1, 'hex')) | ||
const step3 = sha256(step2) | ||
const checksum = u8a.toString(step3, 'hex').substring(0, 8) | ||
const step4 = step1 + checksum | ||
return bytesToBase58(u8a.fromString(step4, 'hex')) | ||
} |
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,14 @@ | ||
import { ec as EC } from 'elliptic' | ||
import { bech32 } from 'bech32' | ||
import * as u8a from 'uint8arrays' | ||
import { sha256 } from '../Digest' | ||
import { Ripemd160 } from './utils/ripemd160' | ||
|
||
export const publicKeyToAddress = (publicKey: string, prefix: string): string => { | ||
const ec = new EC('secp256k1') | ||
const compressedPublicKey = ec.keyFromPublic(publicKey, 'hex').getPublic().encode('hex', true) | ||
const publicKeyBuffer = u8a.fromString(compressedPublicKey, 'hex') | ||
const hash = new Ripemd160().update(sha256(publicKeyBuffer)).digest() | ||
const words = bech32.toWords(hash) | ||
return bech32.encode(prefix, words).replace(prefix, '') | ||
} |
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,24 @@ | ||
import { publicKeyToAddress as bip122 } from './bip122' | ||
import { publicKeyToAddress as cosmos } from './cosmos' | ||
import { toEthereumAddress } from '../Digest' | ||
|
||
export const verifyBlockchainAccountId = (publicKey: string, blockchainAccountId: string | undefined): boolean => { | ||
if (blockchainAccountId) { | ||
const chain = blockchainAccountId.split(':') | ||
switch (chain[0]) { | ||
case 'bip122': | ||
chain[chain.length - 1] = bip122(publicKey) | ||
break | ||
case 'cosmos': | ||
chain[chain.length - 1] = cosmos(publicKey, chain[1]) | ||
break | ||
case 'eip155': | ||
chain[chain.length - 1] = toEthereumAddress(publicKey) | ||
break | ||
default: | ||
return false | ||
} | ||
return chain.join(':') === blockchainAccountId | ||
} | ||
return false | ||
} |
Oops, something went wrong.