bitcore-wallet-service/lib/model/address.js

93 lines
2.5 KiB
JavaScript
Raw Normal View History

2015-01-27 05:18:45 -08:00
'use strict';
var $ = require('preconditions').singleton();
var _ = require('lodash');
2017-08-23 13:28:35 -07:00
var Bitcore = {
'btc': require('bitcore-lib'),
'bch': require('bitcore-lib-cash'),
};
var Common = require('../common');
var Constants = Common.Constants,
Defaults = Common.Defaults,
Utils = Common.Utils;
2015-02-03 11:46:28 -08:00
function Address() {};
2015-02-17 16:20:08 -08:00
Address.create = function(opts) {
2015-02-02 12:07:18 -08:00
opts = opts || {};
2015-01-27 05:18:45 -08:00
2015-02-17 16:20:08 -08:00
var x = new Address();
2017-08-23 13:28:35 -07:00
$.checkArgument(Utils.checkValueInCollection(opts.coin, Constants.COINS));
x.version = '1.0.0';
2015-02-17 16:20:08 -08:00
x.createdOn = Math.floor(Date.now() / 1000);
x.address = opts.address;
2015-04-20 13:46:40 -07:00
x.walletId = opts.walletId;
2015-02-21 14:13:15 -08:00
x.isChange = opts.isChange;
2015-02-17 16:20:08 -08:00
x.path = opts.path;
x.publicKeys = opts.publicKeys;
2017-08-23 13:28:35 -07:00
x.coin = opts.coin;
x.network = Bitcore[opts.coin].Address(x.address).toObject().network;
2015-10-30 11:24:47 -07:00
x.type = opts.type || Constants.SCRIPT_TYPES.P2SH;
2015-10-28 09:23:13 -07:00
x.hasActivity = undefined;
2015-02-17 16:20:08 -08:00
return x;
2015-01-27 05:18:45 -08:00
};
2015-02-17 16:20:08 -08:00
Address.fromObj = function(obj) {
2015-02-02 12:07:18 -08:00
var x = new Address();
2015-01-27 05:18:45 -08:00
x.version = obj.version;
2015-02-02 12:07:18 -08:00
x.createdOn = obj.createdOn;
x.address = obj.address;
2015-04-20 13:46:40 -07:00
x.walletId = obj.walletId;
2017-08-23 13:28:35 -07:00
x.coin = obj.coin || Defaults.COIN;
2015-04-23 08:25:36 -07:00
x.network = obj.network;
2015-02-21 14:13:15 -08:00
x.isChange = obj.isChange;
2015-02-02 12:07:18 -08:00
x.path = obj.path;
2015-02-03 11:46:28 -08:00
x.publicKeys = obj.publicKeys;
2015-10-30 11:24:47 -07:00
x.type = obj.type || Constants.SCRIPT_TYPES.P2SH;
2015-10-28 09:23:13 -07:00
x.hasActivity = obj.hasActivity;
2015-02-02 12:07:18 -08:00
return x;
2015-01-27 05:18:45 -08:00
};
2017-08-23 13:28:35 -07:00
Address._deriveAddress = function(scriptType, publicKeyRing, path, m, coin, network) {
$.checkArgument(Utils.checkValueInCollection(scriptType, Constants.SCRIPT_TYPES));
var publicKeys = _.map(publicKeyRing, function(item) {
2017-08-23 13:28:35 -07:00
var xpub = new Bitcore[coin].HDPublicKey(item.xPubKey);
2017-03-15 11:36:11 -07:00
return xpub.deriveChild(path).publicKey;
});
var bitcoreAddress;
switch (scriptType) {
case Constants.SCRIPT_TYPES.P2SH:
2017-08-23 13:28:35 -07:00
bitcoreAddress = Bitcore[coin].Address.createMultisig(publicKeys, m, network);
break;
case Constants.SCRIPT_TYPES.P2PKH:
$.checkState(_.isArray(publicKeys) && publicKeys.length == 1);
2017-08-23 13:28:35 -07:00
bitcoreAddress = Bitcore[coin].Address.fromPublicKey(publicKeys[0], network);
break;
}
return {
address: bitcoreAddress.toString(),
path: path,
publicKeys: _.invoke(publicKeys, 'toString'),
};
};
2017-08-23 13:28:35 -07:00
Address.derive = function(walletId, scriptType, publicKeyRing, path, m, coin, network, isChange) {
var raw = Address._deriveAddress(scriptType, publicKeyRing, path, m, coin, network);
return Address.create(_.extend(raw, {
2017-08-23 13:28:35 -07:00
coin: coin,
walletId: walletId,
type: scriptType,
isChange: isChange,
}));
};
2015-01-27 05:18:45 -08:00
module.exports = Address;