@alephium/web3 0.37.0 → 0.38.1

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 (97) 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 -0
  4. package/dist/src/api/types.d.ts +1 -1
  5. package/dist/src/codec/array-codec.d.ts +17 -0
  6. package/dist/src/codec/array-codec.js +59 -0
  7. package/dist/src/codec/asset-output-codec.d.ts +27 -0
  8. package/dist/src/codec/asset-output-codec.js +135 -0
  9. package/dist/src/codec/bigint-codec.d.ts +5 -0
  10. package/dist/src/codec/bigint-codec.js +86 -0
  11. package/dist/src/codec/bytestring-codec.d.ts +16 -0
  12. package/dist/src/codec/bytestring-codec.js +50 -0
  13. package/dist/src/codec/codec.d.ts +8 -0
  14. package/dist/src/codec/codec.js +9 -0
  15. package/dist/src/codec/compact-int-codec.d.ts +51 -0
  16. package/dist/src/codec/compact-int-codec.js +300 -0
  17. package/dist/src/codec/contract-codec.d.ts +23 -0
  18. package/dist/src/codec/contract-codec.js +81 -0
  19. package/dist/src/codec/contract-output-codec.d.ts +21 -0
  20. package/dist/src/codec/contract-output-codec.js +82 -0
  21. package/dist/src/codec/contract-output-ref-codec.d.ts +15 -0
  22. package/dist/src/codec/contract-output-ref-codec.js +38 -0
  23. package/dist/src/codec/either-codec.d.ts +17 -0
  24. package/dist/src/codec/either-codec.js +67 -0
  25. package/dist/src/codec/hash.d.ts +4 -0
  26. package/dist/src/codec/hash.js +23 -0
  27. package/dist/src/codec/index.d.ts +23 -0
  28. package/dist/src/codec/index.js +69 -0
  29. package/dist/src/codec/input-codec.d.ts +22 -0
  30. package/dist/src/codec/input-codec.js +71 -0
  31. package/dist/src/codec/instr-codec.d.ts +230 -0
  32. package/dist/src/codec/instr-codec.js +471 -0
  33. package/dist/src/codec/lockup-script-codec.d.ts +28 -0
  34. package/dist/src/codec/lockup-script-codec.js +80 -0
  35. package/dist/src/codec/long-codec.d.ts +9 -0
  36. package/dist/src/codec/long-codec.js +56 -0
  37. package/dist/src/codec/method-codec.d.ts +31 -0
  38. package/dist/src/codec/method-codec.js +78 -0
  39. package/dist/src/codec/option-codec.d.ts +15 -0
  40. package/dist/src/codec/option-codec.js +55 -0
  41. package/dist/src/codec/output-codec.d.ts +7 -0
  42. package/dist/src/codec/output-codec.js +26 -0
  43. package/dist/src/codec/script-codec.d.ts +16 -0
  44. package/dist/src/codec/script-codec.js +41 -0
  45. package/dist/src/codec/signature-codec.d.ts +14 -0
  46. package/dist/src/codec/signature-codec.js +19 -0
  47. package/dist/src/codec/signed-int-codec.d.ts +9 -0
  48. package/dist/src/codec/signed-int-codec.js +39 -0
  49. package/dist/src/codec/token-codec.d.ts +16 -0
  50. package/dist/src/codec/token-codec.js +46 -0
  51. package/dist/src/codec/transaction-codec.d.ts +27 -0
  52. package/dist/src/codec/transaction-codec.js +128 -0
  53. package/dist/src/codec/unlock-script-codec.d.ts +40 -0
  54. package/dist/src/codec/unlock-script-codec.js +170 -0
  55. package/dist/src/codec/unsigned-tx-codec.d.ts +30 -0
  56. package/dist/src/codec/unsigned-tx-codec.js +103 -0
  57. package/dist/src/contract/contract.d.ts +10 -4
  58. package/dist/src/contract/contract.js +184 -11
  59. package/dist/src/contract/ralph.d.ts +16 -0
  60. package/dist/src/contract/ralph.js +127 -1
  61. package/dist/src/index.d.ts +1 -0
  62. package/dist/src/index.js +2 -1
  63. package/dist/src/utils/address.d.ts +2 -0
  64. package/dist/src/utils/address.js +18 -6
  65. package/package.json +4 -3
  66. package/src/api/api-alephium.ts +6 -0
  67. package/src/api/types.ts +1 -1
  68. package/src/codec/array-codec.ts +63 -0
  69. package/src/codec/asset-output-codec.ts +149 -0
  70. package/src/codec/bigint-codec.ts +92 -0
  71. package/src/codec/bytestring-codec.ts +56 -0
  72. package/src/codec/codec.ts +31 -0
  73. package/src/codec/compact-int-codec.ts +316 -0
  74. package/src/codec/contract-codec.ts +95 -0
  75. package/src/codec/contract-output-codec.ts +95 -0
  76. package/src/codec/contract-output-ref-codec.ts +42 -0
  77. package/src/codec/either-codec.ts +74 -0
  78. package/src/codec/hash.ts +35 -0
  79. package/src/codec/index.ts +41 -0
  80. package/src/codec/input-codec.ts +81 -0
  81. package/src/codec/instr-codec.ts +479 -0
  82. package/src/codec/lockup-script-codec.ts +99 -0
  83. package/src/codec/long-codec.ts +59 -0
  84. package/src/codec/method-codec.ts +97 -0
  85. package/src/codec/option-codec.ts +60 -0
  86. package/src/codec/output-codec.ts +26 -0
  87. package/src/codec/script-codec.ts +45 -0
  88. package/src/codec/signature-codec.ts +40 -0
  89. package/src/codec/signed-int-codec.ts +37 -0
  90. package/src/codec/token-codec.ts +51 -0
  91. package/src/codec/transaction-codec.ts +147 -0
  92. package/src/codec/unlock-script-codec.ts +194 -0
  93. package/src/codec/unsigned-tx-codec.ts +124 -0
  94. package/src/contract/contract.ts +271 -14
  95. package/src/contract/ralph.ts +140 -2
  96. package/src/index.ts +1 -1
  97. package/src/utils/address.ts +17 -5
@@ -0,0 +1,31 @@
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
+
21
+ export interface Codec<T> {
22
+ parser: Parser
23
+ encode(input: T): Buffer
24
+ decode(input: Buffer): T
25
+ }
26
+
27
+ export function assert(value: boolean, message: string) {
28
+ if (!value) {
29
+ throw new Error(message)
30
+ }
31
+ }
@@ -0,0 +1,316 @@
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
+ import { BigIntCodec } from './bigint-codec'
22
+ import { ArrayCodec } from './array-codec'
23
+
24
+ export class CompactInt {
25
+ static readonly oneBytePrefix = 0x00
26
+ static readonly oneByteNegPrefix = 0xc0
27
+ static readonly twoBytePrefix = 0x40
28
+ static readonly twoByteNegPrefix = 0x80
29
+ static readonly fourBytePrefix = 0x80
30
+ static readonly fourByteNegPrefix = 0x40
31
+ static readonly multiBytePrefix = 0xc0
32
+ }
33
+
34
+ const maskRest = 0xc0
35
+ const maskMode = 0x3f
36
+ const maskModeNeg = 0xffffffc0
37
+ const signFlag = 0x20 // 0b00100000
38
+
39
+ export interface DecodedCompactInt {
40
+ mode: number
41
+ rest: Uint8Array
42
+ }
43
+
44
+ const compactIntParser = new Parser().uint8('mode').buffer('rest', {
45
+ length: function (ctx) {
46
+ const rawMode = this['mode']
47
+ const mode = rawMode & maskRest
48
+
49
+ switch (mode) {
50
+ case CompactInt.oneBytePrefix:
51
+ return 0
52
+ case CompactInt.twoBytePrefix:
53
+ return 1
54
+ case CompactInt.fourBytePrefix:
55
+ return 3
56
+ default:
57
+ return (rawMode & maskMode) + 4
58
+ }
59
+ }
60
+ })
61
+
62
+ export class CompactUnsignedIntCodec implements Codec<DecodedCompactInt> {
63
+ private oneByteBound = 0x40
64
+ private twoByteBound = this.oneByteBound << 8
65
+ private fourByteBound = this.oneByteBound << (8 * 3)
66
+
67
+ parser = compactIntParser
68
+
69
+ encode(input: DecodedCompactInt): Buffer {
70
+ return Buffer.from([input.mode, ...input.rest])
71
+ }
72
+
73
+ encodeU32(value: number): Buffer {
74
+ if (value < this.oneByteBound) {
75
+ return Buffer.from([(CompactInt.oneBytePrefix + value) & 0xff])
76
+ } else if (value < this.twoByteBound) {
77
+ return Buffer.from([(CompactInt.twoBytePrefix + (value >> 8)) & 0xff, value & 0xff])
78
+ } else if (value < this.fourByteBound) {
79
+ return Buffer.from([
80
+ (CompactInt.fourBytePrefix + (value >> 24)) & 0xff,
81
+ (value >> 16) & 0xff,
82
+ (value >> 8) & 0xff,
83
+ value & 0xff
84
+ ])
85
+ } else {
86
+ return Buffer.from([
87
+ CompactInt.multiBytePrefix,
88
+ (value >> 24) & 0xff,
89
+ (value >> 16) & 0xff,
90
+ (value >> 8) & 0xff,
91
+ value & 0xff
92
+ ])
93
+ }
94
+ }
95
+
96
+ encodeU256(value: bigint): Buffer {
97
+ assert(value >= 0n, 'Value should be positive')
98
+
99
+ if (value < this.fourByteBound) {
100
+ return this.encodeU32(Number(value))
101
+ } else {
102
+ let bytes = BigIntCodec.encode(value)
103
+ if (bytes[0] === 0) {
104
+ bytes = bytes.slice(1)
105
+ }
106
+
107
+ assert(bytes.length <= 32, 'Expect <= 32 bytes for U256')
108
+
109
+ const header = (bytes.length - 4 + CompactInt.multiBytePrefix) & 0xff
110
+ return Buffer.concat([Buffer.from([header]), bytes])
111
+ }
112
+ }
113
+
114
+ decodeU32(input: Buffer): number {
115
+ const decoded = this.decode(input)
116
+ return this.toU32(decoded)
117
+ }
118
+
119
+ decodeU256(input: Buffer): bigint {
120
+ const decoded = this.decode(input)
121
+ return this.toU256(decoded)
122
+ }
123
+
124
+ decode(input: Buffer): DecodedCompactInt {
125
+ return this.parser.parse(input)
126
+ }
127
+
128
+ toU32(value: DecodedCompactInt): number {
129
+ const body = Buffer.from([value.mode, ...value.rest])
130
+ return decodePositiveInt(value.mode, body)
131
+ }
132
+
133
+ fromU32(value: number): DecodedCompactInt {
134
+ return this.decode(this.encodeU32(value))
135
+ }
136
+
137
+ toU256(value: DecodedCompactInt): bigint {
138
+ const mode = value.mode & maskRest
139
+ if (fixedSize(mode)) {
140
+ return BigInt(this.toU32(value))
141
+ } else {
142
+ assert(value.rest.length <= 32, 'Expect <= 32 bytes for U256')
143
+ return BigIntCodec.decode(Buffer.from(value.rest), false)
144
+ }
145
+ }
146
+
147
+ fromU256(value: bigint): DecodedCompactInt {
148
+ return this.decode(this.encodeU256(value))
149
+ }
150
+ }
151
+
152
+ export const compactUnsignedIntCodec = new CompactUnsignedIntCodec()
153
+
154
+ export class CompactSignedIntCodec implements Codec<DecodedCompactInt> {
155
+ private signFlag = 0x20 // 0b00100000
156
+ private oneByteBound = 0x20 // 0b00100000
157
+ private twoByteBound = this.oneByteBound << 8
158
+ private fourByteBound = this.oneByteBound << (8 * 3)
159
+
160
+ parser = compactIntParser
161
+
162
+ encode(input: DecodedCompactInt): Buffer {
163
+ return Buffer.from([input.mode, ...input.rest])
164
+ }
165
+
166
+ decode(input: Buffer): DecodedCompactInt {
167
+ return this.parser.parse(input)
168
+ }
169
+
170
+ decodeI32(input: Buffer): number {
171
+ const decoded = this.decode(input)
172
+ return this.toI32(decoded)
173
+ }
174
+
175
+ encodeI32(value: number): Buffer {
176
+ if (value >= 0) {
177
+ if (value < this.oneByteBound) {
178
+ return Buffer.from([(CompactInt.oneBytePrefix + value) & 0xff])
179
+ } else if (value < this.twoByteBound) {
180
+ return Buffer.from([(CompactInt.twoBytePrefix + (value >> 8)) & 0xff, value & 0xff])
181
+ } else if (value < this.fourByteBound) {
182
+ return Buffer.from([
183
+ (CompactInt.fourBytePrefix + (value >> 24)) & 0xff,
184
+ (value >> 16) & 0xff,
185
+ (value >> 8) & 0xff,
186
+ value & 0xff
187
+ ])
188
+ } else {
189
+ return Buffer.from([
190
+ CompactInt.multiBytePrefix,
191
+ (value >> 24) & 0xff,
192
+ (value >> 16) & 0xff,
193
+ (value >> 8) & 0xff,
194
+ value & 0xff
195
+ ])
196
+ }
197
+ } else {
198
+ if (value >= -this.oneByteBound) {
199
+ return Buffer.from([(value ^ CompactInt.oneByteNegPrefix) & 0xff])
200
+ } else if (value >= -this.twoByteBound) {
201
+ return Buffer.from([((value >> 8) ^ CompactInt.twoByteNegPrefix) & 0xff, value & 0xff])
202
+ } else if (value >= -this.fourByteBound) {
203
+ return Buffer.from([
204
+ ((value >> 24) ^ CompactInt.fourByteNegPrefix) & 0xff,
205
+ (value >> 16) & 0xff,
206
+ (value >> 8) & 0xff,
207
+ value & 0xff
208
+ ])
209
+ } else {
210
+ return Buffer.from([
211
+ CompactInt.multiBytePrefix,
212
+ (value >> 24) & 0xff,
213
+ (value >> 16) & 0xff,
214
+ (value >> 8) & 0xff,
215
+ value & 0xff
216
+ ])
217
+ }
218
+ }
219
+ }
220
+
221
+ encodeI256(value: bigint): Buffer {
222
+ if (value >= -0x20000000 && value < 0x20000000) {
223
+ return this.encodeI32(Number(value))
224
+ } else {
225
+ const bytes = BigIntCodec.encode(value)
226
+ const header = (bytes.length - 4 + CompactInt.multiBytePrefix) & 0xff
227
+ return Buffer.concat([Buffer.from([header]), bytes])
228
+ }
229
+ }
230
+
231
+ decodeI256(input: Buffer): bigint {
232
+ const decoded = this.decode(input)
233
+ return this.toI256(decoded)
234
+ }
235
+
236
+ toI32(value: DecodedCompactInt): number {
237
+ const body = Buffer.from([value.mode, ...value.rest])
238
+ const mode = value.mode & maskRest
239
+ if (fixedSize(mode)) {
240
+ const isPositive = (value.mode & signFlag) == 0
241
+ if (isPositive) {
242
+ return decodePositiveInt(value.mode, body)
243
+ } else {
244
+ return decodeNegativeInt(value.mode, body)
245
+ }
246
+ } else {
247
+ if (body.length === 5) {
248
+ return ((body[1] & 0xff) << 24) | ((body[2] & 0xff) << 16) | ((body[3] & 0xff) << 8) | (body[4] & 0xff)
249
+ } else {
250
+ throw new Error(`Expect 4 bytes int, but get ${body.length - 1} bytes int`)
251
+ }
252
+ }
253
+ }
254
+
255
+ fromI32(value: number): DecodedCompactInt {
256
+ return this.decode(this.encodeI32(value))
257
+ }
258
+
259
+ toI256(value: DecodedCompactInt): bigint {
260
+ const mode = value.mode & maskRest
261
+
262
+ if (fixedSize(mode)) {
263
+ return BigInt(this.toI32(value))
264
+ } else {
265
+ assert(value.rest.length <= 32, 'Expect <= 32 bytes for I256')
266
+ return BigIntCodec.decode(Buffer.from(value.rest), true)
267
+ }
268
+ }
269
+
270
+ fromI256(value: bigint): DecodedCompactInt {
271
+ return this.decode(this.encodeI256(value))
272
+ }
273
+ }
274
+
275
+ export const compactSignedIntCodec = new CompactSignedIntCodec()
276
+
277
+ function decodePositiveInt(rawMode: number, body: Buffer): number {
278
+ const mode = rawMode & maskRest
279
+
280
+ switch (mode) {
281
+ case CompactInt.oneBytePrefix:
282
+ return rawMode
283
+ case CompactInt.twoBytePrefix:
284
+ assert(body.length === 2, 'Length should be 2')
285
+ return ((body[0] & maskMode) << 8) | (body[1] & 0xff)
286
+ case CompactInt.fourBytePrefix:
287
+ assert(body.length === 4, 'Length should be 4')
288
+ return ((body[0] & maskMode) << 24) | ((body[1] & 0xff) << 16) | ((body[2] & 0xff) << 8) | (body[3] & 0xff)
289
+ default:
290
+ if (body.length === 5) {
291
+ return Number(BigInt('0x' + body.slice(1).toString('hex')))
292
+ } else {
293
+ throw new Error(`decodePositiveInt: Expect 4 bytes int, but get ${body.length - 1} bytes int`)
294
+ }
295
+ }
296
+ }
297
+
298
+ function decodeNegativeInt(rawMode: number, body: Buffer) {
299
+ const mode = rawMode & maskRest
300
+ switch (mode) {
301
+ case CompactInt.oneBytePrefix:
302
+ return rawMode | maskModeNeg
303
+ case CompactInt.twoBytePrefix:
304
+ assert(body.length === 2, 'Length should be 2')
305
+ return ((body[0] | maskModeNeg) << 8) | (body[1] & 0xff)
306
+ case CompactInt.fourBytePrefix:
307
+ assert(body.length === 4, 'Length should be 4')
308
+ return ((body[0] | maskModeNeg) << 24) | ((body[1] & 0xff) << 16) | ((body[2] & 0xff) << 8) | (body[3] & 0xff)
309
+ default:
310
+ throw new Error(`decodeNegativeInt: Expect 4 bytes int, but get ${body.length - 1} bytes int`)
311
+ }
312
+ }
313
+
314
+ function fixedSize(mode: number): boolean {
315
+ return mode === CompactInt.oneBytePrefix || mode === CompactInt.twoBytePrefix || mode === CompactInt.fourBytePrefix
316
+ }
@@ -0,0 +1,95 @@
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
+
19
+ import { Buffer } from 'buffer/'
20
+ import { Parser } from 'binary-parser'
21
+ import { ArrayCodec, DecodedArray } from './array-codec'
22
+ import { Codec } from './codec'
23
+ import { compactSignedIntCodec, compactUnsignedIntCodec, DecodedCompactInt } from './compact-int-codec'
24
+ import { Method, MethodCodec, methodCodec } from './method-codec'
25
+
26
+ const compactSignedIntsCodec = new ArrayCodec(compactSignedIntCodec)
27
+
28
+ export interface HalfDecodedContract {
29
+ fieldLength: DecodedCompactInt
30
+ methodIndexes: DecodedArray<DecodedCompactInt>
31
+ methods: Buffer
32
+ }
33
+
34
+ export interface Contract {
35
+ fieldLength: number
36
+ methods: Method[]
37
+ }
38
+
39
+ export function toHalfDecoded(contract: Contract): HalfDecodedContract {
40
+ const fieldLength = compactSignedIntCodec.fromI32(contract.fieldLength)
41
+ const methods = contract.methods.map((m) => methodCodec.encode(MethodCodec.fromMethod(m)))
42
+ let count = 0
43
+ const methodIndexes = Array.from(Array(methods.length).keys()).map((index) => {
44
+ count += methods[`${index}`].length
45
+ return count
46
+ })
47
+ return {
48
+ fieldLength,
49
+ methodIndexes: {
50
+ length: compactSignedIntCodec.fromI32(methodIndexes.length),
51
+ value: methodIndexes.map((value) => compactSignedIntCodec.fromI32(value))
52
+ },
53
+ methods: methods.reduce((acc, buffer) => Buffer.concat([acc, buffer]))
54
+ }
55
+ }
56
+
57
+ export class ContractCodec implements Codec<HalfDecodedContract> {
58
+ parser = Parser.start()
59
+ .nest('fieldLength', {
60
+ type: compactSignedIntCodec.parser
61
+ })
62
+ .nest('methodIndexes', {
63
+ type: compactSignedIntsCodec.parser
64
+ })
65
+ .buffer('methods', { readUntil: 'eof' })
66
+
67
+ encode(input: HalfDecodedContract): Buffer {
68
+ return Buffer.from([
69
+ ...compactSignedIntCodec.encode(input.fieldLength),
70
+ ...compactSignedIntsCodec.encode(input.methodIndexes.value),
71
+ ...input.methods
72
+ ])
73
+ }
74
+
75
+ decode(input: Buffer): HalfDecodedContract {
76
+ return this.parser.parse(input)
77
+ }
78
+
79
+ decodeContract(input: Buffer): Contract {
80
+ const halfDecoded = this.decode(input)
81
+ const fieldLength = compactUnsignedIntCodec.toU32(halfDecoded.fieldLength)
82
+ const methodIndexes = halfDecoded.methodIndexes.value.map((v) => compactUnsignedIntCodec.toU32(v))
83
+ const methods: Method[] = []
84
+ for (let i = 0, start = 0; i < methodIndexes.length; i++) {
85
+ const end = methodIndexes[i]
86
+ const method = MethodCodec.toMethod(methodCodec.decode(halfDecoded.methods.slice(start, end)))
87
+ methods.push(method)
88
+ start = end
89
+ }
90
+
91
+ return { fieldLength, methods }
92
+ }
93
+ }
94
+
95
+ export const contractCodec = new ContractCodec()
@@ -0,0 +1,95 @@
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 { DecodedArray } from './array-codec'
21
+ import { DecodedCompactInt, compactUnsignedIntCodec } from './compact-int-codec'
22
+ import { P2C } from './lockup-script-codec'
23
+ import { Codec } from './codec'
24
+ import { Token, tokensCodec } from './token-codec'
25
+ import { ContractOutput as ApiContractOutput } from '../api/api-alephium'
26
+ import { blakeHash, createHint } from './hash'
27
+ import { binToHex, bs58 } from '..'
28
+ import { signedIntCodec } from './signed-int-codec'
29
+ import { lockupScriptCodec } from './lockup-script-codec'
30
+
31
+ export interface ContractOutput {
32
+ amount: DecodedCompactInt
33
+ lockupScript: P2C
34
+ tokens: DecodedArray<Token>
35
+ }
36
+
37
+ export class ContractOutputCodec implements Codec<ContractOutput> {
38
+ parser = Parser.start()
39
+ .nest('amount', {
40
+ type: compactUnsignedIntCodec.parser
41
+ })
42
+ .nest('lockupScript', {
43
+ type: Parser.start().buffer('contractId', { length: 32 })
44
+ })
45
+ .nest('tokens', {
46
+ type: tokensCodec.parser
47
+ })
48
+
49
+ encode(input: ContractOutput): Buffer {
50
+ const amount = Buffer.from(compactUnsignedIntCodec.encode(input.amount))
51
+ const lockupScript = input.lockupScript.contractId
52
+ const tokens = Buffer.from(tokensCodec.encode(input.tokens.value))
53
+
54
+ return Buffer.concat([amount, lockupScript, tokens])
55
+ }
56
+
57
+ decode(input: Buffer): ContractOutput {
58
+ return this.parser.parse(input)
59
+ }
60
+
61
+ static convertToApiContractOutput(txIdBytes: Uint8Array, output: ContractOutput, index: number): ApiContractOutput {
62
+ const hint = createHint(output.lockupScript.contractId)
63
+ const key = binToHex(blakeHash(Buffer.concat([txIdBytes, signedIntCodec.encode(index)])))
64
+ const attoAlphAmount = compactUnsignedIntCodec.toU256(output.amount).toString()
65
+ const address = bs58.encode(Buffer.concat([Buffer.from([0x03]), output.lockupScript.contractId]))
66
+ const tokens = output.tokens.value.map((token) => {
67
+ return {
68
+ id: token.tokenId.toString('hex'),
69
+ amount: compactUnsignedIntCodec.toU256(token.amount).toString()
70
+ }
71
+ })
72
+ return { hint, key, attoAlphAmount, address, tokens, type: 'ContractOutput' }
73
+ }
74
+
75
+ static convertToOutput(apiContractOutput: ApiContractOutput): ContractOutput {
76
+ const amount: DecodedCompactInt = compactUnsignedIntCodec.fromU256(BigInt(apiContractOutput.attoAlphAmount))
77
+ const lockupScript: P2C = lockupScriptCodec.decode(Buffer.from(bs58.decode(apiContractOutput.address)))
78
+ .script as P2C
79
+
80
+ const tokensValue = apiContractOutput.tokens.map((token) => {
81
+ return {
82
+ tokenId: Buffer.from(token.id, 'hex'),
83
+ amount: compactUnsignedIntCodec.fromU256(BigInt(token.amount))
84
+ }
85
+ })
86
+ const tokens: DecodedArray<Token> = {
87
+ length: compactUnsignedIntCodec.fromU32(tokensValue.length),
88
+ value: tokensValue
89
+ }
90
+
91
+ return { amount, lockupScript, tokens }
92
+ }
93
+ }
94
+
95
+ export const contractOutputCodec = new ContractOutputCodec()
@@ -0,0 +1,42 @@
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 } from './array-codec'
21
+ import { Codec } from './codec'
22
+ import { signedIntCodec } from './signed-int-codec'
23
+
24
+ export interface ContractOutputRef {
25
+ hint: number
26
+ key: Buffer
27
+ }
28
+
29
+ export class ContractOutputRefCodec implements Codec<ContractOutputRef> {
30
+ parser = Parser.start().int32('hint').buffer('key', { length: 32 })
31
+
32
+ encode(input: ContractOutputRef): Buffer {
33
+ return Buffer.concat([Buffer.from([...signedIntCodec.encode(input.hint), ...input.key])])
34
+ }
35
+
36
+ decode(input: Buffer): ContractOutputRef {
37
+ return this.parser.parse(input)
38
+ }
39
+ }
40
+
41
+ export const contractOutputRefCodec = new ContractOutputRefCodec()
42
+ export const contractOutputRefsCodec = new ArrayCodec(contractOutputRefCodec)
@@ -0,0 +1,74 @@
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 } from './codec'
21
+
22
+ export interface Either<L, R> {
23
+ either: number
24
+ value: L | R
25
+ }
26
+
27
+ export class EitherCodec<L, R> implements Codec<Either<L, R>> {
28
+ constructor(
29
+ private leftCodec: Codec<L>,
30
+ private rightCodec: Codec<R>,
31
+ public parser = Parser.start()
32
+ .uint8('either')
33
+ .choice('value', {
34
+ tag: 'either',
35
+ choices: {
36
+ 0: leftCodec.parser,
37
+ 1: rightCodec.parser
38
+ }
39
+ })
40
+ ) {}
41
+
42
+ encode(input: Either<L, R>): Buffer {
43
+ const result = [input.either]
44
+ if (input.either === 0) {
45
+ result.push(...this.leftCodec.encode(input.value as L))
46
+ } else {
47
+ result.push(...this.rightCodec.encode(input.value as R))
48
+ }
49
+ return Buffer.from(result)
50
+ }
51
+
52
+ decode(input: Buffer): Either<L, R> {
53
+ const result = this.parser.parse(input)
54
+ return {
55
+ ...result,
56
+ value:
57
+ result.either === 0 ? this.leftCodec.decode(result.value.value) : this.rightCodec.decode(result.value.value)
58
+ }
59
+ }
60
+
61
+ fromLeft(left: L): Either<L, R> {
62
+ return {
63
+ either: 0,
64
+ value: left
65
+ }
66
+ }
67
+
68
+ fromRight(right: R): Either<L, R> {
69
+ return {
70
+ either: 1,
71
+ value: right
72
+ }
73
+ }
74
+ }
@@ -0,0 +1,35 @@
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 blake from 'blakejs'
20
+
21
+ export function blakeHash(raw: Uint8Array) {
22
+ return blake.blake2b(raw, undefined, 32)
23
+ }
24
+
25
+ export function djbIntHash(bytes: Buffer): number {
26
+ let hash = 5381
27
+ bytes.forEach((byte) => {
28
+ hash = (hash << 5) + hash + (byte & 0xff)
29
+ })
30
+ return hash
31
+ }
32
+
33
+ export function createHint(input: Buffer): number {
34
+ return djbIntHash(input) | 1
35
+ }