diff --git a/modules/bitgo/test/unit/bip32util.ts b/modules/bitgo/test/unit/bip32util.ts deleted file mode 100644 index f5e501cc03..0000000000 --- a/modules/bitgo/test/unit/bip32util.ts +++ /dev/null @@ -1,32 +0,0 @@ -/** - * @prettier - */ -import { signMessage, verifyMessage } from '@bitgo/sdk-core'; -import * as crypto from 'crypto'; -import { bip32 } from '@bitgo/utxo-lib'; -import 'should'; - -import * as utxolib from '@bitgo/utxo-lib'; - -describe('bip32util', function () { - function getSeedBuffers(length: number) { - return Array.from({ length }).map((_, i) => crypto.createHash('sha256').update(`${i}`).digest()); - } - it('signMessage/verifyMessage', function () { - const keys = getSeedBuffers(4).map((seed) => bip32.fromSeed(seed)); - const messages = ['hello', 'goodbye']; - keys.forEach((key) => { - messages.forEach((message) => { - const signature = signMessage(message, key, utxolib.networks.bitcoin); - - keys.forEach((otherKey) => { - messages.forEach((otherMessage) => { - verifyMessage(otherMessage, otherKey, signature, utxolib.networks.bitcoin).should.eql( - message === otherMessage && key === otherKey - ); - }); - }); - }); - }); - }); -}); diff --git a/modules/utxo-core/package.json b/modules/utxo-core/package.json index bb714e8c16..7b77fb9bb6 100644 --- a/modules/utxo-core/package.json +++ b/modules/utxo-core/package.json @@ -55,11 +55,7 @@ "@bitgo/utxo-lib": "^11.6.1", "@bitgo/wasm-miniscript": "2.0.0-beta.7", "bip174": "npm:@bitgo-forks/bip174@3.1.0-master.4", - "bitcoinjs-message": "npm:@bitgo-forks/bitcoinjs-message@1.0.0-master.3", - "lodash": "^4.17.15" - }, - "devDependencies": { - "@types/lodash": "^4.14.151" + "bitcoinjs-message": "npm:@bitgo-forks/bitcoinjs-message@1.0.0-master.3" }, "gitHead": "18e460ddf02de2dbf13c2aa243478188fb539f0c" } diff --git a/modules/utxo-core/src/bip32utils.ts b/modules/utxo-core/src/bip32utils.ts index 6e2af495a4..1d15cb9281 100644 --- a/modules/utxo-core/src/bip32utils.ts +++ b/modules/utxo-core/src/bip32utils.ts @@ -1,5 +1,4 @@ import * as utxolib from '@bitgo/utxo-lib'; -import * as _ from 'lodash'; import * as bitcoinMessage from 'bitcoinjs-message'; import { BIP32Interface } from '@bitgo/utxo-lib'; /** @@ -7,7 +6,7 @@ import { BIP32Interface } from '@bitgo/utxo-lib'; * @see {bitcoinMessage.sign} */ export function signMessage( - message: string, + message: string | Buffer, privateKey: BIP32Interface | Buffer, network: { messagePrefix: string } ): Buffer { @@ -17,7 +16,7 @@ export function signMessage( throw new Error(`must provide privateKey`); } } - if (!_.isObject(network) || !_.isString(network.messagePrefix)) { + if (network === null || typeof network !== 'object' || typeof network.messagePrefix !== 'string') { throw new Error(`invalid argument 'network'`); } const compressed = true; @@ -29,7 +28,7 @@ export function signMessage( * @see {bitcoinMessage.verify} */ export function verifyMessage( - message: string, + message: string | Buffer, publicKey: BIP32Interface | Buffer, signature: Buffer, network: { messagePrefix: string } @@ -37,7 +36,7 @@ export function verifyMessage( if (!Buffer.isBuffer(publicKey)) { publicKey = publicKey.publicKey; } - if (!_.isObject(network) || !_.isString(network.messagePrefix)) { + if (network === null || typeof network !== 'object' || typeof network.messagePrefix !== 'string') { throw new Error(`invalid argument 'network'`); } diff --git a/modules/utxo-core/test/bip32utils.ts b/modules/utxo-core/test/bip32utils.ts new file mode 100644 index 0000000000..0f5b94b088 --- /dev/null +++ b/modules/utxo-core/test/bip32utils.ts @@ -0,0 +1,32 @@ +import * as crypto from 'crypto'; +import * as assert from 'assert'; + +import * as utxolib from '@bitgo/utxo-lib'; + +import { signMessage, verifyMessage } from '../src/bip32utils'; + +describe('bip32utils', function () { + function getSeedBuffers(length: number) { + return Array.from({ length }).map((_, i) => crypto.createHash('sha256').update(`${i}`).digest()); + } + it('signMessage/verifyMessage', function () { + const keys = getSeedBuffers(4).map((seed) => utxolib.bip32.fromSeed(seed)); + const messages = ['hello', 'goodbye', Buffer.from('\x01\x02\x03'), Buffer.from('')]; + keys.forEach((key) => { + messages.forEach((message) => { + const signature = signMessage(message, key, utxolib.networks.bitcoin); + + keys.forEach((otherKey) => { + messages.forEach((otherMessage) => { + const expectValid = message === otherMessage && key === otherKey; + assert.strictEqual(verifyMessage(otherMessage, otherKey, signature, utxolib.networks.bitcoin), expectValid); + assert.strictEqual( + verifyMessage(Buffer.from(otherMessage), otherKey, signature, utxolib.networks.bitcoin), + expectValid + ); + }); + }); + }); + }); + }); +});