-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathbip32utils.ts
More file actions
57 lines (48 loc) · 2.27 KB
/
bip32utils.ts
File metadata and controls
57 lines (48 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import * as crypto from 'crypto';
import * as assert from 'assert';
import { bip32, bip32utils } from '../src';
const { signMessage, verifyMessage } = bip32utils;
// Bitcoin mainnet message prefix - matches utxolib.networks.bitcoin.messagePrefix
const bitcoinNetwork = {
messagePrefix: '\x18Bitcoin Signed Message:\n',
};
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) => 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, bitcoinNetwork);
keys.forEach((otherKey) => {
messages.forEach((otherMessage) => {
const expectValid = message === otherMessage && key === otherKey;
assert.strictEqual(verifyMessage(otherMessage, otherKey, signature, bitcoinNetwork), expectValid);
assert.strictEqual(
verifyMessage(Buffer.from(otherMessage), otherKey, signature, bitcoinNetwork),
expectValid
);
});
});
});
});
});
it('signMessage throws on missing privateKey', function () {
const key = bip32.fromSeed(getSeedBuffers(1)[0]);
const neutered = key.neutered();
assert.throws(() => signMessage('hello', neutered, bitcoinNetwork), /must provide privateKey/);
});
it('signMessage throws on invalid network', function () {
const key = bip32.fromSeed(getSeedBuffers(1)[0]);
assert.throws(() => signMessage('hello', key, null as any), /invalid argument 'network'/);
assert.throws(() => signMessage('hello', key, {} as any), /invalid argument 'network'/);
});
it('verifyMessage throws on invalid network', function () {
const key = bip32.fromSeed(getSeedBuffers(1)[0]);
const signature = signMessage('hello', key, bitcoinNetwork);
assert.throws(() => verifyMessage('hello', key, signature, null as any), /invalid argument 'network'/);
assert.throws(() => verifyMessage('hello', key, signature, {} as any), /invalid argument 'network'/);
});
});