1 | 'use strict'
|
2 |
|
3 | var _ = require('./util/_')
|
4 | var $ = require('./util/preconditions')
|
5 | var errors = require('./errors')
|
6 | var Base58Check = require('./encoding/base58check')
|
7 | var Networks = require('./networks')
|
8 | var Hash = require('./crypto/hash')
|
9 | var JSUtil = require('./util/js')
|
10 | var PublicKey = require('./publickey')
|
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 | function Address (data, network, type) {
|
47 | if (!(this instanceof Address)) {
|
48 | return new Address(data, network, type)
|
49 | }
|
50 |
|
51 | if (_.isArray(data) && _.isNumber(network)) {
|
52 | return Address.createMultisig(data, network, type)
|
53 | }
|
54 |
|
55 | if (data instanceof Address) {
|
56 |
|
57 | return data
|
58 | }
|
59 |
|
60 | $.checkArgument(data, 'First argument is required, please include address data.', 'guide/address.html')
|
61 |
|
62 | if (network && !Networks.get(network)) {
|
63 | throw new TypeError('Second argument must be "livenet", "testnet", or "regtest".')
|
64 | }
|
65 |
|
66 | if (type && (type !== Address.PayToPublicKeyHash && type !== Address.PayToScriptHash)) {
|
67 | throw new TypeError('Third argument must be "pubkeyhash" or "scripthash".')
|
68 | }
|
69 |
|
70 | var info = this._classifyArguments(data, network, type)
|
71 |
|
72 |
|
73 | info.network = info.network || Networks.get(network) || Networks.defaultNetwork
|
74 | info.type = info.type || type || Address.PayToPublicKeyHash
|
75 |
|
76 | JSUtil.defineImmutable(this, {
|
77 | hashBuffer: info.hashBuffer,
|
78 | network: info.network,
|
79 | type: info.type
|
80 | })
|
81 |
|
82 | return this
|
83 | }
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | Address.prototype._classifyArguments = function (data, network, type) {
|
93 |
|
94 | if ((data instanceof Buffer || data instanceof Uint8Array) && data.length === 20) {
|
95 | return Address._transformHash(data)
|
96 | } else if ((data instanceof Buffer || data instanceof Uint8Array) && data.length === 21) {
|
97 | return Address._transformBuffer(data, network, type)
|
98 | } else if (data instanceof PublicKey) {
|
99 | return Address._transformPublicKey(data)
|
100 | } else if (data instanceof Script) {
|
101 | return Address._transformScript(data, network)
|
102 | } else if (typeof (data) === 'string') {
|
103 | return Address._transformString(data, network, type)
|
104 | } else if (_.isObject(data)) {
|
105 | return Address._transformObject(data)
|
106 | } else {
|
107 | throw new TypeError('First argument is an unrecognized data format.')
|
108 | }
|
109 | }
|
110 |
|
111 |
|
112 | Address.PayToPublicKeyHash = 'pubkeyhash'
|
113 |
|
114 | Address.PayToScriptHash = 'scripthash'
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | Address._transformHash = function (hash) {
|
122 | var info = {}
|
123 | if (!(hash instanceof Buffer) && !(hash instanceof Uint8Array)) {
|
124 | throw new TypeError('Address supplied is not a buffer.')
|
125 | }
|
126 | if (hash.length !== 20) {
|
127 | throw new TypeError('Address hashbuffers must be exactly 20 bytes.')
|
128 | }
|
129 | info.hashBuffer = hash
|
130 | return info
|
131 | }
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | Address._transformObject = function (data) {
|
142 | $.checkArgument(data.hash || data.hashBuffer, 'Must provide a `hash` or `hashBuffer` property')
|
143 | $.checkArgument(data.type, 'Must provide a `type` property')
|
144 | return {
|
145 | hashBuffer: data.hash ? Buffer.from(data.hash, 'hex') : data.hashBuffer,
|
146 | network: Networks.get(data.network) || Networks.defaultNetwork,
|
147 | type: data.type
|
148 | }
|
149 | }
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 | Address._classifyFromVersion = function (buffer) {
|
159 | var version = {}
|
160 |
|
161 | var pubkeyhashNetwork = Networks.get(buffer[0], 'pubkeyhash')
|
162 | var scripthashNetwork = Networks.get(buffer[0], 'scripthash')
|
163 |
|
164 | if (pubkeyhashNetwork) {
|
165 | version.network = pubkeyhashNetwork
|
166 | version.type = Address.PayToPublicKeyHash
|
167 | } else if (scripthashNetwork) {
|
168 | version.network = scripthashNetwork
|
169 | version.type = Address.PayToScriptHash
|
170 | }
|
171 |
|
172 | return version
|
173 | }
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 | Address._transformBuffer = function (buffer, network, type) {
|
185 | var info = {}
|
186 | if (!(buffer instanceof Buffer) && !(buffer instanceof Uint8Array)) {
|
187 | throw new TypeError('Address supplied is not a buffer.')
|
188 | }
|
189 | if (buffer.length !== 1 + 20) {
|
190 | throw new TypeError('Address buffers must be exactly 21 bytes.')
|
191 | }
|
192 |
|
193 | var networkObj = Networks.get(network)
|
194 | var bufferVersion = Address._classifyFromVersion(buffer)
|
195 |
|
196 | if (network && !networkObj) {
|
197 | throw new TypeError('Unknown network')
|
198 | }
|
199 |
|
200 | if (!bufferVersion.network || (networkObj && networkObj !== bufferVersion.network)) {
|
201 |
|
202 | throw new TypeError('Address has mismatched network type.')
|
203 | }
|
204 |
|
205 | if (!bufferVersion.type || (type && type !== bufferVersion.type)) {
|
206 | throw new TypeError('Address has mismatched type.')
|
207 | }
|
208 |
|
209 | info.hashBuffer = buffer.slice(1)
|
210 | info.network = bufferVersion.network
|
211 | info.type = bufferVersion.type
|
212 | return info
|
213 | }
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 | Address._transformPublicKey = function (pubkey) {
|
223 | var info = {}
|
224 | if (!(pubkey instanceof PublicKey)) {
|
225 | throw new TypeError('Address must be an instance of PublicKey.')
|
226 | }
|
227 | info.hashBuffer = Hash.sha256ripemd160(pubkey.toBuffer())
|
228 | info.type = Address.PayToPublicKeyHash
|
229 | return info
|
230 | }
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 | Address._transformScript = function (script, network) {
|
240 | $.checkArgument(script instanceof Script, 'script must be a Script instance')
|
241 | var info = script.getAddressInfo(network)
|
242 | if (!info) {
|
243 | throw new errors.Script.CantDeriveAddress(script)
|
244 | }
|
245 | return info
|
246 | }
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 | Address.createMultisig = function (publicKeys, threshold, network) {
|
261 | network = network || publicKeys[0].network || Networks.defaultNetwork
|
262 | return Address.payingTo(Script.buildMultisigOut(publicKeys, threshold), network)
|
263 | }
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 | Address._transformString = function (data, network, type) {
|
275 | if (typeof (data) !== 'string') {
|
276 | throw new TypeError('data parameter supplied is not a string.')
|
277 | }
|
278 | if (data.length < 27) {
|
279 | throw new Error('Invalid Address string provided')
|
280 | }
|
281 | data = data.trim()
|
282 | var networkObj = Networks.get(network)
|
283 |
|
284 | if (network && !networkObj) {
|
285 | throw new TypeError('Unknown network')
|
286 | }
|
287 |
|
288 | var addressBuffer = Base58Check.decode(data)
|
289 | return Address._transformBuffer(addressBuffer, network, type)
|
290 | }
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 | Address.fromPublicKey = function (data, network) {
|
300 | var info = Address._transformPublicKey(data)
|
301 | network = network || Networks.defaultNetwork
|
302 | return new Address(info.hashBuffer, network, info.type)
|
303 | }
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 | Address.fromPrivateKey = function (privateKey, network) {
|
313 | let publicKey = PublicKey.fromPrivateKey(privateKey)
|
314 | network = network || privateKey.network || Networks.defaultNetwork
|
315 | return Address.fromPublicKey(publicKey, network)
|
316 | }
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 |
|
325 | Address.fromPublicKeyHash = function (hash, network) {
|
326 | var info = Address._transformHash(hash)
|
327 | return new Address(info.hashBuffer, network, Address.PayToPublicKeyHash)
|
328 | }
|
329 |
|
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 |
|
336 |
|
337 | Address.fromScriptHash = function (hash, network) {
|
338 | $.checkArgument(hash, 'hash parameter is required')
|
339 | var info = Address._transformHash(hash)
|
340 | return new Address(info.hashBuffer, network, Address.PayToScriptHash)
|
341 | }
|
342 |
|
343 |
|
344 |
|
345 |
|
346 |
|
347 |
|
348 |
|
349 |
|
350 |
|
351 |
|
352 |
|
353 | Address.payingTo = function (script, network) {
|
354 | $.checkArgument(script, 'script is required')
|
355 | $.checkArgument(script instanceof Script, 'script must be instance of Script')
|
356 |
|
357 | return Address.fromScriptHash(Hash.sha256ripemd160(script.toBuffer()), network)
|
358 | }
|
359 |
|
360 |
|
361 |
|
362 |
|
363 |
|
364 |
|
365 |
|
366 |
|
367 |
|
368 |
|
369 |
|
370 |
|
371 |
|
372 | Address.fromScript = function (script, network) {
|
373 | $.checkArgument(script instanceof Script, 'script must be a Script instance')
|
374 | var info = Address._transformScript(script, network)
|
375 | return new Address(info.hashBuffer, network, info.type)
|
376 | }
|
377 |
|
378 |
|
379 |
|
380 |
|
381 |
|
382 |
|
383 |
|
384 |
|
385 |
|
386 | Address.fromBuffer = function (buffer, network, type) {
|
387 | var info = Address._transformBuffer(buffer, network, type)
|
388 | return new Address(info.hashBuffer, info.network, info.type)
|
389 | }
|
390 |
|
391 | Address.fromHex = function (hex, network, type) {
|
392 | return Address.fromBuffer(Buffer.from(hex, 'hex'), network, type)
|
393 | }
|
394 |
|
395 |
|
396 |
|
397 |
|
398 |
|
399 |
|
400 |
|
401 |
|
402 |
|
403 | Address.fromString = function (str, network, type) {
|
404 | var info = Address._transformString(str, network, type)
|
405 | return new Address(info.hashBuffer, info.network, info.type)
|
406 | }
|
407 |
|
408 |
|
409 |
|
410 |
|
411 |
|
412 |
|
413 |
|
414 | Address.fromObject = function fromObject (obj) {
|
415 | $.checkState(
|
416 | JSUtil.isHexa(obj.hash),
|
417 | 'Unexpected hash property, "' + obj.hash + '", expected to be hex.'
|
418 | )
|
419 | var hashBuffer = Buffer.from(obj.hash, 'hex')
|
420 | return new Address(hashBuffer, obj.network, obj.type)
|
421 | }
|
422 |
|
423 |
|
424 |
|
425 |
|
426 |
|
427 |
|
428 |
|
429 |
|
430 |
|
431 |
|
432 |
|
433 |
|
434 |
|
435 |
|
436 |
|
437 | Address.getValidationError = function (data, network, type) {
|
438 | var error
|
439 | try {
|
440 | new Address(data, network, type)
|
441 | } catch (e) {
|
442 | error = e
|
443 | }
|
444 | return error
|
445 | }
|
446 |
|
447 |
|
448 |
|
449 |
|
450 |
|
451 |
|
452 |
|
453 |
|
454 |
|
455 |
|
456 |
|
457 |
|
458 |
|
459 |
|
460 | Address.isValid = function (data, network, type) {
|
461 | return !Address.getValidationError(data, network, type)
|
462 | }
|
463 |
|
464 |
|
465 |
|
466 |
|
467 |
|
468 | Address.prototype.isPayToPublicKeyHash = function () {
|
469 | return this.type === Address.PayToPublicKeyHash
|
470 | }
|
471 |
|
472 |
|
473 |
|
474 |
|
475 |
|
476 | Address.prototype.isPayToScriptHash = function () {
|
477 | return this.type === Address.PayToScriptHash
|
478 | }
|
479 |
|
480 |
|
481 |
|
482 |
|
483 |
|
484 |
|
485 | Address.prototype.toBuffer = function () {
|
486 | var version = Buffer.from([this.network[this.type]])
|
487 | var buf = Buffer.concat([version, this.hashBuffer])
|
488 | return buf
|
489 | }
|
490 |
|
491 | Address.prototype.toHex = function () {
|
492 | return this.toBuffer().toString('hex')
|
493 | }
|
494 |
|
495 |
|
496 |
|
497 |
|
498 | Address.prototype.toObject = Address.prototype.toJSON = function toObject () {
|
499 | return {
|
500 | hash: this.hashBuffer.toString('hex'),
|
501 | type: this.type,
|
502 | network: this.network.toString()
|
503 | }
|
504 | }
|
505 |
|
506 |
|
507 |
|
508 |
|
509 |
|
510 |
|
511 | Address.prototype.inspect = function () {
|
512 | return '<Address: ' + this.toString() + ', type: ' + this.type + ', network: ' + this.network + '>'
|
513 | }
|
514 |
|
515 |
|
516 |
|
517 |
|
518 |
|
519 |
|
520 | Address.prototype.toString = function () {
|
521 | return Base58Check.encode(this.toBuffer())
|
522 | }
|
523 |
|
524 | module.exports = Address
|
525 |
|
526 | var Script = require('./script')
|