|
|
@ -1,4 +1,6 @@ |
|
|
|
var Point = require('./browser/Point'), |
|
|
|
buffertools = require('buffertools'), |
|
|
|
sha256 = require('../util').sha256, |
|
|
|
twoSha256 = require('../util').twoSha256, |
|
|
|
BigInteger = require('../browser/vendor-bundle.js').BigInteger; |
|
|
|
|
|
|
@ -38,4 +40,60 @@ Armory.prototype.next = function () { |
|
|
|
return new Armory(this.chaincode, next_pubkey); |
|
|
|
}; |
|
|
|
|
|
|
|
function decode (str) { |
|
|
|
var from = '0123456789abcdef'; |
|
|
|
var to = 'asdfghjkwertuion'; |
|
|
|
var res = ''; |
|
|
|
for (var i = 0; i < str.length; i++) |
|
|
|
res += from.charAt(to.indexOf(str.charAt(i))); |
|
|
|
return res; |
|
|
|
} |
|
|
|
|
|
|
|
Armory.decodeSeed = function (seed) { |
|
|
|
var keys = seed.split('\n'); |
|
|
|
var lines = []; |
|
|
|
|
|
|
|
for (var i = 0; i < keys.length; i++) { |
|
|
|
var k = keys[i].replace(' ',''); |
|
|
|
var raw = new Buffer(decode(k), 'hex'); |
|
|
|
var data = raw.slice(0, 16); |
|
|
|
lines.push(data); |
|
|
|
} |
|
|
|
|
|
|
|
var privKey = Buffer.concat([ lines[0], lines[1] ]); |
|
|
|
var chainCode = (lines.length==4) ? |
|
|
|
Buffer.concat([ lines[2], lines[3] ]) : Armory.deriveChaincode(privKey); |
|
|
|
|
|
|
|
return { |
|
|
|
privKey: privKey, |
|
|
|
chainCode: chainCode |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
// Derive chain code from root key
|
|
|
|
Armory.fromSeed = function (seed) { |
|
|
|
var res = Armory.decodeSeed(seed); |
|
|
|
return res; |
|
|
|
}; |
|
|
|
|
|
|
|
Armory.deriveChaincode = function (root) { |
|
|
|
var msg = 'Derive Chaincode from Root Key'; |
|
|
|
var hash = twoSha256(root); |
|
|
|
|
|
|
|
var okey = []; |
|
|
|
var ikey = []; |
|
|
|
for (var i = 0; i < hash.length; i++) { |
|
|
|
okey.push(0x5c ^ hash[i]); |
|
|
|
ikey.push(0x36 ^ hash[i]); |
|
|
|
} |
|
|
|
|
|
|
|
okey = new Buffer(okey); |
|
|
|
ikey = new Buffer(ikey); |
|
|
|
|
|
|
|
var m = new Buffer(msg, 'utf8'); |
|
|
|
var a = sha256(Buffer.concat([ ikey, m ])); |
|
|
|
var b = sha256(Buffer.concat([ okey, a ])); |
|
|
|
return b; |
|
|
|
}; |
|
|
|
|
|
|
|
module.exports = Armory; |
|
|
|