This repository has been archived by the owner on Apr 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 547
/
address.js
95 lines (79 loc) · 2.63 KB
/
address.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
'use strict';
var $ = require('preconditions').singleton();
var _ = require('lodash');
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;
function Address() {};
Address.create = function(opts) {
opts = opts || {};
var x = new Address();
$.checkArgument(Utils.checkValueInCollection(opts.coin, Constants.COINS));
x.version = '1.0.0';
x.createdOn = Math.floor(Date.now() / 1000);
x.address = opts.address;
x.walletId = opts.walletId;
x.isChange = opts.isChange;
x.path = opts.path;
x.publicKeys = opts.publicKeys;
x.coin = opts.coin;
x.network = Bitcore[opts.coin].Address(x.address).toObject().network;
x.type = opts.type || Constants.SCRIPT_TYPES.P2SH;
x.hasActivity = undefined;
x.beRegistered = null;
return x;
};
Address.fromObj = function(obj) {
var x = new Address();
x.version = obj.version;
x.createdOn = obj.createdOn;
x.address = obj.address;
x.walletId = obj.walletId;
x.coin = obj.coin || Defaults.COIN;
x.network = obj.network;
x.isChange = obj.isChange;
x.path = obj.path;
x.publicKeys = obj.publicKeys;
x.type = obj.type || Constants.SCRIPT_TYPES.P2SH;
x.hasActivity = obj.hasActivity;
x.beRegistered = obj.beRegistered;
return x;
};
Address._deriveAddress = function(scriptType, publicKeyRing, path, m, coin, network) {
$.checkArgument(Utils.checkValueInCollection(scriptType, Constants.SCRIPT_TYPES));
var publicKeys = _.map(publicKeyRing, function(item) {
var xpub = new Bitcore[coin].HDPublicKey(item.xPubKey);
return xpub.deriveChild(path).publicKey;
});
var bitcoreAddress;
switch (scriptType) {
case Constants.SCRIPT_TYPES.P2SH:
bitcoreAddress = Bitcore[coin].Address.createMultisig(publicKeys, m, network);
break;
case Constants.SCRIPT_TYPES.P2PKH:
$.checkState(_.isArray(publicKeys) && publicKeys.length == 1);
bitcoreAddress = Bitcore[coin].Address.fromPublicKey(publicKeys[0], network);
break;
}
return {
// bws still use legacy addresses for BCH
address: coin == 'bch' ? bitcoreAddress.toLegacyAddress() : bitcoreAddress.toString(),
path: path,
publicKeys: _.invokeMap(publicKeys, 'toString'),
};
};
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, {
coin: coin,
walletId: walletId,
type: scriptType,
isChange: isChange,
}));
};
module.exports = Address;