@opcat-labs/opcat 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.mocharc.yaml +3 -0
- package/index.d.ts +1541 -0
- package/index.js +74 -0
- package/lib/address.js +478 -0
- package/lib/block/block.js +277 -0
- package/lib/block/blockheader.js +295 -0
- package/lib/block/index.js +4 -0
- package/lib/block/merkleblock.js +323 -0
- package/lib/bn.js +3423 -0
- package/lib/crypto/bn.js +278 -0
- package/lib/crypto/ecdsa.js +339 -0
- package/lib/crypto/hash.browser.js +171 -0
- package/lib/crypto/hash.js +2 -0
- package/lib/crypto/hash.node.js +171 -0
- package/lib/crypto/point.js +221 -0
- package/lib/crypto/random.browser.js +28 -0
- package/lib/crypto/random.js +2 -0
- package/lib/crypto/random.node.js +11 -0
- package/lib/crypto/signature.js +325 -0
- package/lib/encoding/base58.js +111 -0
- package/lib/encoding/base58check.js +121 -0
- package/lib/encoding/bufferreader.js +212 -0
- package/lib/encoding/bufferwriter.js +140 -0
- package/lib/encoding/decode-asm.js +24 -0
- package/lib/encoding/decode-hex.js +32 -0
- package/lib/encoding/decode-script-chunks.js +43 -0
- package/lib/encoding/encode-hex.js +284 -0
- package/lib/encoding/is-hex.js +7 -0
- package/lib/encoding/varint.js +75 -0
- package/lib/errors/index.js +54 -0
- package/lib/errors/spec.js +314 -0
- package/lib/hash-cache.js +50 -0
- package/lib/hdprivatekey.js +678 -0
- package/lib/hdpublickey.js +525 -0
- package/lib/message/message.js +191 -0
- package/lib/mnemonic/mnemonic.js +303 -0
- package/lib/mnemonic/pbkdf2.browser.js +68 -0
- package/lib/mnemonic/pbkdf2.js +2 -0
- package/lib/mnemonic/pbkdf2.node.js +68 -0
- package/lib/mnemonic/words/chinese.js +2054 -0
- package/lib/mnemonic/words/english.js +2054 -0
- package/lib/mnemonic/words/french.js +2054 -0
- package/lib/mnemonic/words/index.js +8 -0
- package/lib/mnemonic/words/italian.js +2054 -0
- package/lib/mnemonic/words/japanese.js +2054 -0
- package/lib/mnemonic/words/spanish.js +2054 -0
- package/lib/networks.js +379 -0
- package/lib/opcode.js +255 -0
- package/lib/privatekey.js +374 -0
- package/lib/publickey.js +386 -0
- package/lib/script/index.js +5 -0
- package/lib/script/interpreter.js +1834 -0
- package/lib/script/script.js +1074 -0
- package/lib/script/stack.js +109 -0
- package/lib/script/write-i32-le.js +17 -0
- package/lib/script/write-push-data.js +35 -0
- package/lib/script/write-u16-le.js +12 -0
- package/lib/script/write-u32-le.js +16 -0
- package/lib/script/write-u64-le.js +24 -0
- package/lib/script/write-u8-le.js +8 -0
- package/lib/script/write-varint.js +46 -0
- package/lib/transaction/index.js +7 -0
- package/lib/transaction/input/index.js +5 -0
- package/lib/transaction/input/input.js +354 -0
- package/lib/transaction/input/multisig.js +242 -0
- package/lib/transaction/input/publickey.js +100 -0
- package/lib/transaction/input/publickeyhash.js +118 -0
- package/lib/transaction/output.js +231 -0
- package/lib/transaction/sighash.js +167 -0
- package/lib/transaction/signature.js +97 -0
- package/lib/transaction/transaction.js +1639 -0
- package/lib/transaction/unspentoutput.js +113 -0
- package/lib/util/_.js +47 -0
- package/lib/util/js.js +90 -0
- package/lib/util/preconditions.js +33 -0
- package/package.json +26 -0
- package/test/address.js +509 -0
- package/test/block/block.js +251 -0
- package/test/block/blockheader.js +275 -0
- package/test/block/merklebloack.js +211 -0
- package/test/crypto/bn.js +177 -0
- package/test/crypto/ecdsa.js +391 -0
- package/test/crypto/hash.browser.js +135 -0
- package/test/crypto/hash.js +136 -0
- package/test/crypto/point.js +224 -0
- package/test/crypto/random.js +32 -0
- package/test/crypto/signature.js +409 -0
- package/test/data/bip69.json +215 -0
- package/test/data/bitcoind/base58_keys_invalid.json +52 -0
- package/test/data/bitcoind/base58_keys_valid.json +335 -0
- package/test/data/bitcoind/blocks.json +22 -0
- package/test/data/bitcoind/script_tests.json +3822 -0
- package/test/data/bitcoind/sig_canonical.json +7 -0
- package/test/data/bitcoind/sig_noncanonical.json +36 -0
- package/test/data/bitcoind/tx_invalid.json +445 -0
- package/test/data/bitcoind/tx_valid.json +44 -0
- package/test/data/blk86756-testnet.dat +0 -0
- package/test/data/blk86756-testnet.js +14 -0
- package/test/data/blk86756-testnet.json +684 -0
- package/test/data/block.hex +1 -0
- package/test/data/ecdsa.json +230 -0
- package/test/data/merkleblocks.js +488 -0
- package/test/data/messages.json +22 -0
- package/test/data/sighash.json +12 -0
- package/test/data/tx_creation.json +95 -0
- package/test/encoding/base58.js +131 -0
- package/test/encoding/base58check.js +136 -0
- package/test/encoding/bufferreader.js +337 -0
- package/test/encoding/bufferwriter.js +172 -0
- package/test/encoding/varint.js +104 -0
- package/test/hashCache.js +67 -0
- package/test/hdkeys.js +445 -0
- package/test/hdprivatekey.js +332 -0
- package/test/hdpublickey.js +304 -0
- package/test/index.js +16 -0
- package/test/message/message.js +204 -0
- package/test/mnemonic/data/fixtures.json +300 -0
- package/test/mnemonic/mnemonic.js +259 -0
- package/test/mnemonic/mocha.opts +1 -0
- package/test/mnemonic/pbkdf2.test.js +59 -0
- package/test/networks.js +159 -0
- package/test/opcode.js +161 -0
- package/test/privatekey.js +439 -0
- package/test/publickey.js +554 -0
- package/test/script/interpreter.js +734 -0
- package/test/script/script.js +1437 -0
- package/test/transaction/deserialize.js +34 -0
- package/test/transaction/input/input.js +90 -0
- package/test/transaction/input/multisig.js +90 -0
- package/test/transaction/input/publickey.js +68 -0
- package/test/transaction/input/publickeyhash.js +51 -0
- package/test/transaction/output.js +185 -0
- package/test/transaction/sighash.js +65 -0
- package/test/transaction/signature.js +114 -0
- package/test/transaction/transaction.js +1109 -0
- package/test/transaction/unspentoutput.js +110 -0
- package/test/util/js.js +76 -0
- package/test/util/preconditions.js +79 -0
package/index.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var opcat = {};
|
|
4
|
+
|
|
5
|
+
// module information
|
|
6
|
+
opcat.version = 'v' + require('./package.json').version;
|
|
7
|
+
opcat.versionGuard = function (version) {
|
|
8
|
+
if (version !== undefined) {
|
|
9
|
+
var message = `
|
|
10
|
+
More than one instance of opcat found. other version: ${version}
|
|
11
|
+
Please make sure to require opcat and check that submodules do
|
|
12
|
+
not also include their own opcat dependency.`;
|
|
13
|
+
console.warn(message);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
opcat.versionGuard(global.opcat);
|
|
17
|
+
global.opcat = opcat.version;
|
|
18
|
+
|
|
19
|
+
// crypto
|
|
20
|
+
opcat.crypto = {};
|
|
21
|
+
opcat.crypto.BN = require('./lib/crypto/bn.js');
|
|
22
|
+
opcat.crypto.ECDSA = require('./lib/crypto/ecdsa.js');
|
|
23
|
+
opcat.crypto.Hash = require('./lib/crypto/hash.js');
|
|
24
|
+
opcat.crypto.Random = require('./lib/crypto/random.js');
|
|
25
|
+
opcat.crypto.Point = require('./lib/crypto/point.js');
|
|
26
|
+
opcat.crypto.Signature = require('./lib/crypto/signature.js');
|
|
27
|
+
|
|
28
|
+
// encoding
|
|
29
|
+
opcat.encoding = {};
|
|
30
|
+
opcat.encoding.Base58 = require('./lib/encoding/base58.js');
|
|
31
|
+
opcat.encoding.Base58Check = require('./lib/encoding/base58check.js');
|
|
32
|
+
opcat.encoding.BufferReader = require('./lib/encoding/bufferreader.js');
|
|
33
|
+
opcat.encoding.BufferWriter = require('./lib/encoding/bufferwriter.js');
|
|
34
|
+
opcat.encoding.Varint = require('./lib/encoding/varint.js');
|
|
35
|
+
|
|
36
|
+
// utilities
|
|
37
|
+
opcat.util = {};
|
|
38
|
+
opcat.util.js = require('./lib/util/js.js');
|
|
39
|
+
opcat.util.preconditions = require('./lib/util/preconditions.js');
|
|
40
|
+
|
|
41
|
+
// errors thrown by the library
|
|
42
|
+
opcat.errors = require('./lib/errors/index.js');
|
|
43
|
+
|
|
44
|
+
// main bitcoin library
|
|
45
|
+
opcat.Address = require('./lib/address.js');
|
|
46
|
+
opcat.Block = require('./lib/block/index.js');
|
|
47
|
+
opcat.MerkleBlock = require('./lib/block/merkleblock.js');
|
|
48
|
+
opcat.BlockHeader = require('./lib/block/blockheader.js');
|
|
49
|
+
opcat.HDPrivateKey = require('./lib/hdprivatekey.js');
|
|
50
|
+
opcat.HDPublicKey = require('./lib/hdpublickey.js');
|
|
51
|
+
opcat.Networks = require('./lib/networks.js');
|
|
52
|
+
opcat.Opcode = require('./lib/opcode.js');
|
|
53
|
+
opcat.PrivateKey = require('./lib/privatekey.js');
|
|
54
|
+
opcat.PublicKey = require('./lib/publickey.js');
|
|
55
|
+
opcat.Script = require('./lib/script/index.js');
|
|
56
|
+
opcat.Interpreter = require('./lib/script/interpreter.js');
|
|
57
|
+
opcat.Transaction = require('./lib/transaction/index.js');
|
|
58
|
+
opcat.HashCache = require('./lib/hash-cache.js');
|
|
59
|
+
|
|
60
|
+
// dependencies, subject to change
|
|
61
|
+
opcat.deps = {};
|
|
62
|
+
opcat.deps.bs58 = require('bs58');
|
|
63
|
+
opcat.deps.Buffer = Buffer;
|
|
64
|
+
opcat.deps.elliptic = require('elliptic');
|
|
65
|
+
opcat.deps._ = require('./lib/util/_.js');
|
|
66
|
+
|
|
67
|
+
// Internal usage, exposed for testing/advanced tweaking
|
|
68
|
+
opcat.Transaction.sighash = require('./lib/transaction/sighash.js');
|
|
69
|
+
|
|
70
|
+
opcat.Message = require('./lib/message/message.js');
|
|
71
|
+
opcat.Mnemonic = require('./lib/mnemonic/mnemonic.js');
|
|
72
|
+
|
|
73
|
+
module.exports = opcat;
|
|
74
|
+
// export default opcat
|
package/lib/address.js
ADDED
|
@@ -0,0 +1,478 @@
|
|
|
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
|
+
* Instantiate an address from an address String or Buffer, a public key hash Buffer,
|
|
13
|
+
* or an instance of {@link PublicKey}.
|
|
14
|
+
*
|
|
15
|
+
* This is an immutable class, and if the first parameter provided to this constructor is an
|
|
16
|
+
* `Address` instance, the same argument will be returned.
|
|
17
|
+
*
|
|
18
|
+
* An address has two key properties: `network` and `type`. The type is either
|
|
19
|
+
* `Address.PayToPublicKeyHash` (value is the `'pubkeyhash'` string).
|
|
20
|
+
* The network is an instance of {@link Network}.
|
|
21
|
+
* You can quickly check whether an address is of a given kind by using the methods
|
|
22
|
+
* `isPayToPublicKeyHash`
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```javascript
|
|
26
|
+
* // validate that an input field is valid
|
|
27
|
+
* var error = Address.getValidationError(input, 'testnet');
|
|
28
|
+
* if (!error) {
|
|
29
|
+
* var address = Address(input, 'testnet');
|
|
30
|
+
* } else {
|
|
31
|
+
* // invalid network or checksum (typo?)
|
|
32
|
+
* var message = error.messsage;
|
|
33
|
+
* }
|
|
34
|
+
*
|
|
35
|
+
* // get an address from a public key
|
|
36
|
+
* var address = Address(publicKey, 'testnet').toString();
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* @param {*} data - The encoded data in various formats
|
|
40
|
+
* @param {Network|String|number=} network - The network: 'livenet' or 'testnet'
|
|
41
|
+
* @param {string=} type - The type of address: 'pubkey'
|
|
42
|
+
* @returns {Address} A new valid and frozen instance of an Address
|
|
43
|
+
* @constructor
|
|
44
|
+
*/
|
|
45
|
+
function Address(data, network, type) {
|
|
46
|
+
if (!(this instanceof Address)) {
|
|
47
|
+
return new Address(data, network, type);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (data instanceof Address) {
|
|
51
|
+
// Immutable instance
|
|
52
|
+
return data;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
$.checkArgument(
|
|
56
|
+
data,
|
|
57
|
+
'First argument is required, please include address data.',
|
|
58
|
+
'guide/address.html',
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
if (network && !Networks.get(network)) {
|
|
62
|
+
throw new TypeError('Second argument must be "livenet", "testnet", or "regtest".');
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (type && type !== Address.PayToPublicKeyHash) {
|
|
66
|
+
throw new TypeError('Third argument must be "pubkeyhash".');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
var info = this._classifyArguments(data, network, type);
|
|
70
|
+
|
|
71
|
+
// set defaults if not set
|
|
72
|
+
info.network = info.network || Networks.get(network) || Networks.defaultNetwork;
|
|
73
|
+
info.type = info.type || type || Address.PayToPublicKeyHash;
|
|
74
|
+
|
|
75
|
+
JSUtil.defineImmutable(this, {
|
|
76
|
+
hashBuffer: info.hashBuffer,
|
|
77
|
+
network: info.network,
|
|
78
|
+
type: info.type,
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
return this;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Internal function used to split different kinds of arguments of the constructor
|
|
86
|
+
* @param {*} data - The encoded data in various formats
|
|
87
|
+
* @param {Network|String|number=} network - The network: 'livenet' or 'testnet'
|
|
88
|
+
* @param {string=} type - The type of address: 'pubkey'
|
|
89
|
+
* @returns {Object} An "info" object with "type", "network", and "hashBuffer"
|
|
90
|
+
*/
|
|
91
|
+
Address.prototype._classifyArguments = function (data, network, type) {
|
|
92
|
+
var Script = require('./script');
|
|
93
|
+
// transform and validate input data
|
|
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
|
+
/** @static */
|
|
112
|
+
Address.PayToPublicKeyHash = 'pubkeyhash';
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* @param {Buffer} hash - An instance of a hash Buffer
|
|
116
|
+
* @returns {Object} An object with keys: hashBuffer
|
|
117
|
+
* @private
|
|
118
|
+
*/
|
|
119
|
+
Address._transformHash = function (hash) {
|
|
120
|
+
var info = {};
|
|
121
|
+
if (!(hash instanceof Buffer) && !(hash instanceof Uint8Array)) {
|
|
122
|
+
throw new TypeError('Address supplied is not a buffer.');
|
|
123
|
+
}
|
|
124
|
+
if (hash.length !== 20) {
|
|
125
|
+
throw new TypeError('Address hashbuffers must be exactly 20 bytes.');
|
|
126
|
+
}
|
|
127
|
+
info.hashBuffer = hash;
|
|
128
|
+
return info;
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Deserializes an address serialized through `Address#toObject()`
|
|
133
|
+
* @param {Object} data
|
|
134
|
+
* @param {string} data.hash - the hash that this address encodes
|
|
135
|
+
* @param {string} data.type - either 'pubkeyhash' or 'scripthash'
|
|
136
|
+
* @param {Network=} data.network - the name of the network associated
|
|
137
|
+
* @return {Address}
|
|
138
|
+
*/
|
|
139
|
+
Address._transformObject = function (data) {
|
|
140
|
+
$.checkArgument(data.hash || data.hashBuffer, 'Must provide a `hash` or `hashBuffer` property');
|
|
141
|
+
$.checkArgument(data.type, 'Must provide a `type` property');
|
|
142
|
+
return {
|
|
143
|
+
hashBuffer: data.hash ? Buffer.from(data.hash, 'hex') : data.hashBuffer,
|
|
144
|
+
network: Networks.get(data.network) || Networks.defaultNetwork,
|
|
145
|
+
type: data.type,
|
|
146
|
+
};
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Internal function to discover the network and type based on the first data byte
|
|
151
|
+
*
|
|
152
|
+
* @param {Buffer} buffer - An instance of a hex encoded address Buffer
|
|
153
|
+
* @returns {Object} An object with keys: network and type
|
|
154
|
+
* @private
|
|
155
|
+
*/
|
|
156
|
+
Address._classifyFromVersion = function (buffer) {
|
|
157
|
+
var version = {};
|
|
158
|
+
|
|
159
|
+
var pubkeyhashNetwork = Networks.get(buffer[0], 'pubkeyhash');
|
|
160
|
+
|
|
161
|
+
if (pubkeyhashNetwork) {
|
|
162
|
+
version.network = pubkeyhashNetwork;
|
|
163
|
+
version.type = Address.PayToPublicKeyHash;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return version;
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Internal function to transform a bitcoin address buffer
|
|
171
|
+
*
|
|
172
|
+
* @param {Buffer} buffer - An instance of a hex encoded address Buffer
|
|
173
|
+
* @param {string=} network - The network: 'livenet' or 'testnet'
|
|
174
|
+
* @param {string=} type - The type: 'pubkeyhash' or 'scripthash'
|
|
175
|
+
* @returns {Object} An object with keys: hashBuffer, network and type
|
|
176
|
+
* @private
|
|
177
|
+
*/
|
|
178
|
+
Address._transformBuffer = function (buffer, network, type) {
|
|
179
|
+
var info = {};
|
|
180
|
+
if (!(buffer instanceof Buffer) && !(buffer instanceof Uint8Array)) {
|
|
181
|
+
throw new TypeError('Address supplied is not a buffer.');
|
|
182
|
+
}
|
|
183
|
+
if (buffer.length !== 1 + 20) {
|
|
184
|
+
throw new TypeError('Address buffers must be exactly 21 bytes.');
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
var networkObj = Networks.get(network);
|
|
188
|
+
var bufferVersion = Address._classifyFromVersion(buffer);
|
|
189
|
+
|
|
190
|
+
if (network && !networkObj) {
|
|
191
|
+
throw new TypeError('Unknown network');
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (!bufferVersion.network || (networkObj && networkObj !== bufferVersion.network)) {
|
|
195
|
+
// console.log(bufferVersion)
|
|
196
|
+
throw new TypeError('Address has mismatched network type.');
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
if (!bufferVersion.type || (type && type !== bufferVersion.type)) {
|
|
200
|
+
throw new TypeError('Address has mismatched type.');
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
info.hashBuffer = buffer.slice(1);
|
|
204
|
+
info.network = bufferVersion.network;
|
|
205
|
+
info.type = bufferVersion.type;
|
|
206
|
+
return info;
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Internal function to transform a {@link PublicKey}
|
|
211
|
+
*
|
|
212
|
+
* @param {PublicKey} pubkey - An instance of PublicKey
|
|
213
|
+
* @returns {Object} An object with keys: hashBuffer, type
|
|
214
|
+
* @private
|
|
215
|
+
*/
|
|
216
|
+
Address._transformPublicKey = function (pubkey) {
|
|
217
|
+
var info = {};
|
|
218
|
+
if (!(pubkey instanceof PublicKey)) {
|
|
219
|
+
throw new TypeError('Address must be an instance of PublicKey.');
|
|
220
|
+
}
|
|
221
|
+
info.hashBuffer = Hash.sha256ripemd160(pubkey.toBuffer());
|
|
222
|
+
info.type = Address.PayToPublicKeyHash;
|
|
223
|
+
return info;
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Internal function to transform a {@link Script} into a `info` object.
|
|
228
|
+
*
|
|
229
|
+
* @param {Script} script - An instance of Script
|
|
230
|
+
* @returns {Object} An object with keys: hashBuffer, type
|
|
231
|
+
* @private
|
|
232
|
+
*/
|
|
233
|
+
Address._transformScript = function (script, network) {
|
|
234
|
+
var Script = require('./script');
|
|
235
|
+
$.checkArgument(script instanceof Script, 'script must be a Script instance');
|
|
236
|
+
var info = script.getAddressInfo(network);
|
|
237
|
+
if (!info) {
|
|
238
|
+
throw new errors.Script.CantDeriveAddress(script);
|
|
239
|
+
}
|
|
240
|
+
return info;
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Internal function to transform a bitcoin cash address string
|
|
245
|
+
*
|
|
246
|
+
* @param {string} data
|
|
247
|
+
* @param {String|Network=} network - either a Network instance, 'livenet', or 'testnet'
|
|
248
|
+
* @param {string=} type - The type: 'pubkeyhash'
|
|
249
|
+
* @returns {Object} An object with keys: hashBuffer, network and type
|
|
250
|
+
* @private
|
|
251
|
+
*/
|
|
252
|
+
Address._transformString = function (data, network, type) {
|
|
253
|
+
if (typeof data !== 'string') {
|
|
254
|
+
throw new TypeError('data parameter supplied is not a string.');
|
|
255
|
+
}
|
|
256
|
+
if (data.length < 27) {
|
|
257
|
+
throw new Error('Invalid Address string provided');
|
|
258
|
+
}
|
|
259
|
+
data = data.trim();
|
|
260
|
+
var networkObj = Networks.get(network);
|
|
261
|
+
|
|
262
|
+
if (network && !networkObj) {
|
|
263
|
+
throw new TypeError('Unknown network');
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
var addressBuffer = Base58Check.decode(data);
|
|
267
|
+
return Address._transformBuffer(addressBuffer, network, type);
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Instantiate an address from a PublicKey instance
|
|
272
|
+
*
|
|
273
|
+
* @param {PublicKey} data
|
|
274
|
+
* @param {String|Network} network - either a Network instance, 'livenet', or 'testnet'
|
|
275
|
+
* @returns {Address} A new valid and frozen instance of an Address
|
|
276
|
+
*/
|
|
277
|
+
Address.fromPublicKey = function (data, network) {
|
|
278
|
+
var info = Address._transformPublicKey(data);
|
|
279
|
+
network = network || Networks.defaultNetwork;
|
|
280
|
+
return new Address(info.hashBuffer, network, info.type);
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Instantiate an address from a PrivateKey instance
|
|
285
|
+
*
|
|
286
|
+
* @param {PrivateKey} privateKey
|
|
287
|
+
* @param {String|Network} network - either a Network instance, 'livenet', or 'testnet'
|
|
288
|
+
* @returns {Address} A new valid and frozen instance of an Address
|
|
289
|
+
*/
|
|
290
|
+
Address.fromPrivateKey = function (privateKey, network) {
|
|
291
|
+
let publicKey = PublicKey.fromPrivateKey(privateKey);
|
|
292
|
+
network = network || privateKey.network || Networks.defaultNetwork;
|
|
293
|
+
return Address.fromPublicKey(publicKey, network);
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Instantiate an address from a ripemd160 public key hash
|
|
298
|
+
*
|
|
299
|
+
* @param {Buffer} hash - An instance of buffer of the hash
|
|
300
|
+
* @param {String|Network} network - either a Network instance, 'livenet', or 'testnet'
|
|
301
|
+
* @returns {Address} A new valid and frozen instance of an Address
|
|
302
|
+
*/
|
|
303
|
+
Address.fromPublicKeyHash = function (hash, network) {
|
|
304
|
+
var info = Address._transformHash(hash);
|
|
305
|
+
return new Address(info.hashBuffer, network, Address.PayToPublicKeyHash);
|
|
306
|
+
};
|
|
307
|
+
/**
|
|
308
|
+
* Extract address from a Script. The script must be of one
|
|
309
|
+
* of the following types: p2pkh input, p2pkh output, p2sh input
|
|
310
|
+
* or p2sh output.
|
|
311
|
+
* This will analyze the script and extract address information from it.
|
|
312
|
+
* If you want to transform any script to a p2sh Address paying
|
|
313
|
+
* to that script's hash instead, use {{Address#payingTo}}
|
|
314
|
+
*
|
|
315
|
+
* @param {Script} script - An instance of Script
|
|
316
|
+
* @param {String|Network} network - either a Network instance, 'livenet', or 'testnet'
|
|
317
|
+
* @returns {Address} A new valid and frozen instance of an Address
|
|
318
|
+
*/
|
|
319
|
+
Address.fromScript = function (script, network) {
|
|
320
|
+
var Script = require('./script');
|
|
321
|
+
$.checkArgument(script instanceof Script, 'script must be a Script instance');
|
|
322
|
+
var info = Address._transformScript(script, network);
|
|
323
|
+
return new Address(info.hashBuffer, network, info.type);
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Instantiate an address from a buffer of the address
|
|
328
|
+
*
|
|
329
|
+
* @param {Buffer} buffer - An instance of buffer of the address
|
|
330
|
+
* @param {String|Network=} network - either a Network instance, 'livenet', or 'testnet'
|
|
331
|
+
* @param {string=} type - The type of address: 'pubkey'
|
|
332
|
+
* @returns {Address} A new valid and frozen instance of an Address
|
|
333
|
+
*/
|
|
334
|
+
Address.fromBuffer = function (buffer, network, type) {
|
|
335
|
+
var info = Address._transformBuffer(buffer, network, type);
|
|
336
|
+
return new Address(info.hashBuffer, info.network, info.type);
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
Address.fromHex = function (hex, network, type) {
|
|
340
|
+
return Address.fromBuffer(Buffer.from(hex, 'hex'), network, type);
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* Instantiate an address from an address string
|
|
345
|
+
*
|
|
346
|
+
* @param {string} str - An string of the bitcoin address
|
|
347
|
+
* @param {String|Network=} network - either a Network instance, 'livenet', or 'testnet'
|
|
348
|
+
* @param {string=} type - The type of address: 'pubkey'
|
|
349
|
+
* @returns {Address} A new valid and frozen instance of an Address
|
|
350
|
+
*/
|
|
351
|
+
Address.fromString = function (str, network, type) {
|
|
352
|
+
var info = Address._transformString(str, network, type);
|
|
353
|
+
return new Address(info.hashBuffer, info.network, info.type);
|
|
354
|
+
};
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* Instantiate an address from an Object
|
|
358
|
+
*
|
|
359
|
+
* @param {string} json - An JSON string or Object with keys: hash, network and type
|
|
360
|
+
* @returns {Address} A new valid instance of an Address
|
|
361
|
+
*/
|
|
362
|
+
Address.fromObject = function fromObject(obj) {
|
|
363
|
+
$.checkState(
|
|
364
|
+
JSUtil.isHexa(obj.hash),
|
|
365
|
+
'Unexpected hash property, "' + obj.hash + '", expected to be hex.',
|
|
366
|
+
);
|
|
367
|
+
var hashBuffer = Buffer.from(obj.hash, 'hex');
|
|
368
|
+
return new Address(hashBuffer, obj.network, obj.type);
|
|
369
|
+
};
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* Will return a validation error if exists
|
|
373
|
+
*
|
|
374
|
+
* @example
|
|
375
|
+
* ```javascript
|
|
376
|
+
* // a network mismatch error
|
|
377
|
+
* var error = Address.getValidationError('15vkcKf7gB23wLAnZLmbVuMiiVDc1Nm4a2', 'testnet');
|
|
378
|
+
* ```
|
|
379
|
+
*
|
|
380
|
+
* @param {string} data - The encoded data
|
|
381
|
+
* @param {String|Network} network - either a Network instance, 'livenet', or 'testnet'
|
|
382
|
+
* @param {string} type - The type of address: 'pubkey'
|
|
383
|
+
* @returns {null|Error} The corresponding error message
|
|
384
|
+
*/
|
|
385
|
+
Address.getValidationError = function (data, network, type) {
|
|
386
|
+
var error;
|
|
387
|
+
try {
|
|
388
|
+
new Address(data, network, type);
|
|
389
|
+
} catch (e) {
|
|
390
|
+
error = e;
|
|
391
|
+
}
|
|
392
|
+
return error;
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* Will return a boolean if an address is valid
|
|
397
|
+
*
|
|
398
|
+
* @example
|
|
399
|
+
* ```javascript
|
|
400
|
+
* assert(Address.isValid('15vkcKf7gB23wLAnZLmbVuMiiVDc1Nm4a2', 'livenet'));
|
|
401
|
+
* ```
|
|
402
|
+
*
|
|
403
|
+
* @param {string} data - The encoded data
|
|
404
|
+
* @param {String|Network} network - either a Network instance, 'livenet', or 'testnet'
|
|
405
|
+
* @param {string} type - The type of address: 'pubkey'
|
|
406
|
+
* @returns {boolean} The corresponding error message
|
|
407
|
+
*/
|
|
408
|
+
Address.isValid = function (data, network, type) {
|
|
409
|
+
return !Address.getValidationError(data, network, type);
|
|
410
|
+
};
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Returns true if an address is of pay to public key hash type
|
|
414
|
+
* @return boolean
|
|
415
|
+
*/
|
|
416
|
+
Address.prototype.isPayToPublicKeyHash = function () {
|
|
417
|
+
return this.type === Address.PayToPublicKeyHash;
|
|
418
|
+
};
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* Will return a buffer representation of the address
|
|
422
|
+
*
|
|
423
|
+
* @returns {Buffer} Bitcoin address buffer
|
|
424
|
+
*/
|
|
425
|
+
Address.prototype.toBuffer = function () {
|
|
426
|
+
var version = Buffer.from([this.network[this.type]]);
|
|
427
|
+
var buf = Buffer.concat([version, this.hashBuffer]);
|
|
428
|
+
return buf;
|
|
429
|
+
};
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* Converts the address to a hexadecimal string representation.
|
|
433
|
+
* @returns {string} The hexadecimal string representation of the address.
|
|
434
|
+
*/
|
|
435
|
+
Address.prototype.toHex = function () {
|
|
436
|
+
return this.toBuffer().toString('hex');
|
|
437
|
+
};
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* Converts the address to a publickey hash string representation.
|
|
441
|
+
* @returns {string} The hexadecimal string of the publickey hash buffer.
|
|
442
|
+
*/
|
|
443
|
+
Address.prototype.toPublickeyHash = function () {
|
|
444
|
+
return this.hashBuffer.toString('hex');
|
|
445
|
+
};
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* @returns {Object} A plain object with the address information
|
|
449
|
+
*/
|
|
450
|
+
Address.prototype.toObject = Address.prototype.toJSON = function toObject() {
|
|
451
|
+
return {
|
|
452
|
+
hash: this.hashBuffer.toString('hex'),
|
|
453
|
+
type: this.type,
|
|
454
|
+
network: this.network.toString(),
|
|
455
|
+
};
|
|
456
|
+
};
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* Will return a string formatted for the console
|
|
460
|
+
*
|
|
461
|
+
* @returns {string} Bitcoin address
|
|
462
|
+
*/
|
|
463
|
+
Address.prototype.inspect = function () {
|
|
464
|
+
return (
|
|
465
|
+
'<Address: ' + this.toString() + ', type: ' + this.type + ', network: ' + this.network + '>'
|
|
466
|
+
);
|
|
467
|
+
};
|
|
468
|
+
|
|
469
|
+
/**
|
|
470
|
+
* Will return a the base58 string representation of the address
|
|
471
|
+
*
|
|
472
|
+
* @returns {string} Bitcoin address
|
|
473
|
+
*/
|
|
474
|
+
Address.prototype.toString = function () {
|
|
475
|
+
return Base58Check.encode(this.toBuffer());
|
|
476
|
+
};
|
|
477
|
+
|
|
478
|
+
module.exports = Address;
|