@alephium/web3 0.42.0 → 0.43.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/alephium-web3.min.js +1 -1
- package/dist/alephium-web3.min.js.map +1 -1
- package/dist/src/{utils → address}/address.js +15 -19
- package/dist/src/address/index.d.ts +1 -0
- package/dist/src/address/index.js +34 -0
- package/dist/src/api/node-provider.js +10 -9
- package/dist/src/api/types.js +1 -1
- package/dist/src/codec/array-codec.d.ts +2 -3
- package/dist/src/codec/array-codec.js +1 -2
- package/dist/src/codec/asset-output-codec.d.ts +3 -4
- package/dist/src/codec/asset-output-codec.js +10 -12
- package/dist/src/codec/bigint-codec.d.ts +2 -3
- package/dist/src/codec/bigint-codec.js +2 -3
- package/dist/src/codec/bytestring-codec.d.ts +5 -6
- package/dist/src/codec/bytestring-codec.js +5 -5
- package/dist/src/codec/codec.d.ts +2 -3
- package/dist/src/codec/compact-int-codec.d.ts +12 -13
- package/dist/src/codec/compact-int-codec.js +22 -22
- package/dist/src/codec/contract-codec.d.ts +5 -6
- package/dist/src/codec/contract-codec.js +24 -24
- package/dist/src/codec/contract-output-codec.d.ts +2 -3
- package/dist/src/codec/contract-output-codec.js +8 -10
- package/dist/src/codec/contract-output-ref-codec.d.ts +3 -4
- package/dist/src/codec/contract-output-ref-codec.js +2 -2
- package/dist/src/codec/either-codec.d.ts +2 -3
- package/dist/src/codec/either-codec.js +1 -2
- package/dist/src/codec/hash.d.ts +2 -3
- package/dist/src/codec/hash.js +17 -0
- package/dist/src/codec/input-codec.d.ts +3 -4
- package/dist/src/codec/input-codec.js +6 -6
- package/dist/src/codec/instr-codec.d.ts +2 -3
- package/dist/src/codec/instr-codec.js +1 -2
- package/dist/src/codec/lockup-script-codec.d.ts +5 -6
- package/dist/src/codec/lockup-script-codec.js +1 -2
- package/dist/src/codec/long-codec.d.ts +2 -3
- package/dist/src/codec/long-codec.js +1 -2
- package/dist/src/codec/method-codec.d.ts +2 -3
- package/dist/src/codec/method-codec.js +1 -2
- package/dist/src/codec/option-codec.d.ts +3 -4
- package/dist/src/codec/option-codec.js +2 -3
- package/dist/src/codec/script-codec.d.ts +4 -5
- package/dist/src/codec/script-codec.js +1 -3
- package/dist/src/codec/signature-codec.d.ts +3 -4
- package/dist/src/codec/signature-codec.js +17 -0
- package/dist/src/codec/signed-int-codec.d.ts +2 -3
- package/dist/src/codec/signed-int-codec.js +1 -2
- package/dist/src/codec/token-codec.d.ts +3 -4
- package/dist/src/codec/token-codec.js +3 -3
- package/dist/src/codec/transaction-codec.d.ts +4 -5
- package/dist/src/codec/transaction-codec.js +12 -13
- package/dist/src/codec/unlock-script-codec.d.ts +5 -6
- package/dist/src/codec/unlock-script-codec.js +14 -15
- package/dist/src/codec/unsigned-tx-codec.d.ts +4 -5
- package/dist/src/codec/unsigned-tx-codec.js +4 -5
- package/dist/src/contract/contract.js +20 -20
- package/dist/src/contract/ralph.d.ts +1 -1
- package/dist/src/contract/ralph.js +9 -10
- package/dist/src/{utils → exchange}/exchange.js +5 -5
- package/dist/src/exchange/index.d.ts +1 -0
- package/dist/src/exchange/index.js +25 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.js +2 -0
- package/dist/src/signer/signer.js +4 -5
- package/dist/src/signer/tx-builder.js +3 -3
- package/dist/src/utils/index.d.ts +0 -2
- package/dist/src/utils/index.js +0 -7
- package/dist/src/utils/sign.js +1 -1
- package/dist/src/utils/utils.d.ts +1 -0
- package/dist/src/utils/utils.js +30 -11
- package/dist/src/utils/webcrypto.js +3 -4
- package/package.json +1 -2
- package/src/{utils → address}/address.ts +15 -19
- package/src/address/index.ts +19 -0
- package/src/api/node-provider.ts +2 -9
- package/src/api/types.ts +2 -2
- package/src/codec/array-codec.ts +3 -4
- package/src/codec/asset-output-codec.ts +16 -18
- package/src/codec/bigint-codec.ts +4 -5
- package/src/codec/bytestring-codec.ts +8 -8
- package/src/codec/codec.ts +2 -3
- package/src/codec/compact-int-codec.ts +36 -37
- package/src/codec/contract-codec.ts +28 -27
- package/src/codec/contract-output-codec.ts +11 -13
- package/src/codec/contract-output-ref-codec.ts +5 -5
- package/src/codec/either-codec.ts +3 -4
- package/src/codec/hash.ts +2 -3
- package/src/codec/input-codec.ts +10 -10
- package/src/codec/instr-codec.ts +3 -4
- package/src/codec/lockup-script-codec.ts +8 -9
- package/src/codec/long-codec.ts +3 -4
- package/src/codec/method-codec.ts +3 -4
- package/src/codec/option-codec.ts +4 -5
- package/src/codec/script-codec.ts +5 -7
- package/src/codec/signature-codec.ts +3 -4
- package/src/codec/signed-int-codec.ts +3 -4
- package/src/codec/token-codec.ts +6 -6
- package/src/codec/transaction-codec.ts +17 -18
- package/src/codec/unlock-script-codec.ts +26 -27
- package/src/codec/unsigned-tx-codec.ts +10 -11
- package/src/contract/contract.ts +3 -7
- package/src/contract/ralph.ts +10 -11
- package/src/{utils → exchange}/exchange.ts +3 -11
- package/src/exchange/index.ts +19 -0
- package/src/index.ts +2 -0
- package/src/signer/signer.ts +2 -3
- package/src/signer/tx-builder.ts +2 -2
- package/src/utils/index.ts +0 -2
- package/src/utils/sign.ts +1 -1
- package/src/utils/utils.ts +29 -10
- package/src/utils/webcrypto.ts +3 -4
- package/webpack.config.js +1 -5
- /package/dist/src/{utils → address}/address.d.ts +0 -0
- /package/dist/src/{utils → exchange}/exchange.d.ts +0 -0
|
@@ -15,11 +15,10 @@ GNU Lesser General Public License for more details.
|
|
|
15
15
|
You should have received a copy of the GNU Lesser General Public License
|
|
16
16
|
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
17
|
*/
|
|
18
|
-
import { Buffer } from 'buffer/'
|
|
19
18
|
import { Parser } from 'binary-parser'
|
|
20
19
|
import { Codec, assert } from './codec'
|
|
21
20
|
import { BigIntCodec } from './bigint-codec'
|
|
22
|
-
import {
|
|
21
|
+
import { binToHex } from '../utils'
|
|
23
22
|
|
|
24
23
|
export class CompactInt {
|
|
25
24
|
static readonly oneBytePrefix = 0x00
|
|
@@ -66,24 +65,24 @@ export class CompactUnsignedIntCodec implements Codec<DecodedCompactInt> {
|
|
|
66
65
|
|
|
67
66
|
parser = compactIntParser
|
|
68
67
|
|
|
69
|
-
encode(input: DecodedCompactInt):
|
|
70
|
-
return
|
|
68
|
+
encode(input: DecodedCompactInt): Uint8Array {
|
|
69
|
+
return new Uint8Array([input.mode, ...input.rest])
|
|
71
70
|
}
|
|
72
71
|
|
|
73
|
-
encodeU32(value: number):
|
|
72
|
+
encodeU32(value: number): Uint8Array {
|
|
74
73
|
if (value < this.oneByteBound) {
|
|
75
|
-
return
|
|
74
|
+
return new Uint8Array([(CompactInt.oneBytePrefix + value) & 0xff])
|
|
76
75
|
} else if (value < this.twoByteBound) {
|
|
77
|
-
return
|
|
76
|
+
return new Uint8Array([(CompactInt.twoBytePrefix + (value >> 8)) & 0xff, value & 0xff])
|
|
78
77
|
} else if (value < this.fourByteBound) {
|
|
79
|
-
return
|
|
78
|
+
return new Uint8Array([
|
|
80
79
|
(CompactInt.fourBytePrefix + (value >> 24)) & 0xff,
|
|
81
80
|
(value >> 16) & 0xff,
|
|
82
81
|
(value >> 8) & 0xff,
|
|
83
82
|
value & 0xff
|
|
84
83
|
])
|
|
85
84
|
} else {
|
|
86
|
-
return
|
|
85
|
+
return new Uint8Array([
|
|
87
86
|
CompactInt.multiBytePrefix,
|
|
88
87
|
(value >> 24) & 0xff,
|
|
89
88
|
(value >> 16) & 0xff,
|
|
@@ -93,7 +92,7 @@ export class CompactUnsignedIntCodec implements Codec<DecodedCompactInt> {
|
|
|
93
92
|
}
|
|
94
93
|
}
|
|
95
94
|
|
|
96
|
-
encodeU256(value: bigint):
|
|
95
|
+
encodeU256(value: bigint): Uint8Array {
|
|
97
96
|
assert(value >= 0n, 'Value should be positive')
|
|
98
97
|
|
|
99
98
|
if (value < this.fourByteBound) {
|
|
@@ -107,26 +106,26 @@ export class CompactUnsignedIntCodec implements Codec<DecodedCompactInt> {
|
|
|
107
106
|
assert(bytes.length <= 32, 'Expect <= 32 bytes for U256')
|
|
108
107
|
|
|
109
108
|
const header = (bytes.length - 4 + CompactInt.multiBytePrefix) & 0xff
|
|
110
|
-
return
|
|
109
|
+
return new Uint8Array([header, ...bytes])
|
|
111
110
|
}
|
|
112
111
|
}
|
|
113
112
|
|
|
114
|
-
decodeU32(input:
|
|
113
|
+
decodeU32(input: Uint8Array): number {
|
|
115
114
|
const decoded = this.decode(input)
|
|
116
115
|
return this.toU32(decoded)
|
|
117
116
|
}
|
|
118
117
|
|
|
119
|
-
decodeU256(input:
|
|
118
|
+
decodeU256(input: Uint8Array): bigint {
|
|
120
119
|
const decoded = this.decode(input)
|
|
121
120
|
return this.toU256(decoded)
|
|
122
121
|
}
|
|
123
122
|
|
|
124
|
-
decode(input:
|
|
123
|
+
decode(input: Uint8Array): DecodedCompactInt {
|
|
125
124
|
return this.parser.parse(input)
|
|
126
125
|
}
|
|
127
126
|
|
|
128
127
|
toU32(value: DecodedCompactInt): number {
|
|
129
|
-
const body =
|
|
128
|
+
const body = new Uint8Array([value.mode, ...value.rest])
|
|
130
129
|
return decodePositiveInt(value.mode, body)
|
|
131
130
|
}
|
|
132
131
|
|
|
@@ -140,7 +139,7 @@ export class CompactUnsignedIntCodec implements Codec<DecodedCompactInt> {
|
|
|
140
139
|
return BigInt(this.toU32(value))
|
|
141
140
|
} else {
|
|
142
141
|
assert(value.rest.length <= 32, 'Expect <= 32 bytes for U256')
|
|
143
|
-
return BigIntCodec.decode(
|
|
142
|
+
return BigIntCodec.decode(value.rest, false)
|
|
144
143
|
}
|
|
145
144
|
}
|
|
146
145
|
|
|
@@ -159,34 +158,34 @@ export class CompactSignedIntCodec implements Codec<DecodedCompactInt> {
|
|
|
159
158
|
|
|
160
159
|
parser = compactIntParser
|
|
161
160
|
|
|
162
|
-
encode(input: DecodedCompactInt):
|
|
163
|
-
return
|
|
161
|
+
encode(input: DecodedCompactInt): Uint8Array {
|
|
162
|
+
return new Uint8Array([input.mode, ...input.rest])
|
|
164
163
|
}
|
|
165
164
|
|
|
166
|
-
decode(input:
|
|
165
|
+
decode(input: Uint8Array): DecodedCompactInt {
|
|
167
166
|
return this.parser.parse(input)
|
|
168
167
|
}
|
|
169
168
|
|
|
170
|
-
decodeI32(input:
|
|
169
|
+
decodeI32(input: Uint8Array): number {
|
|
171
170
|
const decoded = this.decode(input)
|
|
172
171
|
return this.toI32(decoded)
|
|
173
172
|
}
|
|
174
173
|
|
|
175
|
-
encodeI32(value: number):
|
|
174
|
+
encodeI32(value: number): Uint8Array {
|
|
176
175
|
if (value >= 0) {
|
|
177
176
|
if (value < this.oneByteBound) {
|
|
178
|
-
return
|
|
177
|
+
return new Uint8Array([(CompactInt.oneBytePrefix + value) & 0xff])
|
|
179
178
|
} else if (value < this.twoByteBound) {
|
|
180
|
-
return
|
|
179
|
+
return new Uint8Array([(CompactInt.twoBytePrefix + (value >> 8)) & 0xff, value & 0xff])
|
|
181
180
|
} else if (value < this.fourByteBound) {
|
|
182
|
-
return
|
|
181
|
+
return new Uint8Array([
|
|
183
182
|
(CompactInt.fourBytePrefix + (value >> 24)) & 0xff,
|
|
184
183
|
(value >> 16) & 0xff,
|
|
185
184
|
(value >> 8) & 0xff,
|
|
186
185
|
value & 0xff
|
|
187
186
|
])
|
|
188
187
|
} else {
|
|
189
|
-
return
|
|
188
|
+
return new Uint8Array([
|
|
190
189
|
CompactInt.multiBytePrefix,
|
|
191
190
|
(value >> 24) & 0xff,
|
|
192
191
|
(value >> 16) & 0xff,
|
|
@@ -196,18 +195,18 @@ export class CompactSignedIntCodec implements Codec<DecodedCompactInt> {
|
|
|
196
195
|
}
|
|
197
196
|
} else {
|
|
198
197
|
if (value >= -this.oneByteBound) {
|
|
199
|
-
return
|
|
198
|
+
return new Uint8Array([(value ^ CompactInt.oneByteNegPrefix) & 0xff])
|
|
200
199
|
} else if (value >= -this.twoByteBound) {
|
|
201
|
-
return
|
|
200
|
+
return new Uint8Array([((value >> 8) ^ CompactInt.twoByteNegPrefix) & 0xff, value & 0xff])
|
|
202
201
|
} else if (value >= -this.fourByteBound) {
|
|
203
|
-
return
|
|
202
|
+
return new Uint8Array([
|
|
204
203
|
((value >> 24) ^ CompactInt.fourByteNegPrefix) & 0xff,
|
|
205
204
|
(value >> 16) & 0xff,
|
|
206
205
|
(value >> 8) & 0xff,
|
|
207
206
|
value & 0xff
|
|
208
207
|
])
|
|
209
208
|
} else {
|
|
210
|
-
return
|
|
209
|
+
return new Uint8Array([
|
|
211
210
|
CompactInt.multiBytePrefix,
|
|
212
211
|
(value >> 24) & 0xff,
|
|
213
212
|
(value >> 16) & 0xff,
|
|
@@ -218,23 +217,23 @@ export class CompactSignedIntCodec implements Codec<DecodedCompactInt> {
|
|
|
218
217
|
}
|
|
219
218
|
}
|
|
220
219
|
|
|
221
|
-
encodeI256(value: bigint):
|
|
220
|
+
encodeI256(value: bigint): Uint8Array {
|
|
222
221
|
if (value >= -0x20000000 && value < 0x20000000) {
|
|
223
222
|
return this.encodeI32(Number(value))
|
|
224
223
|
} else {
|
|
225
224
|
const bytes = BigIntCodec.encode(value)
|
|
226
225
|
const header = (bytes.length - 4 + CompactInt.multiBytePrefix) & 0xff
|
|
227
|
-
return
|
|
226
|
+
return new Uint8Array([header, ...bytes])
|
|
228
227
|
}
|
|
229
228
|
}
|
|
230
229
|
|
|
231
|
-
decodeI256(input:
|
|
230
|
+
decodeI256(input: Uint8Array): bigint {
|
|
232
231
|
const decoded = this.decode(input)
|
|
233
232
|
return this.toI256(decoded)
|
|
234
233
|
}
|
|
235
234
|
|
|
236
235
|
toI32(value: DecodedCompactInt): number {
|
|
237
|
-
const body =
|
|
236
|
+
const body = new Uint8Array([value.mode, ...value.rest])
|
|
238
237
|
const mode = value.mode & maskRest
|
|
239
238
|
if (fixedSize(mode)) {
|
|
240
239
|
const isPositive = (value.mode & signFlag) == 0
|
|
@@ -263,7 +262,7 @@ export class CompactSignedIntCodec implements Codec<DecodedCompactInt> {
|
|
|
263
262
|
return BigInt(this.toI32(value))
|
|
264
263
|
} else {
|
|
265
264
|
assert(value.rest.length <= 32, 'Expect <= 32 bytes for I256')
|
|
266
|
-
return BigIntCodec.decode(
|
|
265
|
+
return BigIntCodec.decode(value.rest, true)
|
|
267
266
|
}
|
|
268
267
|
}
|
|
269
268
|
|
|
@@ -274,7 +273,7 @@ export class CompactSignedIntCodec implements Codec<DecodedCompactInt> {
|
|
|
274
273
|
|
|
275
274
|
export const compactSignedIntCodec = new CompactSignedIntCodec()
|
|
276
275
|
|
|
277
|
-
function decodePositiveInt(rawMode: number, body:
|
|
276
|
+
function decodePositiveInt(rawMode: number, body: Uint8Array): number {
|
|
278
277
|
const mode = rawMode & maskRest
|
|
279
278
|
|
|
280
279
|
switch (mode) {
|
|
@@ -288,14 +287,14 @@ function decodePositiveInt(rawMode: number, body: Buffer): number {
|
|
|
288
287
|
return ((body[0] & maskMode) << 24) | ((body[1] & 0xff) << 16) | ((body[2] & 0xff) << 8) | (body[3] & 0xff)
|
|
289
288
|
default:
|
|
290
289
|
if (body.length === 5) {
|
|
291
|
-
return Number(BigInt('0x' + body.slice(1)
|
|
290
|
+
return Number(BigInt('0x' + binToHex(body.slice(1))))
|
|
292
291
|
} else {
|
|
293
292
|
throw new Error(`decodePositiveInt: Expect 4 bytes int, but get ${body.length - 1} bytes int`)
|
|
294
293
|
}
|
|
295
294
|
}
|
|
296
295
|
}
|
|
297
296
|
|
|
298
|
-
function decodeNegativeInt(rawMode: number, body:
|
|
297
|
+
function decodeNegativeInt(rawMode: number, body: Uint8Array) {
|
|
299
298
|
const mode = rawMode & maskRest
|
|
300
299
|
switch (mode) {
|
|
301
300
|
case CompactInt.oneBytePrefix:
|
|
@@ -16,19 +16,19 @@ You should have received a copy of the GNU Lesser General Public License
|
|
|
16
16
|
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
-
import { Buffer } from 'buffer/'
|
|
20
19
|
import { Parser } from 'binary-parser'
|
|
21
20
|
import { ArrayCodec, DecodedArray } from './array-codec'
|
|
22
21
|
import { Codec } from './codec'
|
|
23
22
|
import { compactSignedIntCodec, compactUnsignedIntCodec, DecodedCompactInt } from './compact-int-codec'
|
|
24
23
|
import { Method, MethodCodec, methodCodec } from './method-codec'
|
|
24
|
+
import { concatBytes } from '../utils'
|
|
25
25
|
|
|
26
26
|
const compactSignedIntsCodec = new ArrayCodec(compactSignedIntCodec)
|
|
27
27
|
|
|
28
28
|
export interface HalfDecodedContract {
|
|
29
29
|
fieldLength: DecodedCompactInt
|
|
30
30
|
methodIndexes: DecodedArray<DecodedCompactInt>
|
|
31
|
-
methods:
|
|
31
|
+
methods: Uint8Array
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
export interface Contract {
|
|
@@ -36,24 +36,6 @@ export interface Contract {
|
|
|
36
36
|
methods: Method[]
|
|
37
37
|
}
|
|
38
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
39
|
export class ContractCodec implements Codec<HalfDecodedContract> {
|
|
58
40
|
parser = Parser.start()
|
|
59
41
|
.nest('fieldLength', {
|
|
@@ -64,19 +46,19 @@ export class ContractCodec implements Codec<HalfDecodedContract> {
|
|
|
64
46
|
})
|
|
65
47
|
.buffer('methods', { readUntil: 'eof' })
|
|
66
48
|
|
|
67
|
-
encode(input: HalfDecodedContract):
|
|
68
|
-
return
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
49
|
+
encode(input: HalfDecodedContract): Uint8Array {
|
|
50
|
+
return concatBytes([
|
|
51
|
+
compactSignedIntCodec.encode(input.fieldLength),
|
|
52
|
+
compactSignedIntsCodec.encode(input.methodIndexes.value),
|
|
53
|
+
input.methods
|
|
72
54
|
])
|
|
73
55
|
}
|
|
74
56
|
|
|
75
|
-
decode(input:
|
|
57
|
+
decode(input: Uint8Array): HalfDecodedContract {
|
|
76
58
|
return this.parser.parse(input)
|
|
77
59
|
}
|
|
78
60
|
|
|
79
|
-
decodeContract(input:
|
|
61
|
+
decodeContract(input: Uint8Array): Contract {
|
|
80
62
|
const halfDecoded = this.decode(input)
|
|
81
63
|
const fieldLength = compactUnsignedIntCodec.toU32(halfDecoded.fieldLength)
|
|
82
64
|
const methodIndexes = halfDecoded.methodIndexes.value.map((v) => compactUnsignedIntCodec.toU32(v))
|
|
@@ -90,6 +72,25 @@ export class ContractCodec implements Codec<HalfDecodedContract> {
|
|
|
90
72
|
|
|
91
73
|
return { fieldLength, methods }
|
|
92
74
|
}
|
|
75
|
+
|
|
76
|
+
encodeContract(contract: Contract): Uint8Array {
|
|
77
|
+
const fieldLength = compactSignedIntCodec.fromI32(contract.fieldLength)
|
|
78
|
+
const methods = contract.methods.map((m) => methodCodec.encode(MethodCodec.fromMethod(m)))
|
|
79
|
+
let count = 0
|
|
80
|
+
const methodIndexes = Array.from(Array(methods.length).keys()).map((index) => {
|
|
81
|
+
count += methods[`${index}`].length
|
|
82
|
+
return count
|
|
83
|
+
})
|
|
84
|
+
const halfDecoded = {
|
|
85
|
+
fieldLength,
|
|
86
|
+
methodIndexes: {
|
|
87
|
+
length: compactSignedIntCodec.fromI32(methodIndexes.length),
|
|
88
|
+
value: methodIndexes.map((value) => compactSignedIntCodec.fromI32(value))
|
|
89
|
+
},
|
|
90
|
+
methods: concatBytes(methods)
|
|
91
|
+
}
|
|
92
|
+
return this.encode(halfDecoded)
|
|
93
|
+
}
|
|
93
94
|
}
|
|
94
95
|
|
|
95
96
|
export const contractCodec = new ContractCodec()
|
|
@@ -15,7 +15,6 @@ GNU Lesser General Public License for more details.
|
|
|
15
15
|
You should have received a copy of the GNU Lesser General Public License
|
|
16
16
|
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
17
|
*/
|
|
18
|
-
import { Buffer } from 'buffer/'
|
|
19
18
|
import { Parser } from 'binary-parser'
|
|
20
19
|
import { DecodedArray } from './array-codec'
|
|
21
20
|
import { DecodedCompactInt, compactUnsignedIntCodec } from './compact-int-codec'
|
|
@@ -24,7 +23,7 @@ import { Codec } from './codec'
|
|
|
24
23
|
import { Token, tokensCodec } from './token-codec'
|
|
25
24
|
import { ContractOutput as ApiContractOutput } from '../api/api-alephium'
|
|
26
25
|
import { blakeHash, createHint } from './hash'
|
|
27
|
-
import { binToHex, bs58 } from '../utils'
|
|
26
|
+
import { binToHex, bs58, concatBytes, hexToBinUnsafe } from '../utils'
|
|
28
27
|
import { signedIntCodec } from './signed-int-codec'
|
|
29
28
|
import { lockupScriptCodec } from './lockup-script-codec'
|
|
30
29
|
|
|
@@ -46,26 +45,26 @@ export class ContractOutputCodec implements Codec<ContractOutput> {
|
|
|
46
45
|
type: tokensCodec.parser
|
|
47
46
|
})
|
|
48
47
|
|
|
49
|
-
encode(input: ContractOutput):
|
|
50
|
-
const amount =
|
|
48
|
+
encode(input: ContractOutput): Uint8Array {
|
|
49
|
+
const amount = compactUnsignedIntCodec.encode(input.amount)
|
|
51
50
|
const lockupScript = input.lockupScript.contractId
|
|
52
|
-
const tokens =
|
|
51
|
+
const tokens = tokensCodec.encode(input.tokens.value)
|
|
53
52
|
|
|
54
|
-
return
|
|
53
|
+
return concatBytes([amount, lockupScript, tokens])
|
|
55
54
|
}
|
|
56
55
|
|
|
57
|
-
decode(input:
|
|
56
|
+
decode(input: Uint8Array): ContractOutput {
|
|
58
57
|
return this.parser.parse(input)
|
|
59
58
|
}
|
|
60
59
|
|
|
61
60
|
static convertToApiContractOutput(txIdBytes: Uint8Array, output: ContractOutput, index: number): ApiContractOutput {
|
|
62
61
|
const hint = createHint(output.lockupScript.contractId)
|
|
63
|
-
const key = binToHex(blakeHash(
|
|
62
|
+
const key = binToHex(blakeHash(concatBytes([txIdBytes, signedIntCodec.encode(index)])))
|
|
64
63
|
const attoAlphAmount = compactUnsignedIntCodec.toU256(output.amount).toString()
|
|
65
|
-
const address = bs58.encode(
|
|
64
|
+
const address = bs58.encode(new Uint8Array([0x03, ...output.lockupScript.contractId]))
|
|
66
65
|
const tokens = output.tokens.value.map((token) => {
|
|
67
66
|
return {
|
|
68
|
-
id: token.tokenId
|
|
67
|
+
id: binToHex(token.tokenId),
|
|
69
68
|
amount: compactUnsignedIntCodec.toU256(token.amount).toString()
|
|
70
69
|
}
|
|
71
70
|
})
|
|
@@ -74,12 +73,11 @@ export class ContractOutputCodec implements Codec<ContractOutput> {
|
|
|
74
73
|
|
|
75
74
|
static convertToOutput(apiContractOutput: ApiContractOutput): ContractOutput {
|
|
76
75
|
const amount: DecodedCompactInt = compactUnsignedIntCodec.fromU256(BigInt(apiContractOutput.attoAlphAmount))
|
|
77
|
-
const lockupScript: P2C = lockupScriptCodec.decode(
|
|
78
|
-
.script as P2C
|
|
76
|
+
const lockupScript: P2C = lockupScriptCodec.decode(bs58.decode(apiContractOutput.address)).script as P2C
|
|
79
77
|
|
|
80
78
|
const tokensValue = apiContractOutput.tokens.map((token) => {
|
|
81
79
|
return {
|
|
82
|
-
tokenId:
|
|
80
|
+
tokenId: hexToBinUnsafe(token.id),
|
|
83
81
|
amount: compactUnsignedIntCodec.fromU256(BigInt(token.amount))
|
|
84
82
|
}
|
|
85
83
|
})
|
|
@@ -15,25 +15,25 @@ GNU Lesser General Public License for more details.
|
|
|
15
15
|
You should have received a copy of the GNU Lesser General Public License
|
|
16
16
|
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
17
|
*/
|
|
18
|
-
import { Buffer } from 'buffer/'
|
|
19
18
|
import { Parser } from 'binary-parser'
|
|
20
19
|
import { ArrayCodec } from './array-codec'
|
|
21
20
|
import { Codec } from './codec'
|
|
22
21
|
import { signedIntCodec } from './signed-int-codec'
|
|
22
|
+
import { concatBytes } from '../utils'
|
|
23
23
|
|
|
24
24
|
export interface ContractOutputRef {
|
|
25
25
|
hint: number
|
|
26
|
-
key:
|
|
26
|
+
key: Uint8Array
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
export class ContractOutputRefCodec implements Codec<ContractOutputRef> {
|
|
30
30
|
parser = Parser.start().int32('hint').buffer('key', { length: 32 })
|
|
31
31
|
|
|
32
|
-
encode(input: ContractOutputRef):
|
|
33
|
-
return
|
|
32
|
+
encode(input: ContractOutputRef): Uint8Array {
|
|
33
|
+
return concatBytes([signedIntCodec.encode(input.hint), input.key])
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
decode(input:
|
|
36
|
+
decode(input: Uint8Array): ContractOutputRef {
|
|
37
37
|
return this.parser.parse(input)
|
|
38
38
|
}
|
|
39
39
|
}
|
|
@@ -15,7 +15,6 @@ GNU Lesser General Public License for more details.
|
|
|
15
15
|
You should have received a copy of the GNU Lesser General Public License
|
|
16
16
|
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
17
|
*/
|
|
18
|
-
import { Buffer } from 'buffer/'
|
|
19
18
|
import { Parser } from 'binary-parser'
|
|
20
19
|
import { Codec } from './codec'
|
|
21
20
|
|
|
@@ -39,17 +38,17 @@ export class EitherCodec<L, R> implements Codec<Either<L, R>> {
|
|
|
39
38
|
})
|
|
40
39
|
) {}
|
|
41
40
|
|
|
42
|
-
encode(input: Either<L, R>):
|
|
41
|
+
encode(input: Either<L, R>): Uint8Array {
|
|
43
42
|
const result = [input.either]
|
|
44
43
|
if (input.either === 0) {
|
|
45
44
|
result.push(...this.leftCodec.encode(input.value as L))
|
|
46
45
|
} else {
|
|
47
46
|
result.push(...this.rightCodec.encode(input.value as R))
|
|
48
47
|
}
|
|
49
|
-
return
|
|
48
|
+
return new Uint8Array(result)
|
|
50
49
|
}
|
|
51
50
|
|
|
52
|
-
decode(input:
|
|
51
|
+
decode(input: Uint8Array): Either<L, R> {
|
|
53
52
|
const result = this.parser.parse(input)
|
|
54
53
|
return {
|
|
55
54
|
...result,
|
package/src/codec/hash.ts
CHANGED
|
@@ -15,14 +15,13 @@ GNU Lesser General Public License for more details.
|
|
|
15
15
|
You should have received a copy of the GNU Lesser General Public License
|
|
16
16
|
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
17
|
*/
|
|
18
|
-
import { Buffer } from 'buffer/'
|
|
19
18
|
import blake from 'blakejs'
|
|
20
19
|
|
|
21
20
|
export function blakeHash(raw: Uint8Array) {
|
|
22
21
|
return blake.blake2b(raw, undefined, 32)
|
|
23
22
|
}
|
|
24
23
|
|
|
25
|
-
export function djbIntHash(bytes:
|
|
24
|
+
export function djbIntHash(bytes: Uint8Array): number {
|
|
26
25
|
let hash = 5381
|
|
27
26
|
bytes.forEach((byte) => {
|
|
28
27
|
hash = (hash << 5) + hash + (byte & 0xff)
|
|
@@ -30,6 +29,6 @@ export function djbIntHash(bytes: Buffer): number {
|
|
|
30
29
|
return hash
|
|
31
30
|
}
|
|
32
31
|
|
|
33
|
-
export function createHint(input:
|
|
32
|
+
export function createHint(input: Uint8Array): number {
|
|
34
33
|
return djbIntHash(input) | 1
|
|
35
34
|
}
|
package/src/codec/input-codec.ts
CHANGED
|
@@ -15,10 +15,9 @@ GNU Lesser General Public License for more details.
|
|
|
15
15
|
You should have received a copy of the GNU Lesser General Public License
|
|
16
16
|
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
17
|
*/
|
|
18
|
-
import { Buffer } from 'buffer/'
|
|
19
18
|
import { Parser } from 'binary-parser'
|
|
20
19
|
import { AssetInput } from '../api/api-alephium'
|
|
21
|
-
import { binToHex } from '../utils'
|
|
20
|
+
import { binToHex, concatBytes, hexToBinUnsafe } from '../utils'
|
|
22
21
|
import { UnlockScript, unlockScriptCodec } from './unlock-script-codec'
|
|
23
22
|
import { Codec } from './codec'
|
|
24
23
|
import { signedIntCodec } from './signed-int-codec'
|
|
@@ -27,7 +26,7 @@ import { ArrayCodec } from './array-codec'
|
|
|
27
26
|
export interface Input {
|
|
28
27
|
outputRef: {
|
|
29
28
|
hint: number
|
|
30
|
-
key:
|
|
29
|
+
key: Uint8Array
|
|
31
30
|
}
|
|
32
31
|
unlockScript: UnlockScript
|
|
33
32
|
}
|
|
@@ -41,14 +40,15 @@ export class InputCodec implements Codec<Input> {
|
|
|
41
40
|
type: unlockScriptCodec.parser
|
|
42
41
|
})
|
|
43
42
|
|
|
44
|
-
encode(input: Input):
|
|
45
|
-
return
|
|
46
|
-
|
|
43
|
+
encode(input: Input): Uint8Array {
|
|
44
|
+
return concatBytes([
|
|
45
|
+
signedIntCodec.encode(input.outputRef.hint),
|
|
46
|
+
input.outputRef.key,
|
|
47
47
|
unlockScriptCodec.encode(input.unlockScript)
|
|
48
48
|
])
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
-
decode(input:
|
|
51
|
+
decode(input: Uint8Array): Input {
|
|
52
52
|
return this.parser.parse(input)
|
|
53
53
|
}
|
|
54
54
|
|
|
@@ -59,7 +59,7 @@ export class InputCodec implements Codec<Input> {
|
|
|
59
59
|
const unlockScript = unlockScriptCodec.encode(input.unlockScript)
|
|
60
60
|
return {
|
|
61
61
|
outputRef: { hint, key },
|
|
62
|
-
unlockScript: unlockScript
|
|
62
|
+
unlockScript: binToHex(unlockScript)
|
|
63
63
|
}
|
|
64
64
|
})
|
|
65
65
|
}
|
|
@@ -67,8 +67,8 @@ export class InputCodec implements Codec<Input> {
|
|
|
67
67
|
static fromAssetInputs(inputs: AssetInput[]): Input[] {
|
|
68
68
|
return inputs.map((input) => {
|
|
69
69
|
const hint = input.outputRef.hint
|
|
70
|
-
const key =
|
|
71
|
-
const unlockScript = unlockScriptCodec.decode(
|
|
70
|
+
const key = hexToBinUnsafe(input.outputRef.key)
|
|
71
|
+
const unlockScript = unlockScriptCodec.decode(hexToBinUnsafe(input.unlockScript))
|
|
72
72
|
return {
|
|
73
73
|
outputRef: { hint, key },
|
|
74
74
|
unlockScript
|
package/src/codec/instr-codec.ts
CHANGED
|
@@ -15,7 +15,6 @@ GNU Lesser General Public License for more details.
|
|
|
15
15
|
You should have received a copy of the GNU Lesser General Public License
|
|
16
16
|
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
17
|
*/
|
|
18
|
-
import { Buffer } from 'buffer/'
|
|
19
18
|
import { Parser } from 'binary-parser'
|
|
20
19
|
import { ArrayCodec, DecodedArray } from './array-codec'
|
|
21
20
|
import { compactUnsignedIntCodec, compactSignedIntCodec, DecodedCompactInt } from './compact-int-codec'
|
|
@@ -458,7 +457,7 @@ export class InstrCodec implements Codec<Instr> {
|
|
|
458
457
|
}
|
|
459
458
|
})
|
|
460
459
|
|
|
461
|
-
encode(instr: Instr):
|
|
460
|
+
encode(instr: Instr): Uint8Array {
|
|
462
461
|
const instrValue = instr.value
|
|
463
462
|
const result = [instr.code]
|
|
464
463
|
const instrsWithIndex = [0x00, 0x01, 0x16, 0x17, 0xa0, 0xa1, 0xce]
|
|
@@ -480,10 +479,10 @@ export class InstrCodec implements Codec<Instr> {
|
|
|
480
479
|
result.push(...signedIntCodec.encode((instrValue as InstrValueWithIndex).index))
|
|
481
480
|
}
|
|
482
481
|
|
|
483
|
-
return
|
|
482
|
+
return new Uint8Array(result)
|
|
484
483
|
}
|
|
485
484
|
|
|
486
|
-
decode(input:
|
|
485
|
+
decode(input: Uint8Array): Instr {
|
|
487
486
|
return this.parser.parse(input)
|
|
488
487
|
}
|
|
489
488
|
}
|
|
@@ -15,24 +15,23 @@ GNU Lesser General Public License for more details.
|
|
|
15
15
|
You should have received a copy of the GNU Lesser General Public License
|
|
16
16
|
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
17
|
*/
|
|
18
|
-
import { Buffer } from 'buffer/'
|
|
19
18
|
import { Parser } from 'binary-parser'
|
|
20
19
|
import { DecodedCompactInt, compactSignedIntCodec } from './compact-int-codec'
|
|
21
20
|
import { Codec } from './codec'
|
|
22
21
|
import { ArrayCodec, DecodedArray } from './array-codec'
|
|
23
22
|
|
|
24
23
|
export interface PublicKeyHash {
|
|
25
|
-
publicKeyHash:
|
|
24
|
+
publicKeyHash: Uint8Array
|
|
26
25
|
}
|
|
27
26
|
|
|
28
27
|
class PublicKeyHashCodec implements Codec<PublicKeyHash> {
|
|
29
28
|
parser = Parser.start().buffer('publicKeyHash', { length: 32 })
|
|
30
29
|
|
|
31
|
-
encode(input: PublicKeyHash):
|
|
30
|
+
encode(input: PublicKeyHash): Uint8Array {
|
|
32
31
|
return input.publicKeyHash
|
|
33
32
|
}
|
|
34
33
|
|
|
35
|
-
decode(input:
|
|
34
|
+
decode(input: Uint8Array): PublicKeyHash {
|
|
36
35
|
return this.parser.parse(input)
|
|
37
36
|
}
|
|
38
37
|
}
|
|
@@ -48,11 +47,11 @@ export interface MultiSig {
|
|
|
48
47
|
}
|
|
49
48
|
|
|
50
49
|
export interface P2SH {
|
|
51
|
-
scriptHash:
|
|
50
|
+
scriptHash: Uint8Array
|
|
52
51
|
}
|
|
53
52
|
|
|
54
53
|
export interface P2C {
|
|
55
|
-
contractId:
|
|
54
|
+
contractId: Uint8Array
|
|
56
55
|
}
|
|
57
56
|
|
|
58
57
|
export interface LockupScript {
|
|
@@ -73,7 +72,7 @@ export class LockupScriptCodec implements Codec<LockupScript> {
|
|
|
73
72
|
}
|
|
74
73
|
})
|
|
75
74
|
|
|
76
|
-
encode(input: LockupScript):
|
|
75
|
+
encode(input: LockupScript): Uint8Array {
|
|
77
76
|
const result: number[] = [input.scriptType]
|
|
78
77
|
if (input.scriptType === 0) {
|
|
79
78
|
result.push(...(input.script as PublicKeyHash).publicKeyHash)
|
|
@@ -88,10 +87,10 @@ export class LockupScriptCodec implements Codec<LockupScript> {
|
|
|
88
87
|
throw new Error(`Unsupported script type: ${input.scriptType}`)
|
|
89
88
|
}
|
|
90
89
|
|
|
91
|
-
return
|
|
90
|
+
return new Uint8Array(result)
|
|
92
91
|
}
|
|
93
92
|
|
|
94
|
-
decode(input:
|
|
93
|
+
decode(input: Uint8Array): LockupScript {
|
|
95
94
|
return this.parser.parse(input)
|
|
96
95
|
}
|
|
97
96
|
}
|
package/src/codec/long-codec.ts
CHANGED
|
@@ -15,7 +15,6 @@ GNU Lesser General Public License for more details.
|
|
|
15
15
|
You should have received a copy of the GNU Lesser General Public License
|
|
16
16
|
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
17
|
*/
|
|
18
|
-
import { Buffer } from 'buffer/'
|
|
19
18
|
import { Parser } from 'binary-parser'
|
|
20
19
|
import { Codec, assert } from './codec'
|
|
21
20
|
|
|
@@ -24,7 +23,7 @@ export class LongCodec implements Codec<bigint> {
|
|
|
24
23
|
length: 8
|
|
25
24
|
})
|
|
26
25
|
|
|
27
|
-
encode(input: bigint):
|
|
26
|
+
encode(input: bigint): Uint8Array {
|
|
28
27
|
const byteArray = new Uint8Array(8)
|
|
29
28
|
|
|
30
29
|
assert(byteArray.length <= 8, 'Length should be less than or equal to 8')
|
|
@@ -34,10 +33,10 @@ export class LongCodec implements Codec<bigint> {
|
|
|
34
33
|
input >>= BigInt(8)
|
|
35
34
|
}
|
|
36
35
|
|
|
37
|
-
return
|
|
36
|
+
return byteArray
|
|
38
37
|
}
|
|
39
38
|
|
|
40
|
-
decode(bytes:
|
|
39
|
+
decode(bytes: Uint8Array): bigint {
|
|
41
40
|
assert(bytes.length == 8, 'Length should be 8')
|
|
42
41
|
let int64 = BigInt(0)
|
|
43
42
|
let pow = BigInt(1)
|