@@ -3,7 +3,18 @@ const should = require('should');
3
3
const testutils = require ( './testutils' ) ;
4
4
const admin = require ( '../app/admin.js' ) ;
5
5
const WalletKey = require ( '../app/models/walletkey' ) ;
6
+ const MasterKey = require ( '../app/models/masterkey' ) ;
6
7
const utils = require ( '../app/utils.js' ) ;
8
+ const Promise = require ( 'bluebird' ) ;
9
+ const co = Promise . coroutine ;
10
+ let originalVerifcationPub = process . config . verificationPub ;
11
+
12
+ const testVerificationPub = '1GS6JPCUpyFZLrE5bbJBcpeg1EdqW63nHd' ;
13
+ const xpub = 'xpub661MyMwAqRbcGGPNi42htgwXoLuPjEtdRSRUm65GWRPAb31WPRRkxzL18TGrpU2sirNjXAHFjAEmiav9kmVfY83dTfxh3DVVwNcM9JNVebh' ;
14
+ const xpubSig = 'IJB1Ubed4LNVvHfbH3s7i9duWmVi+98rCcGpB/t/V9xkY+VLl+YEv3w/g/79QgHYFv4D/nm2SGrT+FaOL1PomTU' ;
15
+ const xlmPub = 'GCM67ICQVOYN7GYYHMMGVPQ7NDCLNVBM4R74IIQTG65H4DM3KGEDMW3S' ;
16
+ const xlmSig = 'IBdvAO2063rsqo6zyhcoq4qGWMZFFQXCbcP4X7/vnbuyQO7VF2FHHr/M85CQb/yBfPgZRy5VsmDmrW3Qa6OXk6w' ;
17
+ const badSig = 'IBadAO2063rsqo6zyhcoq4qGWMZFFQXCbcP4X7/vnbuyQO7VF2FHHr/M85CQb/yBfPgZRy5VsmDmrW3Qa6OXk6w' ;
7
18
8
19
describe ( 'Offline Admin Tool' , function ( ) {
9
20
before ( function ( ) {
@@ -12,15 +23,18 @@ describe('Offline Admin Tool', function() {
12
23
13
24
after ( function ( ) {
14
25
testutils . mongoose . connection . close ( ) ;
26
+ process . config . verificationPub = originalVerifcationPub ;
15
27
} ) ;
16
28
17
29
describe ( 'Xpub validation' , function ( ) {
30
+
18
31
// Xpub importing was tested with a local file with contents: (w/o line breaks)
19
32
// xpub6AHA9hZDN11k2ijHMeS5QqHx2KP9aMBRhTDqANMnwVtdyw2TDYRmF8PjpvwUFcL1Et8Hj59S3gTSMcUQ5gAqTz3Wd8EsMTmF3DChhqPQBnU,
20
33
// xpub69pXXVvsBtZHWE1wgoix7xRAnbp1r6tR1kTK9cKvCd4QgYh4JBSLBmLA65Kg7rCMwGYrNHKFKxZDtjRkU4Ex2ozMYGfk14EyotJ5xjf2Goy,
21
34
// xpub6AU2iYgKUY8FvUc2Nz2fcWpKU1HJTeYTbzv4MZLGPBw2YnhoZPkkUo54fvqZyVtxtszMdyksF8k3iqMcoegyvj72xKZCmuCjDWneXjjztLN
22
35
// and successfully saved to local Mongo instance
23
36
describe ( 'failure' , function ( ) {
37
+ process . config . verificationPub = null ;
24
38
it ( 'should fail if length is not 111' , function ( ) {
25
39
const SHORT_XPUB = { path : 'm/0\'' , pub : 'xpub1234567890' } ;
26
40
@@ -42,14 +56,15 @@ describe('Offline Admin Tool', function() {
42
56
43
57
describe ( 'success' , function ( ) {
44
58
it ( 'should succeed with a valid key' , function ( ) {
59
+ process . config . verificationPub = null ;
45
60
const GOOD_XPUB = { path : 'm/0\'' , pub : 'xpub6B7XuUcPQ9MeszNzaTTGtni9W79MmFnHa7FUe7Hrbv3pefnaDFCHtJWaWdg1FVbocHhivnCRTCbHTjDrMBEyAGDJHGyqCnLhtEWP2rtb1sL' } ;
46
-
47
61
admin . validateKey ( GOOD_XPUB , 'xpub' ) . should . equal ( true ) ;
48
62
} ) ;
49
63
} ) ;
50
64
} ) ;
51
65
52
66
describe ( 'BIP32 child key derivation' , function ( ) {
67
+ process . config . verificationPub = null ;
53
68
// from https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki test vector 2 chain m
54
69
const MASTER_XPUB = 'xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB' ;
55
70
@@ -80,46 +95,123 @@ describe('Offline Admin Tool', function() {
80
95
} ) ;
81
96
82
97
describe ( 'Stellar key derivation' , function ( ) {
83
- // from test 3 at https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0005.md#test-cases
84
- const MASTER_SEED = '937ae91f6ab6f12461d9936dfc1375ea5312d097f3f1eb6fed6a82fbe38c85824da8704389831482db0433e5f6c6c9700ff1946aa75ad8cc2654d6e40f567866'
85
-
86
- describe ( 'failure' , function ( ) {
87
- it ( 'should fail with an invalid master seed' , function ( ) {
88
- const BAD_SEED = '-thisisabadseed' ;
89
-
90
- ( function ( ) { utils . deriveChildKey ( BAD_SEED , "m/148'" , 'xlm' ) } ) . should . throw ( Error ) ;
91
- } ) ;
92
-
93
- it ( 'should fail with an invalid derivation path' , function ( ) {
94
- ( function ( ) { utils . deriveChildKey ( MASTER_SEED , 'derivation path' , 'xlm' ) } ) . should . throw ( Error ) ;
95
- } ) ;
96
- } ) ;
97
-
98
- describe ( 'success' , function ( ) {
99
- // test 3 from https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0005.md#test-cases
100
- it ( "should find m/44'/148'/0' of test vector 3" , function ( ) {
101
- const pub = 'GC3MMSXBWHL6CPOAVERSJITX7BH76YU252WGLUOM5CJX3E7UCYZBTPJQ' ;
102
- const priv = 'SAEWIVK3VLNEJ3WEJRZXQGDAS5NVG2BYSYDFRSH4GKVTS5RXNVED5AX7' ;
103
-
104
- const publicKey = utils . deriveChildKey ( MASTER_SEED , "m/44'/148'/0'" , 'xlm' , true ) ;
105
- publicKey . should . equal ( pub ) ;
106
- const secret = utils . deriveChildKey ( MASTER_SEED , "m/44'/148'/0'" , 'xlm' , false ) ;
107
- secret . should . equal ( priv ) ;
108
- } ) ;
109
-
110
- // test 3 from https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0005.md#test-cases
111
- it ( "should find m/44'/148'/6' of test vector 3" , function ( ) {
112
- const pub = 'GCUDW6ZF5SCGCMS3QUTELZ6LSAH6IVVXNRPRLAUNJ2XYLCA7KH7ZCVQS' ;
113
- const priv = 'SBSHUZQNC45IAIRSAHMWJEJ35RY7YNW6SMOEBZHTMMG64NKV7Y52ZEO2' ;
114
-
115
- const publicKey = utils . deriveChildKey ( MASTER_SEED , "m/44'/148'/6'" , 'xlm' , true ) ;
116
- publicKey . should . equal ( pub ) ;
117
- const secret = utils . deriveChildKey ( MASTER_SEED , "m/44'/148'/6'" , 'xlm' , false ) ;
118
- secret . should . equal ( priv ) ;
119
- } ) ;
120
- } )
98
+ process . config . verificationPub = null ;
99
+ // from test 3 at https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0005.md#test-cases
100
+ const MASTER_SEED = '937ae91f6ab6f12461d9936dfc1375ea5312d097f3f1eb6fed6a82fbe38c85824da8704389831482db0433e5f6c6c9700ff1946aa75ad8cc2654d6e40f567866'
101
+
102
+ describe ( 'failure' , function ( ) {
103
+ it ( 'should fail with an invalid master seed' , function ( ) {
104
+ const BAD_SEED = '-thisisabadseed' ;
105
+ ( function ( ) { utils . deriveChildKey ( BAD_SEED , "m/148'" , 'xlm' ) } ) . should . throw ( Error ) ;
106
+ } ) ;
107
+
108
+ it ( 'should fail with an invalid derivation path' , function ( ) {
109
+ ( function ( ) { utils . deriveChildKey ( MASTER_SEED , 'derivation path' , 'xlm' ) } ) . should . throw ( Error ) ;
110
+ } ) ;
111
+ } ) ;
112
+ describe ( 'success' , function ( ) {
113
+ // test 3 from https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0005.md#test-cases
114
+ it ( "should find m/44'/148'/0' of test vector 3" , function ( ) {
115
+ const pub = 'GC3MMSXBWHL6CPOAVERSJITX7BH76YU252WGLUOM5CJX3E7UCYZBTPJQ' ;
116
+ const priv = 'SAEWIVK3VLNEJ3WEJRZXQGDAS5NVG2BYSYDFRSH4GKVTS5RXNVED5AX7' ;
117
+
118
+ const publicKey = utils . deriveChildKey ( MASTER_SEED , "m/44'/148'/0'" , 'xlm' , true ) ;
119
+ publicKey . should . equal ( pub ) ;
120
+ const secret = utils . deriveChildKey ( MASTER_SEED , "m/44'/148'/0'" , 'xlm' , false ) ;
121
+ secret . should . equal ( priv ) ;
122
+ } ) ;
123
+
124
+ // test 3 from https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0005.md#test-cases
125
+ it ( "should find m/44'/148'/6' of test vector 3" , function ( ) {
126
+ const pub = 'GCUDW6ZF5SCGCMS3QUTELZ6LSAH6IVVXNRPRLAUNJ2XYLCA7KH7ZCVQS' ;
127
+ const priv = 'SBSHUZQNC45IAIRSAHMWJEJ35RY7YNW6SMOEBZHTMMG64NKV7Y52ZEO2' ;
128
+ const publicKey = utils . deriveChildKey ( MASTER_SEED , "m/44'/148'/6'" , 'xlm' , true ) ;
129
+ publicKey . should . equal ( pub ) ;
130
+ const secret = utils . deriveChildKey ( MASTER_SEED , "m/44'/148'/6'" , 'xlm' , false ) ;
131
+ secret . should . equal ( priv ) ;
132
+ } ) ;
133
+ } )
121
134
} ) ;
122
135
136
+ describe ( 'Key signature verification' , function ( ) {
137
+ describe ( 'failure' , function ( ) {
138
+ it ( 'should fail xpub validation with a bad signature' , function ( ) {
139
+ process . config . verificationPub = testVerificationPub ;
140
+ const key = {
141
+ pub : xpub ,
142
+ signature : badSig
143
+ } ;
144
+ const valid = admin . validateKey ( key , 'xpub' ) ;
145
+ valid . should . equal ( false ) ;
146
+ } ) ;
147
+
148
+ it ( 'should fail xlm validation with a bad signature' , function ( ) {
149
+ process . config . verificationPub = testVerificationPub ;
150
+ const key = {
151
+ pub : xlmPub ,
152
+ signature : badSig
153
+ } ;
154
+ const valid = admin . validateKey ( key , 'xlm' ) ;
155
+ valid . should . equal ( false ) ;
156
+ } ) ;
157
+
158
+ it ( 'should fail xpub validation with no signature' , function ( ) {
159
+ process . config . verificationPub = testVerificationPub ;
160
+ const key = {
161
+ pub : xpub
162
+ } ;
163
+ const valid = admin . validateKey ( key , 'xpub' ) ;
164
+ valid . should . equal ( false ) ;
165
+ } ) ;
166
+
167
+ it ( 'should fail xlm validation with no signature' , function ( ) {
168
+ process . config . verificationPub = testVerificationPub ;
169
+ const key = {
170
+ pub : xlmPub
171
+ } ;
172
+ const valid = admin . validateKey ( key , 'xlm' ) ;
173
+ valid . should . equal ( false ) ;
174
+ } ) ;
175
+ } ) ;
176
+
177
+ describe ( 'success' , function ( ) {
178
+ it ( 'should validate xpub with a good signature' , function ( ) {
179
+ process . config . verificationPub = testVerificationPub ;
180
+ const key = {
181
+ pub : xpub ,
182
+ signature : xpubSig
183
+ } ;
184
+ const valid = admin . validateKey ( key , 'xpub' ) ;
185
+ valid . should . equal ( true ) ;
186
+ } ) ;
187
+
188
+ it ( 'should validate xlm with a good signature' , function ( ) {
189
+ process . config . verificationPub = testVerificationPub ;
190
+ const key = {
191
+ pub : xlmPub ,
192
+ signature : xlmSig
193
+ } ;
194
+ const valid = admin . validateKey ( key , 'xlm' ) ;
195
+ valid . should . equal ( true ) ;
196
+ } ) ;
197
+ } ) ;
198
+ } ) ;
199
+
200
+ describe ( 'Save a key with a signature' , co ( function * ( ) {
201
+ it ( 'should successfully save a key with a signature to the database' , co ( function * ( ) {
202
+ process . config . verificationPub = testVerificationPub ;
203
+ const key = {
204
+ pub : xpub ,
205
+ signature : xpubSig ,
206
+ path : '0'
207
+ } ;
208
+ const keyList = [ key ] ;
209
+ yield admin . saveKeys ( keyList , 'xpub' ) ;
210
+ const foundKey = yield MasterKey . findOne ( { pub : xpub } ) ;
211
+ foundKey . should . have . property ( 'signature' ) ;
212
+ } ) ) ;
213
+ } ) ) ;
214
+
123
215
describe ( 'Verification' , function ( ) {
124
216
before ( function ( ) {
125
217
const key = new WalletKey ( {
0 commit comments