itns-sidechain/lib/wallet/path.js

312 lines
5.5 KiB
JavaScript
Raw Normal View History

2016-08-24 04:08:40 -07:00
/*!
* path.js - path object for wallets
2017-02-03 22:47:26 -08:00
* Copyright (c) 2014-2017, Christopher Jeffrey (MIT License).
2016-08-24 04:08:40 -07:00
* https://github.com/bcoin-org/bcoin
*/
'use strict';
2017-06-29 20:54:07 -07:00
const assert = require('assert');
2017-11-16 11:43:24 -08:00
const bio = require('bufio');
2017-06-29 20:54:07 -07:00
const Address = require('../primitives/address');
2017-11-16 11:43:24 -08:00
const {encoding} = bio;
2016-08-24 04:08:40 -07:00
/**
* Path
2017-02-03 22:47:26 -08:00
* @alias module:wallet.Path
2016-08-24 04:08:40 -07:00
* @property {String} name - Account name.
* @property {Number} account - Account index.
2016-10-01 18:23:47 -07:00
* @property {Number} branch - Branch index.
2016-08-24 04:08:40 -07:00
* @property {Number} index - Address index.
*/
2017-11-16 20:11:17 -08:00
class Path {
/**
* Create a path.
* @constructor
* @param {Object?} options
*/
2016-08-24 04:08:40 -07:00
2017-11-16 20:11:17 -08:00
constructor(options) {
this.keyType = Path.types.HD;
2016-10-01 12:31:43 -07:00
2017-11-16 20:11:17 -08:00
this.name = null; // Passed in by caller.
this.account = 0;
2017-11-27 12:36:12 -08:00
2018-01-02 20:24:56 -08:00
this.version = 0;
2017-11-16 20:11:17 -08:00
this.branch = -1;
this.index = -1;
2016-08-24 04:08:40 -07:00
2017-11-16 20:11:17 -08:00
this.encrypted = false;
this.data = null;
2016-08-24 04:08:40 -07:00
2017-11-16 20:11:17 -08:00
this.hash = null; // Passed in by caller.
2016-10-02 20:16:22 -07:00
2017-11-16 20:11:17 -08:00
if (options)
this.fromOptions(options);
}
2016-10-02 20:16:22 -07:00
2017-11-16 20:11:17 -08:00
/**
* Instantiate path from options object.
* @private
* @param {Object} options
* @returns {Path}
*/
2016-10-02 20:16:22 -07:00
2017-11-16 20:11:17 -08:00
fromOptions(options) {
this.keyType = options.keyType;
2016-10-02 20:16:22 -07:00
2017-11-16 20:11:17 -08:00
this.name = options.name;
this.account = options.account;
this.branch = options.branch;
this.index = options.index;
2016-10-02 20:16:22 -07:00
2017-11-16 20:11:17 -08:00
this.encrypted = options.encrypted;
this.data = options.data;
2016-10-02 20:16:22 -07:00
2017-11-16 20:11:17 -08:00
this.version = options.version;
this.hash = options.hash;
2016-10-02 20:16:22 -07:00
2017-11-16 20:11:17 -08:00
return this;
}
2016-10-02 20:16:22 -07:00
2017-11-16 20:11:17 -08:00
/**
* Instantiate path from options object.
* @param {Object} options
* @returns {Path}
*/
2016-10-02 20:16:22 -07:00
2017-11-16 20:11:17 -08:00
static fromOptions(options) {
return new this().fromOptions(options);
}
2016-08-24 04:08:40 -07:00
2017-11-16 20:11:17 -08:00
/**
* Clone the path object.
* @returns {Path}
*/
2016-10-01 18:18:02 -07:00
2017-11-16 20:11:17 -08:00
clone() {
const path = new this.constructor();
2016-08-24 04:08:40 -07:00
2017-11-16 20:11:17 -08:00
path.keyType = this.keyType;
2016-08-24 04:08:40 -07:00
2017-11-16 20:11:17 -08:00
path.name = this.name;
path.account = this.account;
path.branch = this.branch;
path.index = this.index;
2016-08-24 04:08:40 -07:00
2017-11-16 20:11:17 -08:00
path.encrypted = this.encrypted;
path.data = this.data;
2016-08-24 04:08:40 -07:00
2017-11-16 20:11:17 -08:00
path.version = this.version;
path.hash = this.hash;
2016-08-24 04:08:40 -07:00
2017-11-16 20:11:17 -08:00
return path;
2016-08-24 04:08:40 -07:00
}
2017-11-16 20:11:17 -08:00
/**
* Inject properties from serialized data.
* @private
* @param {Buffer} data
*/
fromRaw(data) {
const br = bio.read(data);
this.account = br.readU32();
this.keyType = br.readU8();
2018-01-02 20:24:56 -08:00
this.version = br.readU8();
2017-11-27 12:36:12 -08:00
2017-11-16 20:11:17 -08:00
switch (this.keyType) {
case Path.types.HD:
this.branch = br.readU32();
this.index = br.readU32();
break;
case Path.types.KEY:
this.encrypted = br.readU8() === 1;
this.data = br.readVarBytes();
break;
case Path.types.ADDRESS:
// Hash will be passed in by caller.
break;
default:
assert(false);
break;
}
return this;
}
2016-08-24 04:08:40 -07:00
2017-11-16 20:11:17 -08:00
/**
* Instantiate path from serialized data.
* @param {Buffer} data
* @returns {Path}
*/
2017-05-13 00:16:18 -07:00
2017-11-16 20:11:17 -08:00
static fromRaw(data) {
return new this().fromRaw(data);
}
2016-08-24 04:08:40 -07:00
2017-11-16 20:11:17 -08:00
/**
* Calculate serialization size.
* @returns {Number}
*/
2016-08-24 04:08:40 -07:00
2017-11-16 20:11:17 -08:00
getSize() {
let size = 0;
2016-08-24 04:08:40 -07:00
2017-11-27 13:21:17 -08:00
size += 6;
2017-11-16 20:11:17 -08:00
switch (this.keyType) {
case Path.types.HD:
size += 8;
break;
case Path.types.KEY:
size += 1;
size += encoding.sizeVarBytes(this.data);
break;
}
2017-11-16 20:11:17 -08:00
return size;
}
2017-11-16 20:11:17 -08:00
/**
* Serialize path.
* @returns {Buffer}
*/
toRaw() {
const size = this.getSize();
const bw = bio.write(size);
bw.writeU32(this.account);
bw.writeU8(this.keyType);
2018-01-02 20:24:56 -08:00
bw.writeU8(this.version);
2017-11-27 12:36:12 -08:00
2017-11-16 20:11:17 -08:00
switch (this.keyType) {
case Path.types.HD:
assert(!this.data);
assert(this.index !== -1);
bw.writeU32(this.branch);
bw.writeU32(this.index);
break;
case Path.types.KEY:
assert(this.data);
assert(this.index === -1);
bw.writeU8(this.encrypted ? 1 : 0);
bw.writeVarBytes(this.data);
break;
case Path.types.ADDRESS:
assert(!this.data);
assert(this.index === -1);
break;
default:
assert(false);
break;
}
return bw.render();
2016-08-24 04:08:40 -07:00
}
2017-11-16 20:11:17 -08:00
/**
* Inject properties from address.
* @private
* @param {Account} account
* @param {Address} address
*/
fromAddress(account, address) {
this.keyType = Path.types.ADDRESS;
this.name = account.name;
this.account = account.accountIndex;
this.version = address.version;
this.hash = address.getHash('hex');
return this;
}
2016-08-24 04:08:40 -07:00
2017-11-16 20:11:17 -08:00
/**
* Instantiate path from address.
* @param {Account} account
* @param {Address} address
* @returns {Path}
*/
2016-08-24 04:08:40 -07:00
2017-11-16 20:11:17 -08:00
static fromAddress(account, address) {
return new this().fromAddress(account, address);
}
2016-09-30 23:46:13 -07:00
2017-11-16 20:11:17 -08:00
/**
* Convert path object to string derivation path.
* @returns {String}
*/
2016-09-30 23:46:13 -07:00
2017-11-16 20:11:17 -08:00
toPath() {
if (this.keyType !== Path.types.HD)
return null;
2016-09-30 23:46:13 -07:00
2017-11-16 20:11:17 -08:00
return `m/${this.account}'/${this.branch}/${this.index}`;
}
2016-08-24 04:08:40 -07:00
2017-11-16 20:11:17 -08:00
/**
* Convert path object to an address (currently unused).
* @returns {Address}
*/
2016-08-24 04:08:40 -07:00
2017-11-16 20:11:17 -08:00
toAddress() {
2018-01-02 20:24:56 -08:00
return Address.fromHash(this.hash, this.version);
2017-11-16 20:11:17 -08:00
}
2016-09-30 23:46:13 -07:00
2017-11-16 20:11:17 -08:00
/**
* Convert path to a json-friendly object.
* @returns {Object}
*/
toJSON() {
return {
name: this.name,
account: this.account,
change: this.branch === 1,
derivation: this.toPath()
};
}
2016-08-24 04:08:40 -07:00
2017-11-16 20:11:17 -08:00
/**
* Inspect the path.
* @returns {String}
*/
2016-08-24 04:08:40 -07:00
2017-11-16 20:11:17 -08:00
inspect() {
return `<Path: ${this.name}:${this.toPath()}>`;
}
}
2016-08-24 04:08:40 -07:00
/**
2017-11-16 20:11:17 -08:00
* Path types.
* @enum {Number}
* @default
2016-08-24 04:08:40 -07:00
*/
2017-11-16 20:11:17 -08:00
Path.types = {
HD: 0,
KEY: 1,
ADDRESS: 2
2016-08-24 04:08:40 -07:00
};
/**
2017-11-16 20:11:17 -08:00
* Path types.
* @enum {Number}
* @default
2016-08-24 04:08:40 -07:00
*/
2017-11-16 20:11:17 -08:00
Path.typesByVal = [
'HD',
'KEY',
'ADDRESS'
];
2016-08-24 04:08:40 -07:00
/**
* Expose
*/
module.exports = Path;