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