@alephium/web3 0.36.1 → 0.38.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 (95) hide show
  1. package/dist/alephium-web3.min.js +1 -1
  2. package/dist/alephium-web3.min.js.map +1 -1
  3. package/dist/src/api/api-alephium.d.ts +6 -1
  4. package/dist/src/api/api-alephium.js +1 -1
  5. package/dist/src/api/types.d.ts +1 -1
  6. package/dist/src/codec/array-codec.d.ts +17 -0
  7. package/dist/src/codec/array-codec.js +59 -0
  8. package/dist/src/codec/asset-output-codec.d.ts +27 -0
  9. package/dist/src/codec/asset-output-codec.js +135 -0
  10. package/dist/src/codec/bigint-codec.d.ts +5 -0
  11. package/dist/src/codec/bigint-codec.js +86 -0
  12. package/dist/src/codec/bytestring-codec.d.ts +16 -0
  13. package/dist/src/codec/bytestring-codec.js +50 -0
  14. package/dist/src/codec/codec.d.ts +8 -0
  15. package/dist/src/codec/codec.js +9 -0
  16. package/dist/src/codec/compact-int-codec.d.ts +51 -0
  17. package/dist/src/codec/compact-int-codec.js +300 -0
  18. package/dist/src/codec/contract-codec.d.ts +23 -0
  19. package/dist/src/codec/contract-codec.js +81 -0
  20. package/dist/src/codec/contract-output-codec.d.ts +21 -0
  21. package/dist/src/codec/contract-output-codec.js +82 -0
  22. package/dist/src/codec/contract-output-ref-codec.d.ts +15 -0
  23. package/dist/src/codec/contract-output-ref-codec.js +38 -0
  24. package/dist/src/codec/either-codec.d.ts +17 -0
  25. package/dist/src/codec/either-codec.js +67 -0
  26. package/dist/src/codec/hash.d.ts +4 -0
  27. package/dist/src/codec/hash.js +23 -0
  28. package/dist/src/codec/index.d.ts +23 -0
  29. package/dist/src/codec/index.js +69 -0
  30. package/dist/src/codec/input-codec.d.ts +22 -0
  31. package/dist/src/codec/input-codec.js +71 -0
  32. package/dist/src/codec/instr-codec.d.ts +230 -0
  33. package/dist/src/codec/instr-codec.js +471 -0
  34. package/dist/src/codec/lockup-script-codec.d.ts +28 -0
  35. package/dist/src/codec/lockup-script-codec.js +80 -0
  36. package/dist/src/codec/long-codec.d.ts +9 -0
  37. package/dist/src/codec/long-codec.js +56 -0
  38. package/dist/src/codec/method-codec.d.ts +31 -0
  39. package/dist/src/codec/method-codec.js +78 -0
  40. package/dist/src/codec/option-codec.d.ts +15 -0
  41. package/dist/src/codec/option-codec.js +55 -0
  42. package/dist/src/codec/output-codec.d.ts +7 -0
  43. package/dist/src/codec/output-codec.js +26 -0
  44. package/dist/src/codec/script-codec.d.ts +16 -0
  45. package/dist/src/codec/script-codec.js +41 -0
  46. package/dist/src/codec/signature-codec.d.ts +14 -0
  47. package/dist/src/codec/signature-codec.js +19 -0
  48. package/dist/src/codec/signed-int-codec.d.ts +9 -0
  49. package/dist/src/codec/signed-int-codec.js +39 -0
  50. package/dist/src/codec/token-codec.d.ts +16 -0
  51. package/dist/src/codec/token-codec.js +46 -0
  52. package/dist/src/codec/transaction-codec.d.ts +27 -0
  53. package/dist/src/codec/transaction-codec.js +128 -0
  54. package/dist/src/codec/unlock-script-codec.d.ts +40 -0
  55. package/dist/src/codec/unlock-script-codec.js +170 -0
  56. package/dist/src/codec/unsigned-tx-codec.d.ts +30 -0
  57. package/dist/src/codec/unsigned-tx-codec.js +103 -0
  58. package/dist/src/contract/contract.d.ts +14 -8
  59. package/dist/src/contract/contract.js +205 -22
  60. package/dist/src/contract/ralph.d.ts +16 -0
  61. package/dist/src/contract/ralph.js +127 -1
  62. package/dist/src/index.d.ts +1 -0
  63. package/dist/src/index.js +2 -1
  64. package/package.json +5 -4
  65. package/src/api/api-alephium.ts +7 -1
  66. package/src/api/types.ts +1 -1
  67. package/src/codec/array-codec.ts +63 -0
  68. package/src/codec/asset-output-codec.ts +149 -0
  69. package/src/codec/bigint-codec.ts +92 -0
  70. package/src/codec/bytestring-codec.ts +56 -0
  71. package/src/codec/codec.ts +31 -0
  72. package/src/codec/compact-int-codec.ts +316 -0
  73. package/src/codec/contract-codec.ts +95 -0
  74. package/src/codec/contract-output-codec.ts +95 -0
  75. package/src/codec/contract-output-ref-codec.ts +42 -0
  76. package/src/codec/either-codec.ts +74 -0
  77. package/src/codec/hash.ts +35 -0
  78. package/src/codec/index.ts +41 -0
  79. package/src/codec/input-codec.ts +81 -0
  80. package/src/codec/instr-codec.ts +479 -0
  81. package/src/codec/lockup-script-codec.ts +99 -0
  82. package/src/codec/long-codec.ts +59 -0
  83. package/src/codec/method-codec.ts +97 -0
  84. package/src/codec/option-codec.ts +60 -0
  85. package/src/codec/output-codec.ts +26 -0
  86. package/src/codec/script-codec.ts +45 -0
  87. package/src/codec/signature-codec.ts +40 -0
  88. package/src/codec/signed-int-codec.ts +37 -0
  89. package/src/codec/token-codec.ts +51 -0
  90. package/src/codec/transaction-codec.ts +147 -0
  91. package/src/codec/unlock-script-codec.ts +194 -0
  92. package/src/codec/unsigned-tx-codec.ts +124 -0
  93. package/src/contract/contract.ts +299 -23
  94. package/src/contract/ralph.ts +140 -2
  95. package/src/index.ts +1 -1
@@ -0,0 +1,479 @@
1
+ /*
2
+ Copyright 2018 - 2022 The Alephium Authors
3
+ This file is part of the alephium project.
4
+
5
+ The library is free software: you can redistribute it and/or modify
6
+ it under the terms of the GNU Lesser General Public License as published by
7
+ the Free Software Foundation, either version 3 of the License, or
8
+ (at your option) any later version.
9
+
10
+ The library is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ GNU Lesser General Public License for more details.
14
+
15
+ You should have received a copy of the GNU Lesser General Public License
16
+ along with the library. If not, see <http://www.gnu.org/licenses/>.
17
+ */
18
+ import { Buffer } from 'buffer/'
19
+ import { Parser } from 'binary-parser'
20
+ import { ArrayCodec, DecodedArray } from './array-codec'
21
+ import { compactUnsignedIntCodec, compactSignedIntCodec, DecodedCompactInt } from './compact-int-codec'
22
+ import { ByteString, byteStringCodec } from './bytestring-codec'
23
+ import { LockupScript, lockupScriptCodec } from './lockup-script-codec'
24
+ import { Codec } from './codec'
25
+
26
+ const byteStringArrayCodec = new ArrayCodec(byteStringCodec)
27
+
28
+ // eslint-disable-next-line
29
+ export interface InstrValue { }
30
+ export interface InstrValueWithIndex extends InstrValue {
31
+ index: number
32
+ }
33
+ export interface InstrValueWithCompactInt extends InstrValue {
34
+ value: DecodedCompactInt
35
+ }
36
+
37
+ export interface ByteStringConst extends InstrValue {
38
+ value: ByteString
39
+ }
40
+ export interface AddressConst extends InstrValue {
41
+ value: LockupScript
42
+ }
43
+ export interface Debug extends InstrValue {
44
+ stringParts: DecodedArray<ByteString>
45
+ }
46
+ export interface CreateMapEntryValue extends InstrValue {
47
+ immFields: number
48
+ mutFields: number
49
+ }
50
+ export interface Instr {
51
+ code: number
52
+ value: InstrValue
53
+ }
54
+
55
+ export const CallLocal = (index: number): Instr => ({ code: 0x00, value: { index } })
56
+ export const CallExternal = (index: number): Instr => ({ code: 0x01, value: { index } })
57
+ export const Return: Instr = { code: 0x02, value: {} }
58
+ export const ConstTrue: Instr = { code: 0x03, value: {} }
59
+ export const ConstFalse: Instr = { code: 0x04, value: {} }
60
+ export const I256Const0: Instr = { code: 0x05, value: {} }
61
+ export const I256Const1: Instr = { code: 0x06, value: {} }
62
+ export const I256Const2: Instr = { code: 0x07, value: {} }
63
+ export const I256Const3: Instr = { code: 0x08, value: {} }
64
+ export const I256Const4: Instr = { code: 0x09, value: {} }
65
+ export const I256Const5: Instr = { code: 0x0a, value: {} }
66
+ export const I256ConstN1: Instr = { code: 0x0b, value: {} }
67
+ export const U256Const0: Instr = { code: 0x0c, value: {} }
68
+ export const U256Const1: Instr = { code: 0x0d, value: {} }
69
+ export const U256Const2: Instr = { code: 0x0e, value: {} }
70
+ export const U256Const3: Instr = { code: 0x0f, value: {} }
71
+ export const U256Const4: Instr = { code: 0x10, value: {} }
72
+ export const U256Const5: Instr = { code: 0x11, value: {} }
73
+ export const I256Const = (value: DecodedCompactInt): Instr => ({ code: 0x12, value: { value } })
74
+ export const U256Const = (value: DecodedCompactInt): Instr => ({ code: 0x13, value: { value } })
75
+ export const ByteConst = (value: ByteString): Instr => ({ code: 0x14, value: { value } })
76
+ export const AddressConst = (value: LockupScript): Instr => ({ code: 0x15, value: { value } })
77
+ export const LoadLocal = (index: number): Instr => ({ code: 0x16, value: { index } })
78
+ export const StoreLocal = (index: number): Instr => ({ code: 0x17, value: { index } })
79
+ export const Pop: Instr = { code: 0x18, value: {} }
80
+ export const BoolNot: Instr = { code: 0x19, value: {} }
81
+ export const BoolAnd: Instr = { code: 0x1a, value: {} }
82
+ export const BoolOr: Instr = { code: 0x1b, value: {} }
83
+ export const BoolEq: Instr = { code: 0x1c, value: {} }
84
+ export const BoolNeq: Instr = { code: 0x1d, value: {} }
85
+ export const BoolToByteVec: Instr = { code: 0x1e, value: {} }
86
+ export const I256Add: Instr = { code: 0x1f, value: {} }
87
+ export const I256Sub: Instr = { code: 0x20, value: {} }
88
+ export const I256Mul: Instr = { code: 0x21, value: {} }
89
+ export const I256Div: Instr = { code: 0x22, value: {} }
90
+ export const I256Mod: Instr = { code: 0x23, value: {} }
91
+ export const I256Eq: Instr = { code: 0x24, value: {} }
92
+ export const I256Neq: Instr = { code: 0x25, value: {} }
93
+ export const I256Lt: Instr = { code: 0x26, value: {} }
94
+ export const I256Le: Instr = { code: 0x27, value: {} }
95
+ export const I256Gt: Instr = { code: 0x28, value: {} }
96
+ export const I256Ge: Instr = { code: 0x29, value: {} }
97
+ export const U256Add: Instr = { code: 0x2a, value: {} }
98
+ export const U256Sub: Instr = { code: 0x2b, value: {} }
99
+ export const U256Mul: Instr = { code: 0x2c, value: {} }
100
+ export const U256Div: Instr = { code: 0x2d, value: {} }
101
+ export const U256Mod: Instr = { code: 0x2e, value: {} }
102
+ export const U256Eq: Instr = { code: 0x2f, value: {} }
103
+ export const U256Neq: Instr = { code: 0x30, value: {} }
104
+ export const U256Lt: Instr = { code: 0x31, value: {} }
105
+ export const U256Le: Instr = { code: 0x32, value: {} }
106
+ export const U256Gt: Instr = { code: 0x33, value: {} }
107
+ export const U256Ge: Instr = { code: 0x34, value: {} }
108
+ export const U256ModAdd: Instr = { code: 0x35, value: {} }
109
+ export const U256ModSub: Instr = { code: 0x36, value: {} }
110
+ export const U256ModMul: Instr = { code: 0x37, value: {} }
111
+ export const U256BitAnd: Instr = { code: 0x38, value: {} }
112
+ export const U256BitOr: Instr = { code: 0x39, value: {} }
113
+ export const U256Xor: Instr = { code: 0x3a, value: {} }
114
+ export const U256SHL: Instr = { code: 0x3b, value: {} }
115
+ export const U256SHR: Instr = { code: 0x3c, value: {} }
116
+ export const I256ToU256: Instr = { code: 0x3d, value: {} }
117
+ export const I256ToByteVec: Instr = { code: 0x3e, value: {} }
118
+ export const U256ToI256: Instr = { code: 0x3f, value: {} }
119
+ export const U256ToByteVec: Instr = { code: 0x40, value: {} }
120
+ export const ByteVecEq: Instr = { code: 0x41, value: {} }
121
+ export const ByteVecNeq: Instr = { code: 0x42, value: {} }
122
+ export const ByteVecSize: Instr = { code: 0x43, value: {} }
123
+ export const ByteVecConcat: Instr = { code: 0x44, value: {} }
124
+ export const AddressEq: Instr = { code: 0x45, value: {} }
125
+ export const AddressNeq: Instr = { code: 0x46, value: {} }
126
+ export const AddressToByteVec: Instr = { code: 0x47, value: {} }
127
+ export const IsAssetAddress: Instr = { code: 0x48, value: {} }
128
+ export const IsContractAddress: Instr = { code: 0x49, value: {} }
129
+ export const Jump = (value: DecodedCompactInt): Instr => ({ code: 0x4a, value })
130
+ export const IfTrue = (value: DecodedCompactInt): Instr => ({ code: 0x4b, value })
131
+ export const IfFalse = (value: DecodedCompactInt): Instr => ({ code: 0x4c, value: { value } })
132
+ export const Assert: Instr = { code: 0x4d, value: {} }
133
+ export const Blake2b: Instr = { code: 0x4e, value: {} }
134
+ export const Keccak256: Instr = { code: 0x4f, value: {} }
135
+ export const Sha256: Instr = { code: 0x50, value: {} }
136
+ export const Sha3: Instr = { code: 0x51, value: {} }
137
+ export const VerifyTxSignature: Instr = { code: 0x52, value: {} }
138
+ export const VerifySecP256K1: Instr = { code: 0x53, value: {} }
139
+ export const VerifyED25519: Instr = { code: 0x54, value: {} }
140
+ export const NetworkId: Instr = { code: 0x55, value: {} }
141
+ export const BlockTimeStamp: Instr = { code: 0x56, value: {} }
142
+ export const BlockTarget: Instr = { code: 0x57, value: {} }
143
+ export const TxId: Instr = { code: 0x58, value: {} }
144
+ export const TxInputAddressAt: Instr = { code: 0x59, value: {} }
145
+ export const TxInputsSize: Instr = { code: 0x5a, value: {} }
146
+ export const VerifyAbsoluteLocktime: Instr = { code: 0x5b, value: {} }
147
+ export const VerifyRelativeLocktime: Instr = { code: 0x5c, value: {} }
148
+ export const Log1: Instr = { code: 0x5d, value: {} }
149
+ export const Log2: Instr = { code: 0x5e, value: {} }
150
+ export const Log3: Instr = { code: 0x5f, value: {} }
151
+ export const Log4: Instr = { code: 0x60, value: {} }
152
+ export const Log5: Instr = { code: 0x61, value: {} }
153
+ export const ByteVecSlice: Instr = { code: 0x62, value: {} }
154
+ export const ByteVecToAddress: Instr = { code: 0x63, value: {} }
155
+ export const Encode: Instr = { code: 0x64, value: {} }
156
+ export const Zeros: Instr = { code: 0x65, value: {} }
157
+ export const U256To1Byte: Instr = { code: 0x66, value: {} }
158
+ export const U256To2Byte: Instr = { code: 0x67, value: {} }
159
+ export const U256To4Byte: Instr = { code: 0x68, value: {} }
160
+ export const U256To8Byte: Instr = { code: 0x69, value: {} }
161
+ export const U256To16Byte: Instr = { code: 0x6a, value: {} }
162
+ export const U256To32Byte: Instr = { code: 0x6b, value: {} }
163
+ export const U256From1Byte: Instr = { code: 0x6c, value: {} }
164
+ export const U256From2Byte: Instr = { code: 0x6d, value: {} }
165
+ export const U256From4Byte: Instr = { code: 0x6e, value: {} }
166
+ export const U256From8Byte: Instr = { code: 0x6f, value: {} }
167
+ export const U256From16Byte: Instr = { code: 0x70, value: {} }
168
+ export const U256From32Byte: Instr = { code: 0x71, value: {} }
169
+ export const EthEcRecover: Instr = { code: 0x72, value: {} }
170
+ export const Log6: Instr = { code: 0x73, value: {} }
171
+ export const Log7: Instr = { code: 0x74, value: {} }
172
+ export const Log8: Instr = { code: 0x75, value: {} }
173
+ export const Log9: Instr = { code: 0x76, value: {} }
174
+ export const ContractIdToAddress: Instr = { code: 0x77, value: {} }
175
+ export const LoadLocalByIndex: Instr = { code: 0x78, value: {} }
176
+ export const StoreLocalByIndex: Instr = { code: 0x79, value: {} }
177
+ export const Dup: Instr = { code: 0x7a, value: {} }
178
+ export const AssertWithErrorCode: Instr = { code: 0x7b, value: {} }
179
+ export const Swap: Instr = { code: 0x7c, value: {} }
180
+ export const BlockHash: Instr = { code: 0x7d, value: {} }
181
+ export const DEBUG = (stringParts: DecodedArray<ByteString>): Instr => ({ code: 0x7e, value: { stringParts } })
182
+ export const TxGasPrice: Instr = { code: 0x7f, value: {} }
183
+ export const TxGasAmount: Instr = { code: 0x80, value: {} }
184
+ export const TxGasFee: Instr = { code: 0x81, value: {} }
185
+ export const I256Exp: Instr = { code: 0x82, value: {} }
186
+ export const U256Exp: Instr = { code: 0x83, value: {} }
187
+ export const U256ModExp: Instr = { code: 0x84, value: {} }
188
+ export const VerifyBIP340Schnorr: Instr = { code: 0x85, value: {} }
189
+ export const GetSegragatedSignature: Instr = { code: 0x86, value: {} }
190
+ export const MulModN: Instr = { code: 0x87, value: {} }
191
+ export const AddModN: Instr = { code: 0x88, value: {} }
192
+ export const U256ToString: Instr = { code: 0x89, value: {} }
193
+ export const I256ToString: Instr = { code: 0x8a, value: {} }
194
+ export const BoolToString: Instr = { code: 0x8b, value: {} }
195
+ export const LoadMutField = (index: number): Instr => ({ code: 0xa0, value: { index } })
196
+ export const StoreMutField = (index: number): Instr => ({ code: 0xa1, value: { index } })
197
+ export const ApproveAlph: Instr = { code: 0xa2, value: {} }
198
+ export const ApproveToken: Instr = { code: 0xa3, value: {} }
199
+ export const AlphRemaining: Instr = { code: 0xa4, value: {} }
200
+ export const TokenRemaining: Instr = { code: 0xa5, value: {} }
201
+ export const IsPaying: Instr = { code: 0xa6, value: {} }
202
+ export const TransferAlph: Instr = { code: 0xa7, value: {} }
203
+ export const TransferAlphFromSelf: Instr = { code: 0xa8, value: {} }
204
+ export const TransferAlphToSelf: Instr = { code: 0xa9, value: {} }
205
+ export const TransferToken: Instr = { code: 0xaa, value: {} }
206
+ export const TransferTokenFromSelf: Instr = { code: 0xab, value: {} }
207
+ export const TransferTokenToSelf: Instr = { code: 0xac, value: {} }
208
+ export const CreateContract: Instr = { code: 0xad, value: {} }
209
+ export const CreateContractWithToken: Instr = { code: 0xae, value: {} }
210
+ export const CopyCreateContract: Instr = { code: 0xaf, value: {} }
211
+ export const DestroySelf: Instr = { code: 0xb0, value: {} }
212
+ export const SelfContractId: Instr = { code: 0xb1, value: {} }
213
+ export const SelfAddress: Instr = { code: 0xb2, value: {} }
214
+ export const CallerContractId: Instr = { code: 0xb3, value: {} }
215
+ export const CallerAddress: Instr = { code: 0xb4, value: {} }
216
+ export const IsCallerFromTxScript: Instr = { code: 0xb5, value: {} }
217
+ export const CallerInitialStateHash: Instr = { code: 0xb6, value: {} }
218
+ export const CallerCodeHash: Instr = { code: 0xb7, value: {} }
219
+ export const ContractInitialStateHash: Instr = { code: 0xb8, value: {} }
220
+ export const ContractInitialCodeHash: Instr = { code: 0xb9, value: {} }
221
+ export const MigrateSimple: Instr = { code: 0xba, value: {} }
222
+ export const MigrateWithFields: Instr = { code: 0xbb, value: {} }
223
+ export const CopyCreateContractWithToken: Instr = { code: 0xbc, value: {} }
224
+ export const BurnToken: Instr = { code: 0xbd, value: {} }
225
+ export const LockApprovedAssets: Instr = { code: 0xbe, value: {} }
226
+ export const CreateSubContract: Instr = { code: 0xbf, value: {} }
227
+ export const CreateSubContractWithToken: Instr = { code: 0xc0, value: {} }
228
+ export const CopyCreateSubContract: Instr = { code: 0xc1, value: {} }
229
+ export const CopyCreateSubContractWithToken: Instr = { code: 0xc2, value: {} }
230
+ export const LoadMutFieldByIndex: Instr = { code: 0xc3, value: {} }
231
+ export const StoreMutFieldByIndex: Instr = { code: 0xc4, value: {} }
232
+ export const ContractExists: Instr = { code: 0xc5, value: {} }
233
+ export const CreateContractAndTransferToken: Instr = { code: 0xc6, value: {} }
234
+ export const CopyCreateContractAndTransferToken: Instr = { code: 0xc7, value: {} }
235
+ export const CreateSubContractAndTransferToken: Instr = { code: 0xc8, value: {} }
236
+ export const CopyCreateSubContractAndTransferToken: Instr = { code: 0xc9, value: {} }
237
+ export const NullContractAddress: Instr = { code: 0xca, value: {} }
238
+ export const SubContractId: Instr = { code: 0xcb, value: {} }
239
+ export const SubContractIdOf: Instr = { code: 0xcc, value: {} }
240
+ export const AlphTokenId: Instr = { code: 0xcd, value: {} }
241
+ export const LoadImmField = (index: number): Instr => ({ code: 0xce, value: { index } })
242
+ export const LoadImmFieldByIndex: Instr = { code: 0xcf, value: {} }
243
+ export const PayGasFee: Instr = { code: 0xd0, value: {} }
244
+ export const MinimalContractDeposit: Instr = { code: 0xd1, value: {} }
245
+ export const CreateMapEntry: (immFields: number, mutFields: number) => Instr = (
246
+ immFields: number,
247
+ mutFields: number
248
+ ) => ({ code: 0xd2, value: { immFields, mutFields } })
249
+
250
+ export class InstrCodec implements Codec<Instr> {
251
+ parser = Parser.start()
252
+ .uint8('code')
253
+ .choice('value', {
254
+ tag: 'code',
255
+ choices: {
256
+ 0x00: Parser.start().uint8('index'), // CallLocal
257
+ 0x01: Parser.start().uint8('index'), // CallExternal
258
+ [Return.code]: Parser.start(),
259
+ [ConstTrue.code]: Parser.start(),
260
+ [ConstFalse.code]: Parser.start(),
261
+ [I256Const0.code]: Parser.start(),
262
+ [I256Const1.code]: Parser.start(),
263
+ [I256Const2.code]: Parser.start(),
264
+ [I256Const3.code]: Parser.start(),
265
+ [I256Const4.code]: Parser.start(),
266
+ [I256Const5.code]: Parser.start(),
267
+ [I256ConstN1.code]: Parser.start(),
268
+ [U256Const0.code]: Parser.start(),
269
+ [U256Const1.code]: Parser.start(),
270
+ [U256Const2.code]: Parser.start(),
271
+ [U256Const3.code]: Parser.start(),
272
+ [U256Const4.code]: Parser.start(),
273
+ [U256Const5.code]: Parser.start(),
274
+ 0x12: Parser.start().nest('value', { type: compactSignedIntCodec.parser }),
275
+ 0x13: Parser.start().nest('value', { type: compactUnsignedIntCodec.parser }),
276
+ 0x14: Parser.start().nest('value', { type: byteStringCodec.parser }),
277
+ 0x15: Parser.start().nest('value', { type: lockupScriptCodec.parser }),
278
+ 0x16: Parser.start().uint8('index'),
279
+ 0x17: Parser.start().uint8('index'),
280
+ [Pop.code]: Parser.start(),
281
+ [BoolNot.code]: Parser.start(),
282
+ [BoolAnd.code]: Parser.start(),
283
+ [BoolOr.code]: Parser.start(),
284
+ [BoolEq.code]: Parser.start(),
285
+ [BoolNeq.code]: Parser.start(),
286
+ [BoolToByteVec.code]: Parser.start(),
287
+ [I256Add.code]: Parser.start(),
288
+ [I256Sub.code]: Parser.start(),
289
+ [I256Mul.code]: Parser.start(),
290
+ [I256Div.code]: Parser.start(),
291
+ [I256Mod.code]: Parser.start(),
292
+ [I256Eq.code]: Parser.start(),
293
+ [I256Neq.code]: Parser.start(),
294
+ [I256Lt.code]: Parser.start(),
295
+ [I256Le.code]: Parser.start(),
296
+ [I256Gt.code]: Parser.start(),
297
+ [I256Ge.code]: Parser.start(),
298
+ [U256Add.code]: Parser.start(),
299
+ [U256Sub.code]: Parser.start(),
300
+ [U256Mul.code]: Parser.start(),
301
+ [U256Div.code]: Parser.start(),
302
+ [U256Mod.code]: Parser.start(),
303
+ [U256Eq.code]: Parser.start(),
304
+ [U256Neq.code]: Parser.start(),
305
+ [U256Lt.code]: Parser.start(),
306
+ [U256Le.code]: Parser.start(),
307
+ [U256Gt.code]: Parser.start(),
308
+ [U256Ge.code]: Parser.start(),
309
+ [U256ModAdd.code]: Parser.start(),
310
+ [U256ModSub.code]: Parser.start(),
311
+ [U256ModMul.code]: Parser.start(),
312
+ [U256BitAnd.code]: Parser.start(),
313
+ [U256BitOr.code]: Parser.start(),
314
+ [U256Xor.code]: Parser.start(),
315
+ [U256SHL.code]: Parser.start(),
316
+ [U256SHR.code]: Parser.start(),
317
+ [I256ToU256.code]: Parser.start(),
318
+ [I256ToByteVec.code]: Parser.start(),
319
+ [U256ToI256.code]: Parser.start(),
320
+ [U256ToByteVec.code]: Parser.start(),
321
+ [ByteVecEq.code]: Parser.start(),
322
+ [ByteVecNeq.code]: Parser.start(),
323
+ [ByteVecSize.code]: Parser.start(),
324
+ [ByteVecConcat.code]: Parser.start(),
325
+ [AddressEq.code]: Parser.start(),
326
+ [AddressNeq.code]: Parser.start(),
327
+ [AddressToByteVec.code]: Parser.start(),
328
+ [IsAssetAddress.code]: Parser.start(),
329
+ [IsContractAddress.code]: Parser.start(),
330
+ 0x4a: Parser.start().nest('value', { type: compactUnsignedIntCodec.parser }), // Jump
331
+ 0x4b: Parser.start().nest('value', { type: compactUnsignedIntCodec.parser }), // IfTrue
332
+ 0x4c: Parser.start().nest('value', { type: compactUnsignedIntCodec.parser }), // IfFalse
333
+ [Assert.code]: Parser.start(),
334
+ [Blake2b.code]: Parser.start(),
335
+ [Keccak256.code]: Parser.start(),
336
+ [Sha256.code]: Parser.start(),
337
+ [Sha3.code]: Parser.start(),
338
+ [VerifyTxSignature.code]: Parser.start(),
339
+ [VerifySecP256K1.code]: Parser.start(),
340
+ [VerifyED25519.code]: Parser.start(),
341
+ [NetworkId.code]: Parser.start(),
342
+ [BlockTimeStamp.code]: Parser.start(),
343
+ [BlockTarget.code]: Parser.start(),
344
+ [TxId.code]: Parser.start(),
345
+ [TxInputAddressAt.code]: Parser.start(),
346
+ [TxInputsSize.code]: Parser.start(),
347
+ [VerifyAbsoluteLocktime.code]: Parser.start(),
348
+ [VerifyRelativeLocktime.code]: Parser.start(),
349
+ [Log1.code]: Parser.start(),
350
+ [Log2.code]: Parser.start(),
351
+ [Log3.code]: Parser.start(),
352
+ [Log4.code]: Parser.start(),
353
+ [Log5.code]: Parser.start(),
354
+ [ByteVecSlice.code]: Parser.start(),
355
+ [ByteVecToAddress.code]: Parser.start(),
356
+ [Encode.code]: Parser.start(),
357
+ [Zeros.code]: Parser.start(),
358
+ [U256To1Byte.code]: Parser.start(),
359
+ [U256To2Byte.code]: Parser.start(),
360
+ [U256To4Byte.code]: Parser.start(),
361
+ [U256To8Byte.code]: Parser.start(),
362
+ [U256To16Byte.code]: Parser.start(),
363
+ [U256To32Byte.code]: Parser.start(),
364
+ [U256From1Byte.code]: Parser.start(),
365
+ [U256From2Byte.code]: Parser.start(),
366
+ [U256From4Byte.code]: Parser.start(),
367
+ [U256From8Byte.code]: Parser.start(),
368
+ [U256From16Byte.code]: Parser.start(),
369
+ [U256From32Byte.code]: Parser.start(),
370
+ [EthEcRecover.code]: Parser.start(),
371
+ [Log6.code]: Parser.start(),
372
+ [Log7.code]: Parser.start(),
373
+ [Log8.code]: Parser.start(),
374
+ [Log9.code]: Parser.start(),
375
+ [ContractIdToAddress.code]: Parser.start(),
376
+ [LoadLocalByIndex.code]: Parser.start(),
377
+ [StoreLocalByIndex.code]: Parser.start(),
378
+ [Dup.code]: Parser.start(),
379
+ [AssertWithErrorCode.code]: Parser.start(),
380
+ [Swap.code]: Parser.start(),
381
+ [BlockHash.code]: Parser.start(),
382
+ 0x7e: Parser.start().nest('stringParts', { type: byteStringArrayCodec.parser }), // DEBUG
383
+ [TxGasPrice.code]: Parser.start(),
384
+ [TxGasAmount.code]: Parser.start(),
385
+ [TxGasFee.code]: Parser.start(),
386
+ [I256Exp.code]: Parser.start(),
387
+ [U256Exp.code]: Parser.start(),
388
+ [U256ModExp.code]: Parser.start(),
389
+ [VerifyBIP340Schnorr.code]: Parser.start(),
390
+ [GetSegragatedSignature.code]: Parser.start(),
391
+ [MulModN.code]: Parser.start(),
392
+ [AddModN.code]: Parser.start(),
393
+ [U256ToString.code]: Parser.start(),
394
+ [I256ToString.code]: Parser.start(),
395
+ [BoolToString.code]: Parser.start(),
396
+ 0xa0: Parser.start().uint8('index'),
397
+ 0xa1: Parser.start().uint8('index'),
398
+ [ApproveAlph.code]: Parser.start(),
399
+ [ApproveToken.code]: Parser.start(),
400
+ [AlphRemaining.code]: Parser.start(),
401
+ [TokenRemaining.code]: Parser.start(),
402
+ [IsPaying.code]: Parser.start(),
403
+ [TransferAlph.code]: Parser.start(),
404
+ [TransferAlphFromSelf.code]: Parser.start(),
405
+ [TransferAlphToSelf.code]: Parser.start(),
406
+ [TransferToken.code]: Parser.start(),
407
+ [TransferTokenFromSelf.code]: Parser.start(),
408
+ [TransferTokenToSelf.code]: Parser.start(),
409
+ [CreateContract.code]: Parser.start(),
410
+ [CreateContractWithToken.code]: Parser.start(),
411
+ [CopyCreateContract.code]: Parser.start(),
412
+ [DestroySelf.code]: Parser.start(),
413
+ [SelfContractId.code]: Parser.start(),
414
+ [SelfAddress.code]: Parser.start(),
415
+ [CallerContractId.code]: Parser.start(),
416
+ [CallerAddress.code]: Parser.start(),
417
+ [IsCallerFromTxScript.code]: Parser.start(),
418
+ [CallerInitialStateHash.code]: Parser.start(),
419
+ [CallerCodeHash.code]: Parser.start(),
420
+ [ContractInitialStateHash.code]: Parser.start(),
421
+ [ContractInitialCodeHash.code]: Parser.start(),
422
+ [MigrateSimple.code]: Parser.start(),
423
+ [MigrateWithFields.code]: Parser.start(),
424
+ [CopyCreateContractWithToken.code]: Parser.start(),
425
+ [BurnToken.code]: Parser.start(),
426
+ [LockApprovedAssets.code]: Parser.start(),
427
+ [CreateSubContract.code]: Parser.start(),
428
+ [CreateSubContractWithToken.code]: Parser.start(),
429
+ [CopyCreateSubContract.code]: Parser.start(),
430
+ [CopyCreateSubContractWithToken.code]: Parser.start(),
431
+ [LoadMutFieldByIndex.code]: Parser.start(),
432
+ [StoreMutFieldByIndex.code]: Parser.start(),
433
+ [ContractExists.code]: Parser.start(),
434
+ [CreateContractAndTransferToken.code]: Parser.start(),
435
+ [CopyCreateContractAndTransferToken.code]: Parser.start(),
436
+ [CreateSubContractAndTransferToken.code]: Parser.start(),
437
+ [CopyCreateSubContractAndTransferToken.code]: Parser.start(),
438
+ [NullContractAddress.code]: Parser.start(),
439
+ [SubContractId.code]: Parser.start(),
440
+ [SubContractIdOf.code]: Parser.start(),
441
+ [AlphTokenId.code]: Parser.start(),
442
+ 0xce: Parser.start().uint8('index'),
443
+ [LoadImmFieldByIndex.code]: Parser.start(),
444
+ [PayGasFee.code]: Parser.start(),
445
+ [MinimalContractDeposit.code]: Parser.start(),
446
+ 0xd2: Parser.start().uint8('immFields').uint8('mutFields')
447
+ }
448
+ })
449
+
450
+ encode(instr: Instr): Buffer {
451
+ const instrValue = instr.value
452
+ const result = [instr.code]
453
+ const instrsWithIndex = [0x00, 0x01, 0x16, 0x17, 0xa0, 0xa1, 0xce]
454
+ const instrsWithCompactInt = [0x12, 0x13, 0x4a, 0x4b, 0x4c]
455
+ if (instr.code === 0x14) {
456
+ result.push(...byteStringCodec.encode((instrValue as ByteStringConst).value))
457
+ } else if (instr.code === 0x15) {
458
+ result.push(...lockupScriptCodec.encode((instrValue as AddressConst).value))
459
+ } else if (instr.code === 0x7e) {
460
+ result.push(...byteStringArrayCodec.encode((instrValue as Debug).stringParts.value))
461
+ } else if (instrsWithCompactInt.includes(instr.code)) {
462
+ result.push(...compactUnsignedIntCodec.encode((instrValue as InstrValueWithCompactInt).value))
463
+ } else if (instrsWithIndex.includes(instr.code)) {
464
+ result.push((instrValue as InstrValueWithIndex).index)
465
+ } else if (instr.code === 0xd2) {
466
+ const value = instrValue as CreateMapEntryValue
467
+ result.push(value.immFields, value.mutFields)
468
+ }
469
+
470
+ return Buffer.from(result)
471
+ }
472
+
473
+ decode(input: Buffer): Instr {
474
+ return this.parser.parse(input)
475
+ }
476
+ }
477
+
478
+ export const instrCodec = new InstrCodec()
479
+ export const instrsCodec = new ArrayCodec<Instr>(instrCodec)
@@ -0,0 +1,99 @@
1
+ /*
2
+ Copyright 2018 - 2022 The Alephium Authors
3
+ This file is part of the alephium project.
4
+
5
+ The library is free software: you can redistribute it and/or modify
6
+ it under the terms of the GNU Lesser General Public License as published by
7
+ the Free Software Foundation, either version 3 of the License, or
8
+ (at your option) any later version.
9
+
10
+ The library is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ GNU Lesser General Public License for more details.
14
+
15
+ You should have received a copy of the GNU Lesser General Public License
16
+ along with the library. If not, see <http://www.gnu.org/licenses/>.
17
+ */
18
+ import { Buffer } from 'buffer/'
19
+ import { Parser } from 'binary-parser'
20
+ import { DecodedCompactInt, compactUnsignedIntCodec } from './compact-int-codec'
21
+ import { Codec } from './codec'
22
+ import { ArrayCodec, DecodedArray } from './array-codec'
23
+
24
+ export interface PublicKeyHash {
25
+ publicKeyHash: Buffer
26
+ }
27
+
28
+ class PublicKeyHashCodec implements Codec<PublicKeyHash> {
29
+ parser = Parser.start().buffer('publicKeyHash', { length: 32 })
30
+
31
+ encode(input: PublicKeyHash): Buffer {
32
+ return input.publicKeyHash
33
+ }
34
+
35
+ decode(input: Buffer): PublicKeyHash {
36
+ return this.parser.parse(input)
37
+ }
38
+ }
39
+
40
+ const publicKeyHashCodec = new PublicKeyHashCodec()
41
+ const publicKeyHashesCodec = new ArrayCodec(publicKeyHashCodec)
42
+ const multiSigParser = Parser.start()
43
+ .nest('publicKeyHashes', { type: publicKeyHashesCodec.parser })
44
+ .nest('m', { type: compactUnsignedIntCodec.parser })
45
+ export interface MultiSig {
46
+ publicKeyHashes: DecodedArray<PublicKeyHash>
47
+ m: DecodedCompactInt
48
+ }
49
+
50
+ export interface P2SH {
51
+ scriptHash: Buffer
52
+ }
53
+
54
+ export interface P2C {
55
+ contractId: Buffer
56
+ }
57
+
58
+ export interface LockupScript {
59
+ scriptType: number
60
+ script: PublicKeyHash | MultiSig | P2SH | P2C
61
+ }
62
+
63
+ export class LockupScriptCodec implements Codec<LockupScript> {
64
+ parser = Parser.start()
65
+ .uint8('scriptType')
66
+ .choice('script', {
67
+ tag: 'scriptType',
68
+ choices: {
69
+ 0: publicKeyHashCodec.parser,
70
+ 1: multiSigParser,
71
+ 2: Parser.start().buffer('scriptHash', { length: 32 }),
72
+ 3: Parser.start().buffer('contractId', { length: 32 })
73
+ }
74
+ })
75
+
76
+ encode(input: LockupScript): Buffer {
77
+ const result: number[] = [input.scriptType]
78
+ if (input.scriptType === 0) {
79
+ result.push(...(input.script as PublicKeyHash).publicKeyHash)
80
+ } else if (input.scriptType === 1) {
81
+ result.push(...publicKeyHashesCodec.encode((input.script as MultiSig).publicKeyHashes.value))
82
+ result.push(...compactUnsignedIntCodec.encode((input.script as MultiSig).m))
83
+ } else if (input.scriptType === 2) {
84
+ result.push(...(input.script as P2SH).scriptHash)
85
+ } else if (input.scriptType === 3) {
86
+ result.push(...(input.script as P2C).contractId)
87
+ } else {
88
+ throw new Error(`Unsupported script type: ${input.scriptType}`)
89
+ }
90
+
91
+ return Buffer.from(result)
92
+ }
93
+
94
+ decode(input: Buffer): LockupScript {
95
+ return this.parser.parse(input)
96
+ }
97
+ }
98
+
99
+ export const lockupScriptCodec = new LockupScriptCodec()
@@ -0,0 +1,59 @@
1
+ /*
2
+ Copyright 2018 - 2022 The Alephium Authors
3
+ This file is part of the alephium project.
4
+
5
+ The library is free software: you can redistribute it and/or modify
6
+ it under the terms of the GNU Lesser General Public License as published by
7
+ the Free Software Foundation, either version 3 of the License, or
8
+ (at your option) any later version.
9
+
10
+ The library is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ GNU Lesser General Public License for more details.
14
+
15
+ You should have received a copy of the GNU Lesser General Public License
16
+ along with the library. If not, see <http://www.gnu.org/licenses/>.
17
+ */
18
+ import { Buffer } from 'buffer/'
19
+ import { Parser } from 'binary-parser'
20
+ import { Codec, assert } from './codec'
21
+
22
+ export class LongCodec implements Codec<bigint> {
23
+ parser = Parser.start().buffer('value', {
24
+ length: 8
25
+ })
26
+
27
+ encode(input: bigint): Buffer {
28
+ const byteArray = new Uint8Array(8)
29
+
30
+ assert(byteArray.length <= 8, 'Length should be less than or equal to 8')
31
+ for (let index = 0; index < byteArray.length; index++) {
32
+ const byte = input & BigInt(0xff)
33
+ byteArray[byteArray.length - index - 1] = Number(byte)
34
+ input >>= BigInt(8)
35
+ }
36
+
37
+ return Buffer.from(byteArray)
38
+ }
39
+
40
+ decode(bytes: Buffer): bigint {
41
+ assert(bytes.length == 8, 'Length should be 8')
42
+ let int64 = BigInt(0)
43
+ let pow = BigInt(1)
44
+
45
+ for (let i = bytes.length - 1; i >= 0; i--) {
46
+ int64 += BigInt(bytes[i]) * pow
47
+ pow *= BigInt(256)
48
+ }
49
+
50
+ // Determine if the number is negative (check the sign bit of the first byte)
51
+ if (bytes[0] & 0x80) {
52
+ int64 -= BigInt(1) << BigInt(64)
53
+ }
54
+
55
+ return int64
56
+ }
57
+ }
58
+
59
+ export const longCodec = new LongCodec()