copay/js/models/HDPath.js

117 lines
3.1 KiB
JavaScript
Raw Normal View History

2014-05-29 13:19:14 -07:00
'use strict';
2014-09-02 18:27:47 -07:00
// 90.2% typed (by google's closure-compiler account)
2014-05-29 13:19:14 -07:00
2014-09-02 18:27:47 -07:00
var preconditions = require('preconditions').singleton();
2014-10-25 15:57:12 -07:00
var _ = require('lodash');
2014-05-29 13:19:14 -07:00
2014-09-02 18:27:47 -07:00
/**
* @namespace
2014-09-08 11:42:55 -07:00
* @desc
2014-09-02 18:27:47 -07:00
* HDPath contains helper functions to handle BIP32 branches as
* Copay uses them.
2014-05-29 13:19:14 -07:00
* Based on https://github.com/maraoz/bips/blob/master/bip-NNNN.mediawiki
2014-09-02 18:27:47 -07:00
* <pre>
* m / purpose' / copayerIndex / change:boolean / addressIndex
* </pre>
2014-05-29 13:19:14 -07:00
*/
2014-09-02 18:27:47 -07:00
var HDPath = {};
2014-05-29 13:19:14 -07:00
2014-09-02 18:27:47 -07:00
/**
* @desc Copay's BIP45 purpose code
* @const
* @type number
*/
HDPath.PURPOSE = 45;
/**
* @desc Maximum number for non-hardened values (BIP32)
* @const
* @type number
*/
HDPath.MAX_NON_HARDENED = 0x80000000 - 1;
/**
* @desc Shared Index: used for creating addresses for no particular purpose
* @const
* @type number
*/
HDPath.SHARED_INDEX = HDPath.MAX_NON_HARDENED - 0;
/**
* @desc ???
* @const
* @type number
*/
HDPath.ID_INDEX = HDPath.MAX_NON_HARDENED - 1;
2014-05-29 13:19:14 -07:00
2014-09-02 18:27:47 -07:00
/**
* @desc BIP45 prefix for COPAY
* @const
* @type string
*/
HDPath.BIP45_PUBLIC_PREFIX = 'm/' + HDPath.PURPOSE + '\'';
2014-05-29 13:19:14 -07:00
2014-09-02 18:27:47 -07:00
/**
* @desc Retrieve a string to be used with bitcore representing a Copay branch
* @param {number} addressIndex - the last value of the HD derivation
* @param {boolean} isChange - whether this is a change address or a receive
* @param {number} copayerIndex - the index of the copayer in the pubkeyring
* @return {string} - the path for the HD derivation
*/
2014-07-29 07:23:58 -07:00
HDPath.Branch = function(addressIndex, isChange, copayerIndex) {
2014-09-02 18:27:47 -07:00
preconditions.checkArgument(_.isNumber(addressIndex));
preconditions.checkArgument(_.isBoolean(isChange));
2014-05-30 07:07:34 -07:00
var ret = 'm/' +
2014-09-02 18:27:47 -07:00
(typeof copayerIndex !== 'undefined' ? copayerIndex : HDPath.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-09-02 18:27:47 -07:00
/**
* @desc ???
* @param {number} addressIndex - the last value of the HD derivation
* @param {boolean} isChange - whether this is a change address or a receive
* @param {number} copayerIndex - the index of the copayer in the pubkeyring
* @return {string} - the path for the HD derivation
*/
2014-07-29 07:23:58 -07:00
HDPath.FullBranch = function(addressIndex, isChange, copayerIndex) {
2014-09-02 18:27:47 -07:00
preconditions.checkArgument(_.isNumber(addressIndex));
preconditions.checkArgument(_.isBoolean(isChange));
2014-07-29 07:23:58 -07:00
var sub = HDPath.Branch(addressIndex, isChange, copayerIndex);
2014-05-29 13:19:14 -07:00
sub = sub.substring(2);
2014-09-02 18:27:47 -07:00
return HDPath.BIP45_PUBLIC_PREFIX + '/' + sub;
2014-05-29 13:19:14 -07:00
};
2014-06-24 09:17:22 -07:00
2014-09-02 18:27:47 -07:00
/**
* @desc
* Decompose a string and retrieve its arguments as if it where a Copay address.
* @param {string} path - the HD path
* @returns {Object} an object with three keys: addressIndex, isChange, and
* copayerIndex
*/
2014-07-30 17:20:08 -07:00
HDPath.indexesForPath = function(path) {
2014-09-02 18:27:47 -07:00
preconditions.checkArgument(_.isString(path));
2014-06-24 09:17:22 -07:00
var s = path.split('/');
return {
isChange: s[3] === '1',
2014-09-02 18:27:47 -07:00
addressIndex: parseInt(s[4], 10),
copayerIndex: parseInt(s[2], 10)
2014-06-24 09:17:22 -07:00
};
};
2014-09-02 18:27:47 -07:00
/**
* @desc The ID for a shared branch
*/
HDPath.IdFullBranch = HDPath.FullBranch(0, false, HDPath.ID_INDEX);
/**
* @desc Partial ID for a shared branch
*/
HDPath.IdBranch = HDPath.Branch(0, false, HDPath.ID_INDEX);
2014-05-29 13:19:14 -07:00
2014-07-29 07:23:58 -07:00
module.exports = HDPath;