-
Notifications
You must be signed in to change notification settings - Fork 11
/
stealthSpec.js
192 lines (152 loc) · 7.98 KB
/
stealthSpec.js
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* @fileOverview Stealth support.
*/
'use strict';
var Stealth = require('./stealth');
var Bitcoin = require('./bitcoinjs-lib-wrapper');
var BigInteger = require('bigi');
var Buffer = require('buffer').Buffer;
var bufToArray = function(obj) {return Array.prototype.slice.call(obj, 0);};
// Data for the test
var ephemKeyBytes = [95, 112, 167, 123, 50, 38, 10, 122, 50, 198, 34, 66, 56, 31, 186, 44, 244, 12, 14, 32, 158, 102, 90, 121, 89, 65, 142, 174, 79, 45, 162, 43, 1];
var scanKeyBytes = [250, 99, 82, 30, 51, 62, 75, 159, 106, 152, 161, 66, 104, 13, 58, 239, 77, 142, 127, 121, 114, 60, 224, 4, 54, 145, 219, 85, 195, 107, 217, 5];
var spendKeyBytes = [220, 193, 37, 11, 81, 192, 240, 58, 228, 233, 120, 224, 37, 110, 222, 81, 220, 17, 68, 227, 69, 201, 38, 38, 43, 151, 23, 177, 188, 201, 189, 27];
var ephemKey = Bitcoin.ECKey.fromBytes(ephemKeyBytes, true);
var scanKey = Bitcoin.ECKey.fromBytes(scanKeyBytes, true);
var spendKey = Bitcoin.ECKey.fromBytes(spendKeyBytes, true);
var ephemKeyPubBytes = ephemKey.pub.toBytes();
var scanKeyPubBytes = scanKey.pub.toBytes();
var spendKeyPubBytes = spendKey.pub.toBytes();
// Address created from the above keys
var testAddress = "vJmxHgVSxtBcoe9JQTz9qKN8ZnKcehm37FWwSfejU4AeBuxB8sX6hVPCh2iUJsAdVpbgRdUd3CUPzUCgDmLne6tccTL379SLKv15iK";
describe('Stealth library', function() {
it('imports a public component', function() {
var Q = scanKeyPubBytes;
var pubKey = Stealth.importPublic(Q);
expect(pubKey.toBytes()).toEqual(Q)
});
it('performs curvedh and stealth formatting', function() {
var e = scanKey.d;
var decKey = spendKey.pub;
Stealth.stealthDH(e, decKey);
});
it('formats a stealth address in base58', function() {
var spendPubKeys = [spendKeyPubBytes];
var formatted = Stealth.formatAddress(scanKeyPubBytes, spendPubKeys);
expect(formatted).toEqual(testAddress);
});
it('parses a stealth address into its forming parts', function() {
var stealthAddress = testAddress;
var parsed = Stealth.parseAddress(stealthAddress);
expect(parsed.options.reuseScan).toEqual(0);
expect(parsed.scanKey).toEqual(scanKeyPubBytes);
expect(parsed.spendKeys).toEqual([spendKeyPubBytes]);
expect(parsed.sigs).toEqual(1);
expect(parsed.prefix).toEqual([0]);
});
it('generates a key and related address to send to for a stealth address', function() {
var scanBytes = scanKeyPubBytes;
var spendBytes = spendKeyPubBytes;
// remove randomness from the test, this changes how ECKey.makeRandom and crypto browserify rng works
// fixes issues with older ff in tests
if (typeof window !== 'undefined') {
var prevMakeRandom = window.crypto.getRandomValues;
window.crypto.getRandomValues = function(bytes) { bytes[0] = 255; };
}
var res = Stealth.initiateStealth(scanBytes, spendBytes, undefined, ephemKeyBytes);
var address = res[0];
var ephemKey = res[1];
expect(ephemKey).toEqual(ephemKeyPubBytes)
expect(address.toString()).toEqual("1Gvq8pSTRocNLDyf858o4PL3yhZm5qQDgB")
// try with no ephemKeyBytes so it will be generated (normal case)
res = Stealth.initiateStealth(scanBytes, spendBytes);
expect(res.length).toBe(3);
// restore the original getRandom
if (typeof window !== 'undefined') {
window.crypto.getRandomValues = prevMakeRandom;
}
});
it('uncovers the stealth secret', function() {
var secret = "34053245118207293422371795779243906008263774607703744573712190846512548332868";
var c = Stealth.uncoverStealth(scanKeyBytes, ephemKeyPubBytes);
expect(c.toString()).toEqual(secret.toString());
});
it('generates an address for receiving for a spend key with the given ephemkey', function() {
var scanSecret = scanKeyBytes;
var ephemBytes = ephemKeyPubBytes;
var spendBytes = spendKeyPubBytes;
var keyBytes = Stealth.uncoverPublic(scanSecret, ephemBytes, spendBytes);
expect(Array.prototype.slice.call(keyBytes, 0)).toEqual([3, 5, 246, 185, 154, 68, 162, 189, 236, 139, 72, 79, 252, 238, 86, 28, 249, 160, 195, 183, 234, 146, 234, 142, 99, 52, 230, 251, 196, 241, 193, 120, 153]);
var keyHash = Bitcoin.crypto.hash160(keyBytes);
var address = new Bitcoin.Address(keyHash, Bitcoin.networks.bitcoin.pubKeyHash);
expect(address.toString()).toBe("1Gvq8pSTRocNLDyf858o4PL3yhZm5qQDgB");
});
it('derives a private key from spend key and shared secret', function() {
var privKey = Stealth.uncoverPrivate(scanKeyBytes, ephemKeyPubBytes, spendKeyBytes);
var keyBytes = privKey.pub.toBytes();
var keyHash = Bitcoin.crypto.hash160(new Buffer(keyBytes));
var address = new Bitcoin.Address(keyHash, Bitcoin.networks.bitcoin.pubKeyHash);
expect(address.toString()).toBe("1Gvq8pSTRocNLDyf858o4PL3yhZm5qQDgB");
});
it('derives public key from spend key and shared secret', function() {
var spendKey = Bitcoin.ECPubKey.fromBytes(spendKeyPubBytes);
var c = new BigInteger("10000");
var keyBytes = Stealth.derivePublicKey(spendKey, c);
expect(bufToArray(keyBytes)).toEqual([3, 173, 36, 66, 71, 110, 69, 203, 135, 107, 57, 44, 117, 28, 232, 195, 123, 20, 36, 239, 18, 50, 107, 196, 154, 84, 37, 176, 43, 123, 246, 179, 204]);
});
it('derives a bitcoin address from spendkey and shared secret', function() {
var spendKey = Bitcoin.ECPubKey.fromBytes(spendKeyPubBytes);
var c = new BigInteger("1000");
var address = Stealth.deriveAddress(spendKey, c);
expect(address.toString()).toBe("1EKg16C2fVCDK8GGzABsSbVVAeL1z7yrAh");
});
it('builds the stealth nonce output', function() {
var nonce = 20;
var ephemBytes = ephemKeyPubBytes;
var script = Stealth.buildNonceScript(ephemBytes, nonce)
//expect(stealthOut.value).toBe(0);
expect(script.buffer.length).toBe(2+1+4+33);
expect(script.buffer[2]).toBe(Stealth.nonceVersion);
// TODO: is this correct or should be [0,0,0,20]?
expect(bufToArray(script.buffer.slice(3,7))).toEqual([20,0,0,0]);
expect(bufToArray(script.buffer.slice(7,40))).toEqual(ephemBytes);
});
it('checks prefix against the given array', function() {
// prefix with size 0
var stealthPrefix = [0, 255, 1, 2, 3];
var outHash = [255,8,9,7,12,54,67];
var res1 = Stealth.checkPrefix(outHash, stealthPrefix);
// prefix with size 8
stealthPrefix = [8, 255, 1, 2, 3];
outHash = [255,1,2,3,12,54,67];
var res2 = Stealth.checkPrefix(outHash, stealthPrefix);
// prefix with size 13
stealthPrefix = [13, 255, 255, 2, 3];
outHash = [255,248,2,3,12,54,67];
var res3 = Stealth.checkPrefix(outHash, stealthPrefix);
// prefix with size 13, last bit fail
stealthPrefix = [13, 255, 255, 2, 3];
outHash = [255,240,2,3,12,54,67];
var res4 = Stealth.checkPrefix(outHash, stealthPrefix);
// prefix with size 8, not equal
stealthPrefix = [8, 255, 1, 2, 3];
outHash = [254,255,2,3,12,54,12];
var res5 = Stealth.checkPrefix(outHash, stealthPrefix);
expect(res1).toBe(true);
expect(res2).toBe(true);
expect(res3).toBe(true);
expect(res4).toBe(false);
expect(res5).toBe(false);
});
it('adds stealth output to the given transaction and return destination address', function() {
var newTx = new Bitcoin.Transaction();
var recipient = Stealth.addStealth(testAddress, newTx, undefined, undefined, ephemKeyBytes, 1);
expect(recipient.address.toString()).toBe("1Gvq8pSTRocNLDyf858o4PL3yhZm5qQDgB");
expect(newTx.outs.length).toBe(1);
var outBuffer = newTx.outs[0].script.chunks[1];
expect(newTx.outs[0].script.chunks[0]).toBe(Bitcoin.opcodes.OP_RETURN); // OP_RETURN
expect(outBuffer[0]).toBe(Stealth.nonceVersion);
expect(Array.prototype.slice.call(outBuffer, 1, 5)).toEqual([1,0,0,0]);
expect(newTx.outs[0].script.buffer)
});
});