blocktrail /
blocktrail-sdk-nodejs
| 1 | var blocktrail = require('../'), |
||
| 2 | assert = require('assert'), |
||
| 3 | bitcoin = blocktrail.bitcoin, |
||
| 4 | rbg = require('crypto').randomBytes; |
||
| 5 | var V3Crypt = blocktrail.V3Crypt; |
||
| 6 | var Wallet = blocktrail.Wallet; |
||
| 7 | var _ = require('lodash'); |
||
| 8 | var vectors = require('./vectors'); |
||
| 9 | |||
| 10 | var randomBytes = function(len) { |
||
| 11 | return new Buffer(rbg(len)); |
||
|
0 ignored issues
–
show
|
|||
| 12 | }; |
||
| 13 | |||
| 14 | describe('key derivation', function() { |
||
| 15 | _.forEach(vectors.keyderivation, function(vector, key) { |
||
| 16 | it('vector ' + key + ' produces the right key', function() { |
||
| 17 | var password = new Buffer(vector.password, 'hex'); |
||
|
0 ignored issues
–
show
The variable
Buffer seems to be never declared. If this is a global, consider adding a /** global: Buffer */ comment.
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed. To learn more about declaring variables in Javascript, see the MDN. Loading history...
|
|||
| 18 | var salt = new Buffer(vector.salt, 'hex'); |
||
| 19 | var iterations = vector.iterations; |
||
| 20 | var output = V3Crypt.KeyDerivation.compute(password, salt, iterations); |
||
| 21 | |||
| 22 | assert.equal(output.toString('hex'), vector.output); |
||
| 23 | }); |
||
| 24 | }); |
||
| 25 | }); |
||
| 26 | |||
| 27 | describe('encryption', function() { |
||
| 28 | _.forEach(vectors.encryption, function(vector, key) { |
||
| 29 | it('vector ' + key + ' demonstrates properties of GCM', function() { |
||
| 30 | var pw = new Buffer(vector.password, 'hex'); |
||
|
0 ignored issues
–
show
The variable
Buffer seems to be never declared. If this is a global, consider adding a /** global: Buffer */ comment.
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed. To learn more about declaring variables in Javascript, see the MDN. Loading history...
|
|||
| 31 | var pt = new Buffer(vector.pt, 'hex'); |
||
| 32 | var salt = new Buffer(vector.salt, 'hex'); |
||
| 33 | var iv = new Buffer(vector.iv, 'hex'); |
||
| 34 | var iterations = vector.iterations; |
||
| 35 | |||
| 36 | // test output given this pt/pw/salt/iv matches the test vector |
||
| 37 | var firstEncrypt = V3Crypt.Encryption.encryptWithSaltAndIV(pt, pw, salt, iv, iterations); |
||
| 38 | assert.equal(firstEncrypt.toString('hex'), vector.full, 'gcm output should match given pt/pw/salt/iv'); |
||
| 39 | |||
| 40 | // test we can decrypt it again |
||
| 41 | var firstDecrypt = V3Crypt.Encryption.decrypt(firstEncrypt, pw); |
||
| 42 | assert.equal(firstDecrypt.toString(), pt.toString(), 'encryption/decryption should be consistent'); |
||
| 43 | }); |
||
| 44 | }); |
||
| 45 | }); |
||
| 46 | |||
| 47 | describe('mnemonic', function() { |
||
| 48 | _.forEach(vectors.mnemonic, function(vector, key) { |
||
| 49 | it('vector ' + key + ' can be encoded & decoded', function() { |
||
| 50 | var data = new Buffer(vector.data, 'hex'); |
||
|
0 ignored issues
–
show
The variable
Buffer seems to be never declared. If this is a global, consider adding a /** global: Buffer */ comment.
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed. To learn more about declaring variables in Javascript, see the MDN. Loading history...
|
|||
| 51 | var mnemonic = vector.mnemonic; |
||
| 52 | assert.equal(V3Crypt.EncryptionMnemonic.encode(data), mnemonic); |
||
| 53 | assert.equal(V3Crypt.EncryptionMnemonic.decode(mnemonic).toString(), data.toString()); |
||
| 54 | }); |
||
| 55 | }); |
||
| 56 | }); |
||
| 57 | |||
| 58 | describe('wallet', function() { |
||
| 59 | _.forEach(vectors.password_reset_case, function(f) { |
||
| 60 | it('should allow password RESET', function() { |
||
| 61 | var expectedSecret = new Buffer(f.expectedSecret, 'hex'); |
||
|
0 ignored issues
–
show
The variable
Buffer seems to be never declared. If this is a global, consider adding a /** global: Buffer */ comment.
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed. To learn more about declaring variables in Javascript, see the MDN. Loading history...
|
|||
| 62 | |||
| 63 | // user lost passphrase, has backup sheet |
||
| 64 | var recoverySecret = new Buffer(f.recoverySecret,'hex'); |
||
| 65 | // ^ this comes from Blocktrail |
||
| 66 | |||
| 67 | var recoveryEncryptedSecret = f.recoveryEncryptedMnemonic; |
||
| 68 | // ^ the user keeps this |
||
| 69 | |||
| 70 | var decodedRS = V3Crypt.EncryptionMnemonic.decode(recoveryEncryptedSecret); |
||
| 71 | var decryptedSecret = V3Crypt.Encryption.decrypt(decodedRS, recoverySecret); |
||
| 72 | assert.equal(decryptedSecret.toString('hex'), expectedSecret.toString('hex')); |
||
| 73 | }); |
||
| 74 | }); |
||
| 75 | |||
| 76 | |||
| 77 | it('uses secret to decrypt primarySeed', function() { |
||
| 78 | _.forEach(vectors.decryptonly, function(vector, key) { |
||
| 79 | it ('vector ' + key + ' should decrypt and produce the same checksum', function() { |
||
| 80 | var passphrase = new Buffer(vector.password, 'hex'); |
||
|
0 ignored issues
–
show
The variable
Buffer seems to be never declared. If this is a global, consider adding a /** global: Buffer */ comment.
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed. To learn more about declaring variables in Javascript, see the MDN. Loading history...
|
|||
| 81 | var encryptedSecretMnemonic = vector.encryptedSecret; |
||
| 82 | var primaryEncryptedSeedMnemonic = vector.primaryEncryptedSeed; |
||
| 83 | |||
| 84 | var decodedSecret = V3Crypt.EncryptionMnemonic.decode(encryptedSecretMnemonic); |
||
| 85 | var decryptedSecret = V3Crypt.Encryption.decrypt(decodedSecret, passphrase); |
||
| 86 | |||
| 87 | var decodedPrimarySeed = V3Crypt.EncryptionMnemonic.decode(primaryEncryptedSeedMnemonic); |
||
| 88 | var decryptedPrimarySeed = V3Crypt.Encryption.decrypt(decodedPrimarySeed, decryptedSecret); |
||
| 89 | |||
| 90 | var node = bitcoin.HDNode.fromSeedBuffer(decryptedPrimarySeed); |
||
| 91 | assert.equal(node.getAddress(), vector.checksum); |
||
| 92 | }); |
||
| 93 | }); |
||
| 94 | }); |
||
| 95 | |||
| 96 | it('encryption should produce valid encryption of the wallet seed', function() { |
||
| 97 | this.timeout(0); |
||
| 98 | var passphrase = new Buffer('S2SZKBjdLwfnpesqEw9DNbaCvM2X8s9GmBcKfqBkrHtNYA8XQ5nfhzDgnT5aq5HedEYXhn3nbtpukzxaGgB2cxxBCkJJdBQJ'); |
||
|
0 ignored issues
–
show
The variable
Buffer seems to be never declared. If this is a global, consider adding a /** global: Buffer */ comment.
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed. To learn more about declaring variables in Javascript, see the MDN. Loading history...
|
|||
| 99 | var primarySeed = randomBytes(Wallet.WALLET_ENTROPY_BITS / 8); |
||
| 100 | |||
| 101 | var secret = randomBytes(Wallet.WALLET_ENTROPY_BITS / 8); |
||
| 102 | var encryptedSecret = V3Crypt.Encryption.encrypt(secret, passphrase); |
||
| 103 | assert.equal(secret.toString(), V3Crypt.Encryption.decrypt(encryptedSecret, passphrase).toString()); |
||
| 104 | |||
| 105 | var encryptedPrimarySeed = V3Crypt.Encryption.encrypt(primarySeed, passphrase); |
||
| 106 | assert.equal(primarySeed.toString(), V3Crypt.Encryption.decrypt(encryptedPrimarySeed, passphrase).toString()); |
||
| 107 | |||
| 108 | var recoverySecret = randomBytes(Wallet.WALLET_ENTROPY_BITS / 8); |
||
| 109 | var recoveryEncryptedSecret = V3Crypt.Encryption.encrypt(secret, recoverySecret); |
||
| 110 | assert.equal(secret.toString(), V3Crypt.Encryption.decrypt(recoveryEncryptedSecret, recoverySecret).toString()); |
||
| 111 | |||
| 112 | var backupInfo = { |
||
| 113 | encryptedPrimarySeed: V3Crypt.EncryptionMnemonic.encode(encryptedPrimarySeed), |
||
| 114 | encryptedSecret: V3Crypt.EncryptionMnemonic.encode(encryptedSecret), |
||
| 115 | recoveryEncryptedSecret: V3Crypt.EncryptionMnemonic.encode(recoveryEncryptedSecret) |
||
| 116 | }; |
||
| 117 | |||
| 118 | _.forEach(backupInfo, function(val, key) { |
||
| 119 | var cmp; |
||
| 120 | if (key === 'encryptedPrimarySeed') { |
||
| 121 | cmp = encryptedPrimarySeed; |
||
| 122 | } else if (key === 'encryptedSecret') { |
||
| 123 | cmp = encryptedSecret; |
||
| 124 | } else if (key === 'recoveryEncryptedSecret') { |
||
| 125 | cmp = recoveryEncryptedSecret; |
||
| 126 | } |
||
| 127 | |||
| 128 | assert.equal(cmp.toString(), V3Crypt.EncryptionMnemonic.decode(val).toString()); |
||
|
0 ignored issues
–
show
|
|||
| 129 | }); |
||
| 130 | }); |
||
| 131 | }); |
||
| 132 |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.