From 06e8971e0559042d89dd60892b5a6bcd1d75d3e4 Mon Sep 17 00:00:00 2001 From: "Charles E. Lehner" Date: Thu, 18 Nov 2021 21:16:20 -0500 Subject: [PATCH] Allow alternative Bitcoin address prefixes --- src/blockchains/bip122.ts | 8 +++++--- src/blockchains/index.ts | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/blockchains/bip122.ts b/src/blockchains/bip122.ts index 07401c72..6976e9b9 100644 --- a/src/blockchains/bip122.ts +++ b/src/blockchains/bip122.ts @@ -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) diff --git a/src/blockchains/index.ts b/src/blockchains/index.ts index 7e741fe4..74377056 100644 --- a/src/blockchains/index.ts +++ b/src/blockchains/index.ts @@ -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])