xpi-ts 0.2.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.
Files changed (216) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +516 -0
  3. package/dist/index.d.ts +9 -0
  4. package/dist/index.d.ts.map +1 -0
  5. package/dist/index.js +8 -0
  6. package/dist/lib/bitcore/address.d.ts +66 -0
  7. package/dist/lib/bitcore/address.d.ts.map +1 -0
  8. package/dist/lib/bitcore/address.js +407 -0
  9. package/dist/lib/bitcore/block/block.d.ts +57 -0
  10. package/dist/lib/bitcore/block/block.d.ts.map +1 -0
  11. package/dist/lib/bitcore/block/block.js +233 -0
  12. package/dist/lib/bitcore/block/blockheader.d.ts +82 -0
  13. package/dist/lib/bitcore/block/blockheader.d.ts.map +1 -0
  14. package/dist/lib/bitcore/block/blockheader.js +323 -0
  15. package/dist/lib/bitcore/block/index.d.ts +5 -0
  16. package/dist/lib/bitcore/block/index.d.ts.map +1 -0
  17. package/dist/lib/bitcore/block/index.js +2 -0
  18. package/dist/lib/bitcore/chunk.d.ts +22 -0
  19. package/dist/lib/bitcore/chunk.d.ts.map +1 -0
  20. package/dist/lib/bitcore/chunk.js +46 -0
  21. package/dist/lib/bitcore/crypto/bn.d.ts +53 -0
  22. package/dist/lib/bitcore/crypto/bn.d.ts.map +1 -0
  23. package/dist/lib/bitcore/crypto/bn.js +238 -0
  24. package/dist/lib/bitcore/crypto/ecdsa.d.ts +46 -0
  25. package/dist/lib/bitcore/crypto/ecdsa.d.ts.map +1 -0
  26. package/dist/lib/bitcore/crypto/ecdsa.js +247 -0
  27. package/dist/lib/bitcore/crypto/hash.d.ts +16 -0
  28. package/dist/lib/bitcore/crypto/hash.d.ts.map +1 -0
  29. package/dist/lib/bitcore/crypto/hash.js +87 -0
  30. package/dist/lib/bitcore/crypto/index.d.ts +9 -0
  31. package/dist/lib/bitcore/crypto/index.d.ts.map +1 -0
  32. package/dist/lib/bitcore/crypto/index.js +8 -0
  33. package/dist/lib/bitcore/crypto/musig2.d.ts +40 -0
  34. package/dist/lib/bitcore/crypto/musig2.d.ts.map +1 -0
  35. package/dist/lib/bitcore/crypto/musig2.js +236 -0
  36. package/dist/lib/bitcore/crypto/point.d.ts +20 -0
  37. package/dist/lib/bitcore/crypto/point.d.ts.map +1 -0
  38. package/dist/lib/bitcore/crypto/point.js +133 -0
  39. package/dist/lib/bitcore/crypto/random.d.ts +7 -0
  40. package/dist/lib/bitcore/crypto/random.d.ts.map +1 -0
  41. package/dist/lib/bitcore/crypto/random.js +30 -0
  42. package/dist/lib/bitcore/crypto/schnorr.d.ts +40 -0
  43. package/dist/lib/bitcore/crypto/schnorr.d.ts.map +1 -0
  44. package/dist/lib/bitcore/crypto/schnorr.js +185 -0
  45. package/dist/lib/bitcore/crypto/signature.d.ts +53 -0
  46. package/dist/lib/bitcore/crypto/signature.d.ts.map +1 -0
  47. package/dist/lib/bitcore/crypto/signature.js +300 -0
  48. package/dist/lib/bitcore/crypto/sigtype.d.ts +5 -0
  49. package/dist/lib/bitcore/crypto/sigtype.d.ts.map +1 -0
  50. package/dist/lib/bitcore/crypto/sigtype.js +18 -0
  51. package/dist/lib/bitcore/encoding/base58.d.ts +16 -0
  52. package/dist/lib/bitcore/encoding/base58.d.ts.map +1 -0
  53. package/dist/lib/bitcore/encoding/base58.js +55 -0
  54. package/dist/lib/bitcore/encoding/base58check.d.ts +9 -0
  55. package/dist/lib/bitcore/encoding/base58check.d.ts.map +1 -0
  56. package/dist/lib/bitcore/encoding/base58check.js +82 -0
  57. package/dist/lib/bitcore/encoding/bufferreader.d.ts +34 -0
  58. package/dist/lib/bitcore/encoding/bufferreader.d.ts.map +1 -0
  59. package/dist/lib/bitcore/encoding/bufferreader.js +198 -0
  60. package/dist/lib/bitcore/encoding/bufferwriter.d.ts +36 -0
  61. package/dist/lib/bitcore/encoding/bufferwriter.d.ts.map +1 -0
  62. package/dist/lib/bitcore/encoding/bufferwriter.js +189 -0
  63. package/dist/lib/bitcore/encoding/varint.d.ts +20 -0
  64. package/dist/lib/bitcore/encoding/varint.d.ts.map +1 -0
  65. package/dist/lib/bitcore/encoding/varint.js +61 -0
  66. package/dist/lib/bitcore/errors.d.ts +28 -0
  67. package/dist/lib/bitcore/errors.d.ts.map +1 -0
  68. package/dist/lib/bitcore/errors.js +325 -0
  69. package/dist/lib/bitcore/hdprivatekey.d.ts +78 -0
  70. package/dist/lib/bitcore/hdprivatekey.d.ts.map +1 -0
  71. package/dist/lib/bitcore/hdprivatekey.js +381 -0
  72. package/dist/lib/bitcore/hdpublickey.d.ts +98 -0
  73. package/dist/lib/bitcore/hdpublickey.d.ts.map +1 -0
  74. package/dist/lib/bitcore/hdpublickey.js +416 -0
  75. package/dist/lib/bitcore/index.d.ts +60 -0
  76. package/dist/lib/bitcore/index.d.ts.map +1 -0
  77. package/dist/lib/bitcore/index.js +44 -0
  78. package/dist/lib/bitcore/message.d.ts +23 -0
  79. package/dist/lib/bitcore/message.d.ts.map +1 -0
  80. package/dist/lib/bitcore/message.js +112 -0
  81. package/dist/lib/bitcore/mnemonic/errors.d.ts +7 -0
  82. package/dist/lib/bitcore/mnemonic/errors.d.ts.map +1 -0
  83. package/dist/lib/bitcore/mnemonic/errors.js +20 -0
  84. package/dist/lib/bitcore/mnemonic/index.d.ts +5 -0
  85. package/dist/lib/bitcore/mnemonic/index.d.ts.map +1 -0
  86. package/dist/lib/bitcore/mnemonic/index.js +4 -0
  87. package/dist/lib/bitcore/mnemonic/mnemonic.d.ts +23 -0
  88. package/dist/lib/bitcore/mnemonic/mnemonic.d.ts.map +1 -0
  89. package/dist/lib/bitcore/mnemonic/mnemonic.js +164 -0
  90. package/dist/lib/bitcore/mnemonic/pbkdf2.d.ts +2 -0
  91. package/dist/lib/bitcore/mnemonic/pbkdf2.d.ts.map +1 -0
  92. package/dist/lib/bitcore/mnemonic/pbkdf2.js +25 -0
  93. package/dist/lib/bitcore/mnemonic/words/english.d.ts +2 -0
  94. package/dist/lib/bitcore/mnemonic/words/english.d.ts.map +1 -0
  95. package/dist/lib/bitcore/mnemonic/words/english.js +2050 -0
  96. package/dist/lib/bitcore/mnemonic/words/index.d.ts +4 -0
  97. package/dist/lib/bitcore/mnemonic/words/index.d.ts.map +1 -0
  98. package/dist/lib/bitcore/mnemonic/words/index.js +4 -0
  99. package/dist/lib/bitcore/musig2/index.d.ts +3 -0
  100. package/dist/lib/bitcore/musig2/index.d.ts.map +1 -0
  101. package/dist/lib/bitcore/musig2/index.js +2 -0
  102. package/dist/lib/bitcore/musig2/session.d.ts +79 -0
  103. package/dist/lib/bitcore/musig2/session.d.ts.map +1 -0
  104. package/dist/lib/bitcore/musig2/session.js +346 -0
  105. package/dist/lib/bitcore/musig2/signer.d.ts +61 -0
  106. package/dist/lib/bitcore/musig2/signer.d.ts.map +1 -0
  107. package/dist/lib/bitcore/musig2/signer.js +146 -0
  108. package/dist/lib/bitcore/networks.d.ts +53 -0
  109. package/dist/lib/bitcore/networks.d.ts.map +1 -0
  110. package/dist/lib/bitcore/networks.js +150 -0
  111. package/dist/lib/bitcore/opcode.d.ts +250 -0
  112. package/dist/lib/bitcore/opcode.d.ts.map +1 -0
  113. package/dist/lib/bitcore/opcode.js +270 -0
  114. package/dist/lib/bitcore/privatekey.d.ts +56 -0
  115. package/dist/lib/bitcore/privatekey.d.ts.map +1 -0
  116. package/dist/lib/bitcore/privatekey.js +237 -0
  117. package/dist/lib/bitcore/publickey.d.ts +59 -0
  118. package/dist/lib/bitcore/publickey.d.ts.map +1 -0
  119. package/dist/lib/bitcore/publickey.js +263 -0
  120. package/dist/lib/bitcore/script/interpreter.d.ts +98 -0
  121. package/dist/lib/bitcore/script/interpreter.d.ts.map +1 -0
  122. package/dist/lib/bitcore/script/interpreter.js +1704 -0
  123. package/dist/lib/bitcore/script.d.ts +111 -0
  124. package/dist/lib/bitcore/script.d.ts.map +1 -0
  125. package/dist/lib/bitcore/script.js +1112 -0
  126. package/dist/lib/bitcore/taproot/musig2.d.ts +29 -0
  127. package/dist/lib/bitcore/taproot/musig2.d.ts.map +1 -0
  128. package/dist/lib/bitcore/taproot/musig2.js +104 -0
  129. package/dist/lib/bitcore/taproot/nft.d.ts +164 -0
  130. package/dist/lib/bitcore/taproot/nft.d.ts.map +1 -0
  131. package/dist/lib/bitcore/taproot/nft.js +407 -0
  132. package/dist/lib/bitcore/taproot.d.ts +65 -0
  133. package/dist/lib/bitcore/taproot.d.ts.map +1 -0
  134. package/dist/lib/bitcore/taproot.js +288 -0
  135. package/dist/lib/bitcore/transaction/index.d.ts +12 -0
  136. package/dist/lib/bitcore/transaction/index.d.ts.map +1 -0
  137. package/dist/lib/bitcore/transaction/index.js +6 -0
  138. package/dist/lib/bitcore/transaction/input.d.ts +202 -0
  139. package/dist/lib/bitcore/transaction/input.d.ts.map +1 -0
  140. package/dist/lib/bitcore/transaction/input.js +911 -0
  141. package/dist/lib/bitcore/transaction/output.d.ts +48 -0
  142. package/dist/lib/bitcore/transaction/output.d.ts.map +1 -0
  143. package/dist/lib/bitcore/transaction/output.js +231 -0
  144. package/dist/lib/bitcore/transaction/sighash.d.ts +32 -0
  145. package/dist/lib/bitcore/transaction/sighash.d.ts.map +1 -0
  146. package/dist/lib/bitcore/transaction/sighash.js +335 -0
  147. package/dist/lib/bitcore/transaction/signature.d.ts +36 -0
  148. package/dist/lib/bitcore/transaction/signature.d.ts.map +1 -0
  149. package/dist/lib/bitcore/transaction/signature.js +130 -0
  150. package/dist/lib/bitcore/transaction/transaction.d.ts +164 -0
  151. package/dist/lib/bitcore/transaction/transaction.d.ts.map +1 -0
  152. package/dist/lib/bitcore/transaction/transaction.js +1016 -0
  153. package/dist/lib/bitcore/transaction/unspentoutput.d.ts +58 -0
  154. package/dist/lib/bitcore/transaction/unspentoutput.d.ts.map +1 -0
  155. package/dist/lib/bitcore/transaction/unspentoutput.js +167 -0
  156. package/dist/lib/bitcore/unit.d.ts +44 -0
  157. package/dist/lib/bitcore/unit.d.ts.map +1 -0
  158. package/dist/lib/bitcore/unit.js +106 -0
  159. package/dist/lib/bitcore/uri.d.ts +29 -0
  160. package/dist/lib/bitcore/uri.d.ts.map +1 -0
  161. package/dist/lib/bitcore/uri.js +163 -0
  162. package/dist/lib/bitcore/util/base32.d.ts +5 -0
  163. package/dist/lib/bitcore/util/base32.d.ts.map +1 -0
  164. package/dist/lib/bitcore/util/base32.js +58 -0
  165. package/dist/lib/bitcore/util/buffer.d.ts +18 -0
  166. package/dist/lib/bitcore/util/buffer.d.ts.map +1 -0
  167. package/dist/lib/bitcore/util/buffer.js +76 -0
  168. package/dist/lib/bitcore/util/convertBits.d.ts +2 -0
  169. package/dist/lib/bitcore/util/convertBits.d.ts.map +1 -0
  170. package/dist/lib/bitcore/util/convertBits.js +26 -0
  171. package/dist/lib/bitcore/util/js.d.ts +9 -0
  172. package/dist/lib/bitcore/util/js.d.ts.map +1 -0
  173. package/dist/lib/bitcore/util/js.js +45 -0
  174. package/dist/lib/bitcore/util/preconditions.d.ts +6 -0
  175. package/dist/lib/bitcore/util/preconditions.d.ts.map +1 -0
  176. package/dist/lib/bitcore/util/preconditions.js +31 -0
  177. package/dist/lib/bitcore/util.d.ts +14 -0
  178. package/dist/lib/bitcore/util.d.ts.map +1 -0
  179. package/dist/lib/bitcore/util.js +13 -0
  180. package/dist/lib/bitcore/xaddress.d.ts +45 -0
  181. package/dist/lib/bitcore/xaddress.d.ts.map +1 -0
  182. package/dist/lib/bitcore/xaddress.js +279 -0
  183. package/dist/lib/rank/api.d.ts +75 -0
  184. package/dist/lib/rank/api.d.ts.map +1 -0
  185. package/dist/lib/rank/api.js +4 -0
  186. package/dist/lib/rank/index.d.ts +127 -0
  187. package/dist/lib/rank/index.d.ts.map +1 -0
  188. package/dist/lib/rank/index.js +421 -0
  189. package/dist/lib/rank/opcode.d.ts +23 -0
  190. package/dist/lib/rank/opcode.d.ts.map +1 -0
  191. package/dist/lib/rank/opcode.js +23 -0
  192. package/dist/lib/rank/script.d.ts +2 -0
  193. package/dist/lib/rank/script.d.ts.map +1 -0
  194. package/dist/lib/rank/script.js +7 -0
  195. package/dist/lib/rank/transaction.d.ts +3 -0
  196. package/dist/lib/rank/transaction.d.ts.map +1 -0
  197. package/dist/lib/rank/transaction.js +12 -0
  198. package/dist/lib/rpc.d.ts +136 -0
  199. package/dist/lib/rpc.d.ts.map +1 -0
  200. package/dist/lib/rpc.js +62 -0
  201. package/dist/utils/constants.d.ts +18 -0
  202. package/dist/utils/constants.d.ts.map +1 -0
  203. package/dist/utils/constants.js +20 -0
  204. package/dist/utils/env.d.ts +3 -0
  205. package/dist/utils/env.d.ts.map +1 -0
  206. package/dist/utils/env.js +8 -0
  207. package/dist/utils/string.d.ts +11 -0
  208. package/dist/utils/string.d.ts.map +1 -0
  209. package/dist/utils/string.js +47 -0
  210. package/dist/utils/types.d.ts +2 -0
  211. package/dist/utils/types.d.ts.map +1 -0
  212. package/dist/utils/types.js +1 -0
  213. package/dist/utils/wallet.d.ts +12 -0
  214. package/dist/utils/wallet.d.ts.map +1 -0
  215. package/dist/utils/wallet.js +28 -0
  216. package/package.json +91 -0
@@ -0,0 +1,407 @@
1
+ import { Preconditions } from './util/preconditions.js';
2
+ import { BitcoreError } from './errors.js';
3
+ import { Base58Check } from './encoding/base58check.js';
4
+ import { get as getNetwork, defaultNetwork } from './networks.js';
5
+ import { Hash } from './crypto/hash.js';
6
+ import { JSUtil } from './util/js.js';
7
+ import { PublicKey } from './publickey.js';
8
+ import { XAddress } from './xaddress.js';
9
+ import { Script } from './script.js';
10
+ export class Address {
11
+ static PayToPublicKeyHash = 'pubkeyhash';
12
+ static PayToScriptHash = 'scripthash';
13
+ static PayToTaproot = 'taproot';
14
+ hashBuffer;
15
+ network;
16
+ type;
17
+ constructor(data, network, type) {
18
+ if (Array.isArray(data) && typeof network === 'number') {
19
+ return Address.createMultisig(data, network, type);
20
+ }
21
+ if (data instanceof Address) {
22
+ return data;
23
+ }
24
+ Preconditions.checkArgument(data !== undefined, 'data', 'First argument is required, please include address data.', 'guide/address.html');
25
+ if (network && !getNetwork(network)) {
26
+ throw new TypeError('Second argument must be "livenet", "testnet", or "regtest".');
27
+ }
28
+ const networkExplicitlyProvided = network !== undefined;
29
+ network ||= defaultNetwork;
30
+ if (type &&
31
+ type !== Address.PayToPublicKeyHash &&
32
+ type !== Address.PayToScriptHash &&
33
+ type !== Address.PayToTaproot) {
34
+ throw new TypeError('Third argument must be "pubkeyhash", "scripthash", or "taproot".');
35
+ }
36
+ const info = this._classifyArguments(data, network, type, networkExplicitlyProvided);
37
+ info.network = info.network || getNetwork(network) || defaultNetwork;
38
+ info.type = info.type || type || Address.PayToPublicKeyHash;
39
+ JSUtil.defineImmutable(this, {
40
+ hashBuffer: info.hashBuffer,
41
+ network: info.network,
42
+ type: info.type,
43
+ });
44
+ }
45
+ _classifyArguments(data, network, type, networkExplicitlyProvided = true) {
46
+ if (typeof network === 'string') {
47
+ const networkObj = getNetwork(network);
48
+ if (!networkObj) {
49
+ throw new TypeError('Unknown network');
50
+ }
51
+ network = networkObj;
52
+ }
53
+ if ((Buffer.isBuffer(data) || data instanceof Uint8Array) &&
54
+ data.length === 20) {
55
+ return Address._transformHash(data);
56
+ }
57
+ else if ((Buffer.isBuffer(data) || data instanceof Uint8Array) &&
58
+ data.length === 21) {
59
+ return Address._transformBuffer(data, network, type);
60
+ }
61
+ else if ((Buffer.isBuffer(data) || data instanceof Uint8Array) &&
62
+ data.length === 33) {
63
+ return {
64
+ hashBuffer: Buffer.from(data),
65
+ network: typeof network === 'string' ? getNetwork(network) : network,
66
+ type: type || Address.PayToTaproot,
67
+ };
68
+ }
69
+ else if (data instanceof PublicKey) {
70
+ return Address._transformPublicKey(data, network);
71
+ }
72
+ else if (data instanceof Script) {
73
+ return Address._transformScript(data, network);
74
+ }
75
+ else if (typeof data === 'string') {
76
+ return Address._transformString(data, networkExplicitlyProvided ? network : undefined, type);
77
+ }
78
+ else if (Array.isArray(data)) {
79
+ throw new Error('Multisig addresses should be created with createMultisig');
80
+ }
81
+ else if (typeof data === 'object' && data !== null) {
82
+ return Address._transformObject(data);
83
+ }
84
+ else {
85
+ throw new TypeError('First argument is an unrecognized data format.');
86
+ }
87
+ }
88
+ static _classifyFromVersion(buffer) {
89
+ const version = {};
90
+ const pubkeyhashNetwork = getNetwork(buffer[0], 'pubkeyhash');
91
+ const scripthashNetwork = getNetwork(buffer[0], 'scripthash');
92
+ if (pubkeyhashNetwork) {
93
+ version.network = pubkeyhashNetwork;
94
+ version.type = Address.PayToPublicKeyHash;
95
+ }
96
+ else if (scripthashNetwork) {
97
+ version.network = scripthashNetwork;
98
+ version.type = Address.PayToScriptHash;
99
+ }
100
+ return version;
101
+ }
102
+ static _transformString(data, network, type) {
103
+ if (typeof data !== 'string') {
104
+ throw new TypeError('data parameter supplied is not a string.');
105
+ }
106
+ data = data.trim();
107
+ const networkObj = getNetwork(network);
108
+ if (network && !networkObj) {
109
+ throw new TypeError('Unknown network');
110
+ }
111
+ if (data.indexOf(':') !== -1) {
112
+ const info = Address.decodeCashAddress(data);
113
+ if (!info.network ||
114
+ (networkObj && networkObj.name !== info.network.name)) {
115
+ throw new TypeError('Address has mismatched network type.');
116
+ }
117
+ return {
118
+ hashBuffer: Buffer.from(info.hashBuffer),
119
+ network: info.network,
120
+ type: info.type,
121
+ };
122
+ }
123
+ if (Address._isXAddress(data)) {
124
+ const info = Address._transformXAddressString(data, network, type);
125
+ if (!info.network ||
126
+ (networkObj && networkObj.name !== info.network.name)) {
127
+ throw new TypeError('Address has mismatched network type.');
128
+ }
129
+ return info;
130
+ }
131
+ const info = Address._transformLegacyString(data, network, type);
132
+ if (!info.network ||
133
+ (networkObj && networkObj.name !== info.network.name)) {
134
+ throw new TypeError('Address has mismatched network type.');
135
+ }
136
+ return info;
137
+ }
138
+ static _transformLegacyString(data, network, type) {
139
+ const info = {};
140
+ const decoded = Base58Check.decode(data);
141
+ const version = Address._classifyFromVersion(decoded);
142
+ if (!version.network || !version.type) {
143
+ throw new TypeError('Address has invalid version.');
144
+ }
145
+ info.hashBuffer = decoded.subarray(1);
146
+ info.network = version.network;
147
+ info.type = version.type;
148
+ return info;
149
+ }
150
+ static _isXAddress(data) {
151
+ const match = /[A-Z]|_/.exec(data);
152
+ return match !== null && match.index > 0;
153
+ }
154
+ static _transformXAddressString(data, network, type) {
155
+ if (typeof network === 'string') {
156
+ network = getNetwork(network);
157
+ }
158
+ network ||= defaultNetwork;
159
+ const decodedXAddress = XAddress._decode(data);
160
+ if (!decodedXAddress.hashBuffer) {
161
+ throw new TypeError('Invalid XAddress.');
162
+ }
163
+ let hashBuffer = decodedXAddress.hashBuffer;
164
+ const decodedNetwork = decodedXAddress.network || network;
165
+ const decodedType = decodedXAddress.type;
166
+ if (decodedType === 'taproot' || decodedType === Address.PayToTaproot) {
167
+ if (hashBuffer.length === 36 &&
168
+ hashBuffer[0] === 0x62 &&
169
+ hashBuffer[1] === 0x51 &&
170
+ hashBuffer[2] === 0x21) {
171
+ hashBuffer = hashBuffer.subarray(3, 36);
172
+ }
173
+ else if (hashBuffer.length === 33) {
174
+ }
175
+ else {
176
+ throw new TypeError(`Taproot address has invalid payload length: ${hashBuffer.length} bytes (expected 33 or 36)`);
177
+ }
178
+ return {
179
+ hashBuffer: hashBuffer,
180
+ network: decodedNetwork,
181
+ type: Address.PayToTaproot,
182
+ };
183
+ }
184
+ if (hashBuffer.length === 25 &&
185
+ hashBuffer[0] === 0x76 &&
186
+ hashBuffer[1] === 0xa9 &&
187
+ hashBuffer[2] === 0x14) {
188
+ hashBuffer = hashBuffer.subarray(3, 23);
189
+ }
190
+ else if (hashBuffer.length === 23 &&
191
+ hashBuffer[0] === 0xa9 &&
192
+ hashBuffer[1] === 0x14 &&
193
+ hashBuffer[22] === 0x87) {
194
+ hashBuffer = hashBuffer.subarray(2, 22);
195
+ return {
196
+ hashBuffer: hashBuffer,
197
+ network: decodedNetwork,
198
+ type: Address.PayToScriptHash,
199
+ };
200
+ }
201
+ return {
202
+ hashBuffer: hashBuffer,
203
+ network: decodedNetwork,
204
+ type: type ?? Address.PayToPublicKeyHash,
205
+ };
206
+ }
207
+ static _transformHash(hash) {
208
+ const info = {};
209
+ if (!Buffer.isBuffer(hash) && !(hash instanceof Uint8Array)) {
210
+ throw new TypeError('Address supplied is not a buffer.');
211
+ }
212
+ if (hash.length !== 20) {
213
+ throw new TypeError('Address hashbuffers must be exactly 20 bytes.');
214
+ }
215
+ info.hashBuffer = Buffer.from(hash);
216
+ return info;
217
+ }
218
+ static _transformBuffer(buffer, network, type) {
219
+ const info = {};
220
+ if (!Buffer.isBuffer(buffer) && !(buffer instanceof Uint8Array)) {
221
+ throw new TypeError('Address supplied is not a buffer.');
222
+ }
223
+ if (buffer.length !== 21) {
224
+ throw new TypeError('Address buffers must be exactly 21 bytes.');
225
+ }
226
+ const networkObj = getNetwork(network);
227
+ const bufferVersion = Address._classifyFromVersion(Buffer.from(buffer));
228
+ if (network && !networkObj) {
229
+ throw new TypeError('Unknown network');
230
+ }
231
+ if (!bufferVersion.network ||
232
+ (networkObj && networkObj !== bufferVersion.network)) {
233
+ throw new TypeError('Address has mismatched network type.');
234
+ }
235
+ if (!bufferVersion.type || (type && type !== bufferVersion.type)) {
236
+ throw new TypeError('Address has mismatched type.');
237
+ }
238
+ info.hashBuffer = Buffer.from(buffer).subarray(1);
239
+ info.network = bufferVersion.network;
240
+ info.type = bufferVersion.type;
241
+ return info;
242
+ }
243
+ static _transformPublicKey(pubkey, network) {
244
+ const info = {};
245
+ if (!(pubkey instanceof PublicKey)) {
246
+ throw new TypeError('Address must be an instance of PublicKey.');
247
+ }
248
+ info.hashBuffer = Hash.sha256ripemd160(pubkey.toBuffer());
249
+ info.type = Address.PayToPublicKeyHash;
250
+ info.network = network ?? defaultNetwork;
251
+ return info;
252
+ }
253
+ static _transformScript(script, network) {
254
+ Preconditions.checkArgument(script instanceof Script, 'script', 'script must be a Script instance');
255
+ const address = script.getAddressInfo();
256
+ if (!address) {
257
+ throw new BitcoreError.Script.CantDeriveAddress('Cannot derive address from script');
258
+ }
259
+ if (typeof network === 'string') {
260
+ network = getNetwork(network);
261
+ }
262
+ if (network && network !== address.network) {
263
+ throw new TypeError('Provided network does not match the Address network.');
264
+ }
265
+ return {
266
+ hashBuffer: address.hashBuffer,
267
+ network: address.network,
268
+ type: address.type,
269
+ };
270
+ }
271
+ static _transformObject(data) {
272
+ Preconditions.checkArgument(data.hashBuffer !== undefined, 'data', 'Must provide a `hash` or `hashBuffer` property');
273
+ Preconditions.checkArgument(data.type !== undefined, 'data', 'Must provide a `type` property');
274
+ return {
275
+ hashBuffer: data.hashBuffer || Buffer.from(data.hashBuffer.toString(), 'hex'),
276
+ network: getNetwork(data.network) || defaultNetwork,
277
+ type: data.type,
278
+ };
279
+ }
280
+ static createMultisig(publicKeys, threshold, network) {
281
+ const networkObj = network || publicKeys[0].network || defaultNetwork;
282
+ return Address.payingTo(Script.buildMultisigOut(publicKeys, threshold, {}), networkObj);
283
+ }
284
+ static fromPublicKey(data, network) {
285
+ const networkObj = getNetwork(network) || defaultNetwork;
286
+ const info = Address._transformPublicKey(data, networkObj);
287
+ return new Address(info.hashBuffer, info.network, info.type);
288
+ }
289
+ static fromPublicKeyHash(hash, network) {
290
+ const networkObj = getNetwork(network) || defaultNetwork;
291
+ return new Address(hash, networkObj, Address.PayToPublicKeyHash);
292
+ }
293
+ static fromScriptHash(hash, network) {
294
+ const networkObj = getNetwork(network) || defaultNetwork;
295
+ return new Address(hash, networkObj, Address.PayToScriptHash);
296
+ }
297
+ static fromTaprootCommitment(commitment, network) {
298
+ const networkObj = getNetwork(network) || defaultNetwork;
299
+ const commitmentBuf = commitment instanceof PublicKey ? commitment.toBuffer() : commitment;
300
+ if (commitmentBuf.length !== 33) {
301
+ throw new Error('Taproot commitment must be 33-byte compressed public key');
302
+ }
303
+ return new Address(commitmentBuf, networkObj, Address.PayToTaproot);
304
+ }
305
+ static fromBuffer(buffer, network, type) {
306
+ const info = Address._transformBuffer(buffer, network, type);
307
+ return new Address(info.hashBuffer, info.network, info.type);
308
+ }
309
+ static fromString(str, network) {
310
+ const info = Address._transformString(str, network);
311
+ return new Address(info.hashBuffer, info.network, info.type);
312
+ }
313
+ static fromObject(obj) {
314
+ Preconditions.checkState(JSUtil.isHexa(obj.hash), 'Unexpected hash property, "' + obj.hash + '", expected to be hex.');
315
+ const hashBuffer = Buffer.from(obj.hash, 'hex');
316
+ return new Address(hashBuffer, obj.network, obj.type);
317
+ }
318
+ static fromScript(script, network) {
319
+ Preconditions.checkArgument(script instanceof Script, 'script', 'script must be a Script instance');
320
+ const info = Address._transformScript(script, network);
321
+ return new Address(info.hashBuffer, network, info.type);
322
+ }
323
+ static payingTo(script, network) {
324
+ Preconditions.checkArgument(script !== null, 'script', 'script is required');
325
+ Preconditions.checkArgument(script instanceof Script, 'script', 'script must be instance of Script');
326
+ return Address.fromScriptHash(Hash.sha256ripemd160(script.toBuffer()), network);
327
+ }
328
+ static getValidationError(data, network, type) {
329
+ try {
330
+ new Address(data, network, type);
331
+ return null;
332
+ }
333
+ catch (e) {
334
+ return e;
335
+ }
336
+ }
337
+ static isValid(data, network, type) {
338
+ return !Address.getValidationError(data, network, type);
339
+ }
340
+ isPayToPublicKeyHash() {
341
+ return this.type === Address.PayToPublicKeyHash;
342
+ }
343
+ isPayToScriptHash() {
344
+ return this.type === Address.PayToScriptHash;
345
+ }
346
+ isPayToTaproot() {
347
+ return this.type === Address.PayToTaproot;
348
+ }
349
+ toBuffer() {
350
+ return this.hashBuffer;
351
+ }
352
+ toFullBuffer() {
353
+ const version = Buffer.from([
354
+ this.network[this.type],
355
+ ]);
356
+ const buf = Buffer.concat([version, this.hashBuffer]);
357
+ return buf;
358
+ }
359
+ toCashBuffer() {
360
+ return this.toBuffer();
361
+ }
362
+ toObject() {
363
+ return {
364
+ hash: this.hashBuffer.toString('hex'),
365
+ type: this.type,
366
+ network: this.network.toString(),
367
+ };
368
+ }
369
+ toJSON() {
370
+ return this.toObject();
371
+ }
372
+ toString(network) {
373
+ return this.toXAddress(network);
374
+ }
375
+ toLegacyAddress() {
376
+ return this.toString();
377
+ }
378
+ toCashAddress() {
379
+ return this.toString();
380
+ }
381
+ toXAddress(network) {
382
+ if (this.isPayToTaproot()) {
383
+ const xaddr = new XAddress(this.hashBuffer, network ?? this.network, this.type);
384
+ return xaddr.toString();
385
+ }
386
+ const script = Script.fromAddress(this);
387
+ const xaddr = new XAddress(script.toBuffer(), network ?? this.network, this.type);
388
+ return xaddr.toString();
389
+ }
390
+ static decodeCashAddress(address) {
391
+ const info = Address._transformString(address);
392
+ return {
393
+ network: info.network,
394
+ type: info.type,
395
+ hashBuffer: info.hashBuffer,
396
+ };
397
+ }
398
+ inspect() {
399
+ return ('<Address: ' +
400
+ this.toString() +
401
+ ', type: ' +
402
+ this.type +
403
+ ', network: ' +
404
+ this.network +
405
+ '>');
406
+ }
407
+ }
@@ -0,0 +1,57 @@
1
+ import { BufferReader } from '../encoding/bufferreader.js';
2
+ import { BufferWriter } from '../encoding/bufferwriter.js';
3
+ import { Transaction } from '../transaction/index.js';
4
+ import { BlockHeader, BlockHeaderData, BlockHeaderObject } from './blockheader.js';
5
+ export interface BlockData {
6
+ header?: BlockHeader | BlockHeaderData;
7
+ metadata?: number;
8
+ transactions?: Transaction[];
9
+ }
10
+ export interface BlockObject {
11
+ id: string;
12
+ hash: string;
13
+ header: BlockHeaderObject;
14
+ metadata: number;
15
+ transactions: unknown[];
16
+ }
17
+ export declare class Block {
18
+ static readonly MAX_BLOCK_SIZE: number;
19
+ static readonly START_OF_BLOCK = 0;
20
+ static readonly NULL_HASH: Buffer<ArrayBuffer>;
21
+ static readonly Values: {
22
+ START_OF_BLOCK: number;
23
+ NULL_HASH: Buffer<ArrayBuffer>;
24
+ };
25
+ header: BlockHeader;
26
+ metadata: number;
27
+ transactions: Transaction[];
28
+ private _id?;
29
+ constructor(serialized?: BlockData | Block | Buffer | string);
30
+ static shallowCopy(block: Block): Block;
31
+ private static _from;
32
+ static fromObject(obj: BlockData): Block;
33
+ static fromBufferReader(br: BufferReader): Block;
34
+ static fromBuffer(buf: Buffer): Block;
35
+ static fromString(str: string): Block;
36
+ static fromRawBlock(data: Buffer | string): Block;
37
+ private static _fromObject;
38
+ private static _fromBufferReader;
39
+ private _newBlock;
40
+ fromBuffer(buf: Buffer): Block;
41
+ fromString(str: string): Block;
42
+ fromObject(obj: BlockData): Block;
43
+ toObject(): BlockObject;
44
+ toJSON: () => BlockObject;
45
+ toBuffer(): Buffer;
46
+ toString(): string;
47
+ toBufferWriter(bw?: BufferWriter): BufferWriter;
48
+ getTransactionHashes(): Buffer[];
49
+ getMerkleTree(): Buffer[];
50
+ getMerkleRoot(): Buffer;
51
+ validMerkleRoot(): boolean;
52
+ private _getHash;
53
+ get hash(): string;
54
+ get id(): string;
55
+ inspect(): string;
56
+ }
57
+ //# sourceMappingURL=block.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"block.d.ts","sourceRoot":"","sources":["../../../../lib/bitcore/block/block.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAA;AAC1D,OAAO,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAA;AAG1D,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAA;AAErD,OAAO,EACL,WAAW,EACX,eAAe,EACf,iBAAiB,EAClB,MAAM,kBAAkB,CAAA;AAEzB,MAAM,WAAW,SAAS;IACxB,MAAM,CAAC,EAAE,WAAW,GAAG,eAAe,CAAA;IACtC,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,YAAY,CAAC,EAAE,WAAW,EAAE,CAAA;CAC7B;AAED,MAAM,WAAW,WAAW;IAC1B,EAAE,EAAE,MAAM,CAAA;IACV,IAAI,EAAE,MAAM,CAAA;IACZ,MAAM,EAAE,iBAAiB,CAAA;IACzB,QAAQ,EAAE,MAAM,CAAA;IAChB,YAAY,EAAE,OAAO,EAAE,CAAA;CACxB;AAED,qBAAa,KAAK;IAEhB,MAAM,CAAC,QAAQ,CAAC,cAAc,SAAmB;IACjD,MAAM,CAAC,QAAQ,CAAC,cAAc,KAAI;IAClC,MAAM,CAAC,QAAQ,CAAC,SAAS,sBAGxB;IAGD,MAAM,CAAC,QAAQ,CAAC,MAAM;;;MAMrB;IAGD,MAAM,EAAG,WAAW,CAAA;IACpB,QAAQ,EAAG,MAAM,CAAA;IACjB,YAAY,EAAG,WAAW,EAAE,CAAA;IAC5B,OAAO,CAAC,GAAG,CAAC,CAAQ;gBAER,UAAU,CAAC,EAAE,SAAS,GAAG,KAAK,GAAG,MAAM,GAAG,MAAM;IAsB5D,MAAM,CAAC,WAAW,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK;IAQvC,OAAO,CAAC,MAAM,CAAC,KAAK;IAepB,MAAM,CAAC,UAAU,CAAC,GAAG,EAAE,SAAS,GAAG,KAAK;IAQxC,MAAM,CAAC,gBAAgB,CAAC,EAAE,EAAE,YAAY,GAAG,KAAK;IAShD,MAAM,CAAC,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,KAAK;IAOrC,MAAM,CAAC,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,KAAK;IAQrC,MAAM,CAAC,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,KAAK;IAajD,OAAO,CAAC,MAAM,CAAC,WAAW;IA0B1B,OAAO,CAAC,MAAM,CAAC,iBAAiB;IAuBhC,OAAO,CAAC,SAAS;IASjB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,KAAK;IAW9B,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,KAAK;IAQ9B,UAAU,CAAC,GAAG,EAAE,SAAS,GAAG,KAAK;IAWjC,QAAQ,IAAI,WAAW;IAcvB,MAAM,QAdM,WAAW,CAcD;IAKtB,QAAQ,IAAI,MAAM;IAOlB,QAAQ,IAAI,MAAM;IAOlB,cAAc,CAAC,EAAE,CAAC,EAAE,YAAY,GAAG,YAAY;IAgB/C,oBAAoB,IAAI,MAAM,EAAE;IAoBhC,aAAa,IAAI,MAAM,EAAE;IAsBzB,aAAa,IAAI,MAAM;IAQvB,eAAe,IAAI,OAAO;IAc1B,OAAO,CAAC,QAAQ;IAQhB,IAAI,IAAI,IAAI,MAAM,CAKjB;IAKD,IAAI,EAAE,IAAI,MAAM,CAEf;IAKD,OAAO,IAAI,MAAM;CAGlB"}
@@ -0,0 +1,233 @@
1
+ import { Preconditions } from '../util/preconditions.js';
2
+ import { BufferUtil } from '../util/buffer.js';
3
+ import { BufferReader } from '../encoding/bufferreader.js';
4
+ import { BufferWriter } from '../encoding/bufferwriter.js';
5
+ import { Hash } from '../crypto/hash.js';
6
+ import { JSUtil } from '../util/js.js';
7
+ import { Transaction } from '../transaction/index.js';
8
+ import { BN } from '../crypto/bn.js';
9
+ import { BlockHeader, } from './blockheader.js';
10
+ export class Block {
11
+ static MAX_BLOCK_SIZE = 32 * 1024 * 1024;
12
+ static START_OF_BLOCK = 0;
13
+ static NULL_HASH = Buffer.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex');
14
+ static Values = {
15
+ START_OF_BLOCK: 0,
16
+ NULL_HASH: Buffer.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex'),
17
+ };
18
+ header;
19
+ metadata;
20
+ transactions;
21
+ _id;
22
+ constructor(serialized) {
23
+ if (!(this instanceof Block)) {
24
+ return new Block(serialized);
25
+ }
26
+ if (serialized instanceof Block) {
27
+ return Block.shallowCopy(serialized);
28
+ }
29
+ else if (typeof serialized === 'string' && JSUtil.isHexa(serialized)) {
30
+ this.fromString(serialized);
31
+ }
32
+ else if (Buffer.isBuffer(serialized)) {
33
+ this.fromBuffer(serialized);
34
+ }
35
+ else if (serialized && typeof serialized === 'object') {
36
+ this.fromObject(serialized);
37
+ }
38
+ else {
39
+ this._newBlock();
40
+ }
41
+ }
42
+ static shallowCopy(block) {
43
+ const copy = new Block(block.toBuffer());
44
+ return copy;
45
+ }
46
+ static _from(arg) {
47
+ let info = {};
48
+ if (Buffer.isBuffer(arg)) {
49
+ info = Block._fromBufferReader(new BufferReader(arg));
50
+ }
51
+ else if (typeof arg === 'object' && arg !== null) {
52
+ info = Block._fromObject(arg);
53
+ }
54
+ else {
55
+ throw new TypeError('Unrecognized argument for Block');
56
+ }
57
+ return info;
58
+ }
59
+ static fromObject(obj) {
60
+ const info = Block._fromObject(obj);
61
+ return new Block(info);
62
+ }
63
+ static fromBufferReader(br) {
64
+ Preconditions.checkArgument(br instanceof BufferReader, 'br is required');
65
+ const info = Block._fromBufferReader(br);
66
+ return new Block(info);
67
+ }
68
+ static fromBuffer(buf) {
69
+ return Block.fromBufferReader(new BufferReader(buf));
70
+ }
71
+ static fromString(str) {
72
+ const buf = Buffer.from(str, 'hex');
73
+ return Block.fromBuffer(buf);
74
+ }
75
+ static fromRawBlock(data) {
76
+ if (!BufferUtil.isBuffer(data)) {
77
+ data = Buffer.from(data, 'binary');
78
+ }
79
+ const br = new BufferReader(data);
80
+ br.pos = Block.Values.START_OF_BLOCK;
81
+ const info = Block._fromBufferReader(br);
82
+ return new Block(info);
83
+ }
84
+ static _fromObject(data) {
85
+ const transactions = [];
86
+ if (data.transactions) {
87
+ data.transactions.forEach(tx => {
88
+ if (tx instanceof Transaction) {
89
+ transactions.push(tx);
90
+ }
91
+ else {
92
+ transactions.push(new Transaction(tx));
93
+ }
94
+ });
95
+ }
96
+ return {
97
+ header: data.header
98
+ ? data.header instanceof BlockHeader
99
+ ? data.header
100
+ : new BlockHeader(data.header)
101
+ : new BlockHeader(),
102
+ metadata: data.metadata || 0x00,
103
+ transactions: transactions,
104
+ };
105
+ }
106
+ static _fromBufferReader(br) {
107
+ Preconditions.checkState(!br.finished(), 'No block data received');
108
+ const header = BlockHeader.fromBufferReader(br);
109
+ const metadata = br.readUInt8();
110
+ const transactionCount = br.readVarintNum();
111
+ const transactions = [];
112
+ for (let i = 0; i < transactionCount; i++) {
113
+ const tx = new Transaction();
114
+ transactions.push(tx.fromBufferReader(br));
115
+ }
116
+ return {
117
+ header,
118
+ metadata,
119
+ transactions,
120
+ };
121
+ }
122
+ _newBlock() {
123
+ this.header = new BlockHeader();
124
+ this.metadata = 0x00;
125
+ this.transactions = [];
126
+ }
127
+ fromBuffer(buf) {
128
+ const info = Block._fromBufferReader(new BufferReader(buf));
129
+ this.header = info.header;
130
+ this.metadata = info.metadata;
131
+ this.transactions = info.transactions;
132
+ return this;
133
+ }
134
+ fromString(str) {
135
+ const buf = Buffer.from(str, 'hex');
136
+ return this.fromBuffer(buf);
137
+ }
138
+ fromObject(obj) {
139
+ const info = Block._fromObject(obj);
140
+ this.header = info.header;
141
+ this.metadata = info.metadata;
142
+ this.transactions = info.transactions;
143
+ return this;
144
+ }
145
+ toObject() {
146
+ const transactions = this.transactions.map(tx => tx.toObject());
147
+ return {
148
+ id: this.id,
149
+ hash: this.hash,
150
+ header: this.header.toObject(),
151
+ metadata: this.metadata,
152
+ transactions: transactions,
153
+ };
154
+ }
155
+ toJSON = this.toObject;
156
+ toBuffer() {
157
+ return this.toBufferWriter().concat();
158
+ }
159
+ toString() {
160
+ return this.toBuffer().toString('hex');
161
+ }
162
+ toBufferWriter(bw) {
163
+ if (!bw) {
164
+ bw = new BufferWriter();
165
+ }
166
+ bw.write(this.header.toBuffer());
167
+ bw.writeUInt8(this.metadata);
168
+ bw.writeVarintNum(this.transactions.length);
169
+ for (let i = 0; i < this.transactions.length; i++) {
170
+ this.transactions[i].toBufferWriter(bw);
171
+ }
172
+ return bw;
173
+ }
174
+ getTransactionHashes() {
175
+ const hashes = [];
176
+ if (this.transactions.length === 0) {
177
+ return [Block.Values.NULL_HASH];
178
+ }
179
+ for (let t = 0; t < this.transactions.length; t++) {
180
+ const tx = this.transactions[t];
181
+ const txid = tx._getTxid();
182
+ const hash = tx._getHash();
183
+ const buf = Buffer.concat([hash, txid]);
184
+ const resultHash = Hash.sha256sha256(buf);
185
+ hashes.push(resultHash);
186
+ }
187
+ return hashes;
188
+ }
189
+ getMerkleTree() {
190
+ const tree = this.getTransactionHashes();
191
+ let j = 0;
192
+ for (let size = tree.length; size > 1; size = Math.floor(size / 2)) {
193
+ if (size % 2 === 1) {
194
+ tree.push(Block.Values.NULL_HASH);
195
+ size += 1;
196
+ }
197
+ for (let i = 0; i < size; i += 2) {
198
+ const buf = Buffer.concat([tree[j + i], tree[j + i + 1]]);
199
+ tree.push(Hash.sha256sha256(buf));
200
+ }
201
+ j += size;
202
+ }
203
+ return tree;
204
+ }
205
+ getMerkleRoot() {
206
+ const tree = this.getMerkleTree();
207
+ return tree[tree.length - 1];
208
+ }
209
+ validMerkleRoot() {
210
+ const h = new BN(this.header.merkleRoot.toString('hex'), 'hex');
211
+ const c = new BN(this.getMerkleRoot().toString('hex'), 'hex');
212
+ if (!h.eq(c)) {
213
+ return false;
214
+ }
215
+ return true;
216
+ }
217
+ _getHash() {
218
+ const header = this.header;
219
+ return header._getHash();
220
+ }
221
+ get hash() {
222
+ if (!this._id) {
223
+ this._id = this.header.id;
224
+ }
225
+ return this._id;
226
+ }
227
+ get id() {
228
+ return this.hash;
229
+ }
230
+ inspect() {
231
+ return '<Block ' + this.id + '>';
232
+ }
233
+ }