|
|
@ -1,5 +1,8 @@ |
|
|
|
'use strict'; |
|
|
|
|
|
|
|
var $ = require('preconditions').singleton(); |
|
|
|
var _ = require('lodash'); |
|
|
|
|
|
|
|
var Bitcore = require('bitcore-lib'); |
|
|
|
var Constants = require('../common/constants'); |
|
|
|
|
|
|
@ -39,4 +42,40 @@ Address.fromObj = function(obj) { |
|
|
|
return x; |
|
|
|
}; |
|
|
|
|
|
|
|
Address._deriveAddress = function(scriptType, publicKeyRing, path, m, network) { |
|
|
|
$.checkArgument(_.contains(_.values(Constants.SCRIPT_TYPES), scriptType)); |
|
|
|
|
|
|
|
var publicKeys = _.map(publicKeyRing, function(item) { |
|
|
|
var xpub = new Bitcore.HDPublicKey(item.xPubKey); |
|
|
|
return xpub.derive(path).publicKey; |
|
|
|
}); |
|
|
|
|
|
|
|
var bitcoreAddress; |
|
|
|
switch (scriptType) { |
|
|
|
case Constants.SCRIPT_TYPES.P2SH: |
|
|
|
bitcoreAddress = Bitcore.Address.createMultisig(publicKeys, m, network); |
|
|
|
break; |
|
|
|
case Constants.SCRIPT_TYPES.P2PKH: |
|
|
|
$.checkState(_.isArray(publicKeys) && publicKeys.length == 1); |
|
|
|
bitcoreAddress = Bitcore.Address.fromPublicKey(publicKeys[0], network); |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
return { |
|
|
|
address: bitcoreAddress.toString(), |
|
|
|
path: path, |
|
|
|
publicKeys: _.invoke(publicKeys, 'toString'), |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
Address.derive = function(walletId, scriptType, publicKeyRing, path, m, network, isChange) { |
|
|
|
var raw = Address._deriveAddress(scriptType, publicKeyRing, path, m, network); |
|
|
|
return Address.create(_.extend(raw, { |
|
|
|
walletId: walletId, |
|
|
|
type: scriptType, |
|
|
|
isChange: isChange, |
|
|
|
})); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
module.exports = Address; |
|
|
|