forked from bitpay/bitcore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wallet.js
139 lines (115 loc) · 3.01 KB
/
Wallet.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
var hex = function(hex) {
return new Buffer(hex, 'hex');
};
var fs = require('fs');
var EncFile = require('../util/EncFile');
var Address = require('./Address');
var networks = require('../networks');
var util = require('../util');
var ENC_METHOD = 'aes-256-cbc';
var skeleton = {
client: 'libcoin',
client_version: '0.0.1',
network: 'testnet',
version: 1,
best_hash: null,
best_height: -1,
keys: [],
sin: {},
scripts: {},
};
function Wallet(cfg) {
if (typeof cfg !== 'object')
cfg = {};
// deep copy (no references)
if (cfg.datastore)
this.datastore = JSON.parse(JSON.stringify(cfg.datastore));
else
this.datastore = JSON.parse(JSON.stringify(skeleton));
this.network = undefined;
this.dirty = cfg.dirty || true;
};
Wallet.prototype.readSync = function(filename, passphrase) {
this.datastore = EncFile.readJFileSync(ENC_METHOD,
passphrase, filename);
this.dirty = false;
};
Wallet.prototype.writeSync = function(filename, passphrase) {
var tmp_fn = filename + ".tmp";
EncFile.writeJFileSync(ENC_METHOD, passphrase, tmp_fn,
this.datastore);
fs.renameSync(tmp_fn, filename);
this.dirty = false;
};
Wallet.prototype.setNetwork = function(netname) {
if (!netname)
netname = this.datastore.network;
switch (netname) {
case "mainnet":
case "livenet":
this.network = networks.livenet;
break;
case "testnet":
this.network = networks.testnet;
break;
default:
throw new Error("Unsupported network");
}
// store+canonicalize name
this.datastore['network'] = this.network.name;
this.dirty = true;
};
Wallet.prototype.addKey = function(wkey) {
this.datastore.keys.push(wkey);
this.dirty = true;
};
Wallet.prototype.addSIN = function(sinObj) {
this.datastore.sin[sinObj.sin] = sinObj;
this.dirty = true;
};
Wallet.prototype.findKeyHash = function(pubKeyHash) {
var pkhStr = pubKeyHash.toString();
for (var i = 0; i < this.datastore.keys.length; i++) {
var obj = this.datastore.keys[i];
var addrStr = obj.addr;
var addr = new Address(addrStr);
if (addr.payload().toString() == pkhStr)
return obj;
}
return undefined;
};
Wallet.prototype.expandKey = function(key) {
var addr = new Address(key);
var isAddr = true;
try {
addr.validate();
var b = addr.payload();
var obj = this.findKeyHash(b);
key = obj.pub;
} catch (e) {
// do nothing
}
var re = /^[a-fA-F0-9]+$/;
if (!key.match(re))
throw new Error("Unknown key type");
return hex(key);
};
Wallet.prototype.expandKeys = function(keys) {
var res = [];
var us = this;
keys.forEach(function(key) {
var expKey = us.expandKey(key);
res.push(expKey);
});
return res;
};
Wallet.prototype.addScript = function(script) {
var buf = script.getBuffer();
var hash = util.sha256ripe160(buf);
var addr = new Address(this.network.P2SHVersion, hash);
var addrStr = addr.as('base58');
this.datastore.scripts[addrStr] = buf.toString('hex');
this.dirty = true;
return addrStr;
};
module.exports = Wallet;