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

Allow alternative address prefixes in bip122 blockchain account ids #206

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 5 additions & 3 deletions src/blockchains/bip122.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import * as u8a from 'uint8arrays'
import { bytesToBase58 } from '../util'
import { bytesToBase58, base58ToBytes } from '../util'
import { sha256 } from '../Digest'
import { Ripemd160 } from './utils/ripemd160'

export const publicKeyToAddress = (publicKey: string): string => {
export const publicKeyToAddress = (publicKey: string, otherAddress: string): string => {
// Use the same version/prefix byte as the given address.
const version = u8a.toString(base58ToBytes(otherAddress).slice(0, 1), 'hex')
const publicKeyBuffer = u8a.fromString(publicKey, 'hex')
const publicKeyHash = new Ripemd160().update(sha256(publicKeyBuffer)).digest()
const step1 = '00' + u8a.toString(publicKeyHash, 'hex')
const step1 = version + u8a.toString(publicKeyHash, 'hex')
const step2 = sha256(u8a.fromString(step1, 'hex'))
const step3 = sha256(step2)
const checksum = u8a.toString(step3, 'hex').substring(0, 8)
Expand Down
2 changes: 1 addition & 1 deletion src/blockchains/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const verifyBlockchainAccountId = (publicKey: string, blockchainAccountId
const chain = blockchainAccountId.split(':')
switch (chain[0]) {
case 'bip122':
chain[chain.length - 1] = bip122(publicKey)
chain[chain.length - 1] = bip122(publicKey, chain[chain.length - 1])
break
case 'cosmos':
chain[chain.length - 1] = cosmos(publicKey, chain[1])
Expand Down