@@ -4,12 +4,9 @@ These are actually not utxo-specific and belong in a more general module.
44
55 */
66import assert from 'assert' ;
7- import { createHash } from 'crypto' ;
87
98import buildDebug from 'debug' ;
10- import { bip32 } from '@bitgo/secp256k1' ;
11- import * as bitcoinMessage from 'bitcoinjs-message' ;
12- import bs58check from 'bs58check' ;
9+ import { BIP32 , message } from '@bitgo/wasm-utxo' ;
1310import { BitGoBase , decryptKeychainPrivateKey , KeyIndices } from '@bitgo/sdk-core' ;
1411
1512import { VerifyKeySignaturesOptions , VerifyUserPublicKeyOptions } from './abstractUtxoCoin' ;
@@ -18,21 +15,6 @@ import { UtxoKeychain } from './keychains';
1815
1916const debug = buildDebug ( 'bitgo:abstract-utxo:verifyKey' ) ;
2017
21- function hash160 ( data : Buffer ) : Buffer {
22- const sha256 = createHash ( 'sha256' ) . update ( data ) . digest ( ) ;
23- return createHash ( 'ripemd160' ) . update ( sha256 ) . digest ( ) ;
24- }
25-
26- // Bitcoin mainnet pubKeyHash version byte
27- const BITCOIN_PUBKEY_HASH_VERSION = 0x00 ;
28-
29- function toBase58Check ( hash : Buffer , version : number ) : string {
30- const payload = Buffer . allocUnsafe ( 21 ) ;
31- payload . writeUInt8 ( version , 0 ) ;
32- hash . copy ( payload , 1 ) ;
33- return bs58check . encode ( payload ) ;
34- }
35-
3618/**
3719 * Verify signatures produced by the user key over the backup and bitgo keys.
3820 *
@@ -55,18 +37,13 @@ export function verifyKeySignature(params: VerifyKeySignaturesOptions): boolean
5537 }
5638
5739 assert ( userKeychain . pub ) ;
58- const publicKey = bip32 . fromBase58 ( userKeychain . pub ) . publicKey ;
59- // Due to interface of `bitcoinMessage`, we need to convert the public key to an address.
60- // Note that this address has no relationship to on-chain transactions. We are
61- // only interested in the address as a representation of the public key.
62- // BG-5703: use BTC mainnet prefix for all key signature operations
63- const signingAddress = toBase58Check ( hash160 ( publicKey ) , BITCOIN_PUBKEY_HASH_VERSION ) ;
40+ const publicKey = BIP32 . fromBase58 ( userKeychain . pub ) . publicKey ;
6441
6542 assert ( keychainToVerify . pub ) ;
6643 try {
67- return bitcoinMessage . verify ( keychainToVerify . pub , signingAddress , Buffer . from ( keySignature , 'hex' ) ) ;
44+ return message . verifyMessage ( keychainToVerify . pub , publicKey , Buffer . from ( keySignature , 'hex' ) ) ;
6845 } catch ( e ) {
69- debug ( 'error thrown from bitcoinmessage while verifying key signature' , e ) ;
46+ debug ( 'error thrown from wasm-utxo while verifying key signature' , e ) ;
7047 return false ;
7148 }
7249}
@@ -129,7 +106,7 @@ export function verifyUserPublicKey(bitgo: BitGoBase, params: VerifyUserPublicKe
129106 throw new Error ( errorMessage ) ;
130107 }
131108 } else {
132- const userPrivateKey = bip32 . fromBase58 ( userPrv ) ;
109+ const userPrivateKey = BIP32 . fromBase58 ( userPrv ) ;
133110 if ( userPrivateKey . toBase58 ( ) === userPrivateKey . neutered ( ) . toBase58 ( ) ) {
134111 throw new Error ( 'user private key is only public' ) ;
135112 }
0 commit comments