copay/js/models/core/HDPath.js

54 lines
1.5 KiB
JavaScript
Raw Normal View History

2014-05-29 13:19:14 -07:00
'use strict';
2014-06-24 10:40:03 -07:00
var preconditions = require('preconditions').singleton();
2014-05-29 13:19:14 -07:00
2014-07-29 07:23:58 -07:00
function HDPath() {}
2014-05-29 13:19:14 -07:00
/*
* Based on https://github.com/maraoz/bips/blob/master/bip-NNNN.mediawiki
2014-07-29 06:14:15 -07:00
* m / purpose' / copayerIndex / change / addressIndex
2014-05-29 13:19:14 -07:00
*/
var PURPOSE = 45;
2014-05-30 06:29:51 -07:00
var MAX_NON_HARDENED = 0x80000000 - 1;
2014-05-29 13:19:14 -07:00
var SHARED_INDEX = MAX_NON_HARDENED - 0;
var ID_INDEX = MAX_NON_HARDENED - 1;
2014-05-30 07:07:34 -07:00
var BIP45_PUBLIC_PREFIX = 'm/' + PURPOSE + '\'';
2014-07-29 07:23:58 -07:00
HDPath.BIP45_PUBLIC_PREFIX = BIP45_PUBLIC_PREFIX;
2014-05-29 13:19:14 -07:00
2014-07-29 07:23:58 -07:00
HDPath.Branch = function(addressIndex, isChange, copayerIndex) {
2014-07-29 06:14:15 -07:00
preconditions.shouldBeNumber(addressIndex);
2014-06-24 10:40:03 -07:00
preconditions.shouldBeBoolean(isChange);
2014-05-30 07:07:34 -07:00
var ret = 'm/' +
2014-07-29 06:14:15 -07:00
(typeof copayerIndex !== 'undefined' ? copayerIndex : SHARED_INDEX) + '/' +
2014-05-30 07:07:34 -07:00
(isChange ? 1 : 0) + '/' +
2014-07-29 06:14:15 -07:00
addressIndex;
2014-05-29 13:19:14 -07:00
return ret;
};
2014-07-29 07:23:58 -07:00
HDPath.FullBranch = function(addressIndex, isChange, copayerIndex) {
var sub = HDPath.Branch(addressIndex, isChange, copayerIndex);
2014-05-29 13:19:14 -07:00
sub = sub.substring(2);
return BIP45_PUBLIC_PREFIX + '/' + sub;
};
2014-06-24 09:17:22 -07:00
2014-07-30 17:20:08 -07:00
HDPath.indexesForPath = function(path) {
2014-06-24 10:40:03 -07:00
preconditions.shouldBeString(path);
2014-06-24 09:17:22 -07:00
var s = path.split('/');
return {
isChange: s[3] === '1',
2014-07-30 17:20:08 -07:00
addressIndex: parseInt(s[4]),
copayerIndex: parseInt(s[2])
2014-06-24 09:17:22 -07:00
};
};
2014-07-29 07:23:58 -07:00
HDPath.IdFullBranch = HDPath.FullBranch(0, false, ID_INDEX);
HDPath.IdBranch = HDPath.Branch(0, false, ID_INDEX);
HDPath.PURPOSE = PURPOSE;
HDPath.MAX_NON_HARDENED = MAX_NON_HARDENED;
HDPath.SHARED_INDEX = SHARED_INDEX;
HDPath.ID_INDEX = ID_INDEX;
2014-05-29 13:19:14 -07:00
2014-07-29 07:23:58 -07:00
module.exports = HDPath;