Skip to content

Commit a6b854b

Browse files
OttoAllmendingerllm-git
andcommitted
feat(abstract-utxo): replace bitcoinjs-message with wasm-utxo
Replace dependency on bitcoinjs-message with @bitgo/wasm-utxo for message verification. Remove unused helper functions that are now handled by the wasm module. This simplifies the code and reduces dependencies. Issue: BTC-2768 Co-authored-by: llm-git <llm-git@ttll.de>
1 parent 28bab17 commit a6b854b

2 files changed

Lines changed: 5 additions & 29 deletions

File tree

modules/abstract-utxo/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@
7070
"@types/lodash": "^4.14.121",
7171
"@types/superagent": "4.1.15",
7272
"bignumber.js": "^9.0.2",
73-
"bitcoinjs-message": "npm:@bitgo-forks/bitcoinjs-message@1.0.0-master.3",
7473
"debug": "^3.1.0",
7574
"io-ts": "npm:@bitgo-forks/io-ts@2.1.4",
7675
"lodash": "^4.17.14",

modules/abstract-utxo/src/verifyKey.ts

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@ These are actually not utxo-specific and belong in a more general module.
44
55
*/
66
import assert from 'assert';
7-
import { createHash } from 'crypto';
87

98
import 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';
1310
import { BitGoBase, decryptKeychainPrivateKey, KeyIndices } from '@bitgo/sdk-core';
1411

1512
import { VerifyKeySignaturesOptions, VerifyUserPublicKeyOptions } from './abstractUtxoCoin';
@@ -18,21 +15,6 @@ import { UtxoKeychain } from './keychains';
1815

1916
const 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

Comments
 (0)