@alephium/web3 0.37.0 → 0.38.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/alephium-web3.min.js +1 -1
- package/dist/alephium-web3.min.js.map +1 -1
- package/dist/src/api/api-alephium.d.ts +5 -0
- package/dist/src/api/types.d.ts +1 -1
- package/dist/src/codec/array-codec.d.ts +17 -0
- package/dist/src/codec/array-codec.js +59 -0
- package/dist/src/codec/asset-output-codec.d.ts +27 -0
- package/dist/src/codec/asset-output-codec.js +135 -0
- package/dist/src/codec/bigint-codec.d.ts +5 -0
- package/dist/src/codec/bigint-codec.js +86 -0
- package/dist/src/codec/bytestring-codec.d.ts +16 -0
- package/dist/src/codec/bytestring-codec.js +50 -0
- package/dist/src/codec/codec.d.ts +8 -0
- package/dist/src/codec/codec.js +9 -0
- package/dist/src/codec/compact-int-codec.d.ts +51 -0
- package/dist/src/codec/compact-int-codec.js +300 -0
- package/dist/src/codec/contract-codec.d.ts +23 -0
- package/dist/src/codec/contract-codec.js +81 -0
- package/dist/src/codec/contract-output-codec.d.ts +21 -0
- package/dist/src/codec/contract-output-codec.js +82 -0
- package/dist/src/codec/contract-output-ref-codec.d.ts +15 -0
- package/dist/src/codec/contract-output-ref-codec.js +38 -0
- package/dist/src/codec/either-codec.d.ts +17 -0
- package/dist/src/codec/either-codec.js +67 -0
- package/dist/src/codec/hash.d.ts +4 -0
- package/dist/src/codec/hash.js +23 -0
- package/dist/src/codec/index.d.ts +23 -0
- package/dist/src/codec/index.js +69 -0
- package/dist/src/codec/input-codec.d.ts +22 -0
- package/dist/src/codec/input-codec.js +71 -0
- package/dist/src/codec/instr-codec.d.ts +230 -0
- package/dist/src/codec/instr-codec.js +471 -0
- package/dist/src/codec/lockup-script-codec.d.ts +28 -0
- package/dist/src/codec/lockup-script-codec.js +80 -0
- package/dist/src/codec/long-codec.d.ts +9 -0
- package/dist/src/codec/long-codec.js +56 -0
- package/dist/src/codec/method-codec.d.ts +31 -0
- package/dist/src/codec/method-codec.js +78 -0
- package/dist/src/codec/option-codec.d.ts +15 -0
- package/dist/src/codec/option-codec.js +55 -0
- package/dist/src/codec/output-codec.d.ts +7 -0
- package/dist/src/codec/output-codec.js +26 -0
- package/dist/src/codec/script-codec.d.ts +16 -0
- package/dist/src/codec/script-codec.js +41 -0
- package/dist/src/codec/signature-codec.d.ts +14 -0
- package/dist/src/codec/signature-codec.js +19 -0
- package/dist/src/codec/signed-int-codec.d.ts +9 -0
- package/dist/src/codec/signed-int-codec.js +39 -0
- package/dist/src/codec/token-codec.d.ts +16 -0
- package/dist/src/codec/token-codec.js +46 -0
- package/dist/src/codec/transaction-codec.d.ts +27 -0
- package/dist/src/codec/transaction-codec.js +128 -0
- package/dist/src/codec/unlock-script-codec.d.ts +40 -0
- package/dist/src/codec/unlock-script-codec.js +170 -0
- package/dist/src/codec/unsigned-tx-codec.d.ts +30 -0
- package/dist/src/codec/unsigned-tx-codec.js +103 -0
- package/dist/src/contract/contract.d.ts +10 -4
- package/dist/src/contract/contract.js +184 -11
- package/dist/src/contract/ralph.d.ts +16 -0
- package/dist/src/contract/ralph.js +127 -1
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +2 -1
- package/package.json +4 -3
- package/src/api/api-alephium.ts +6 -0
- package/src/api/types.ts +1 -1
- package/src/codec/array-codec.ts +63 -0
- package/src/codec/asset-output-codec.ts +149 -0
- package/src/codec/bigint-codec.ts +92 -0
- package/src/codec/bytestring-codec.ts +56 -0
- package/src/codec/codec.ts +31 -0
- package/src/codec/compact-int-codec.ts +316 -0
- package/src/codec/contract-codec.ts +95 -0
- package/src/codec/contract-output-codec.ts +95 -0
- package/src/codec/contract-output-ref-codec.ts +42 -0
- package/src/codec/either-codec.ts +74 -0
- package/src/codec/hash.ts +35 -0
- package/src/codec/index.ts +41 -0
- package/src/codec/input-codec.ts +81 -0
- package/src/codec/instr-codec.ts +479 -0
- package/src/codec/lockup-script-codec.ts +99 -0
- package/src/codec/long-codec.ts +59 -0
- package/src/codec/method-codec.ts +97 -0
- package/src/codec/option-codec.ts +60 -0
- package/src/codec/output-codec.ts +26 -0
- package/src/codec/script-codec.ts +45 -0
- package/src/codec/signature-codec.ts +40 -0
- package/src/codec/signed-int-codec.ts +37 -0
- package/src/codec/token-codec.ts +51 -0
- package/src/codec/transaction-codec.ts +147 -0
- package/src/codec/unlock-script-codec.ts +194 -0
- package/src/codec/unsigned-tx-codec.ts +124 -0
- package/src/contract/contract.ts +271 -14
- package/src/contract/ralph.ts +140 -2
- package/src/index.ts +1 -1
package/src/contract/ralph.ts
CHANGED
|
@@ -17,9 +17,11 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
19
|
import { Buffer } from 'buffer/'
|
|
20
|
-
import {
|
|
21
|
-
import { binToHex, bs58, isHexString } from '../utils'
|
|
20
|
+
import { Val, decodeArrayType, toApiAddress, toApiBoolean, toApiByteVec, toApiNumber256 } from '../api'
|
|
21
|
+
import { binToHex, bs58, hexToBinUnsafe, isHexString } from '../utils'
|
|
22
22
|
import { Fields, FieldsSig, Struct } from './contract'
|
|
23
|
+
import { compactSignedIntCodec, compactUnsignedIntCodec } from '../codec'
|
|
24
|
+
import { lockupScriptCodec } from '../codec/lockup-script-codec'
|
|
23
25
|
|
|
24
26
|
const bigIntZero = BigInt(0)
|
|
25
27
|
|
|
@@ -52,6 +54,13 @@ export function encodeBool(bool: boolean): Uint8Array {
|
|
|
52
54
|
return bool ? Uint8Array.from([1]) : Uint8Array.from([0])
|
|
53
55
|
}
|
|
54
56
|
|
|
57
|
+
export function decodeBool(bytes: Uint8Array): boolean {
|
|
58
|
+
if (bytes.length !== 1) {
|
|
59
|
+
throw new Error(`Expected one byte for encoded bool, got ${bytes.length}`)
|
|
60
|
+
}
|
|
61
|
+
return bytes[0] === 1 ? true : false
|
|
62
|
+
}
|
|
63
|
+
|
|
55
64
|
export function encodeI256(i256: bigint): Uint8Array {
|
|
56
65
|
if (i256 >= bigIntZero) {
|
|
57
66
|
return encodeI256Positive(i256)
|
|
@@ -247,6 +256,135 @@ export function encodeScriptField(tpe: string, value: Val): Uint8Array {
|
|
|
247
256
|
throw invalidScriptField(tpe, value)
|
|
248
257
|
}
|
|
249
258
|
|
|
259
|
+
export function splitFields(fieldsSig: FieldsSig): [FieldsSig, FieldsSig] {
|
|
260
|
+
return fieldsSig.types.reduce<[FieldsSig, FieldsSig]>(
|
|
261
|
+
([mapFields, fieldsExceptMaps], type, index) => {
|
|
262
|
+
const fieldSig = type.startsWith('Map[') ? mapFields : fieldsExceptMaps
|
|
263
|
+
fieldSig.names.push(fieldsSig.names[`${index}`])
|
|
264
|
+
fieldSig.types.push(type)
|
|
265
|
+
fieldSig.isMutable.push(fieldsSig.isMutable[`${index}`])
|
|
266
|
+
return [mapFields, fieldsExceptMaps]
|
|
267
|
+
},
|
|
268
|
+
[
|
|
269
|
+
{ names: [], types: [], isMutable: [] },
|
|
270
|
+
{ names: [], types: [], isMutable: [] }
|
|
271
|
+
]
|
|
272
|
+
)
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
export function parseMapType(type: string): [string, string] {
|
|
276
|
+
if (!type.startsWith('Map[')) {
|
|
277
|
+
throw new Error(`Expected map type, got ${type}`)
|
|
278
|
+
}
|
|
279
|
+
const keyStartIndex = type.indexOf('[')
|
|
280
|
+
const keyEndIndex = type.indexOf(',')
|
|
281
|
+
return [type.slice(keyStartIndex + 1, keyEndIndex), type.slice(keyEndIndex + 1, type.length - 1)]
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
export function encodeMapPrefix(mapIndex: number): Uint8Array {
|
|
285
|
+
const str = `__map__${mapIndex}__`
|
|
286
|
+
const bytes = new Uint8Array(str.length)
|
|
287
|
+
for (let i = 0; i < str.length; i += 1) {
|
|
288
|
+
bytes[i] = str.charCodeAt(i)
|
|
289
|
+
}
|
|
290
|
+
return bytes
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
function fromAscii(str: string): string {
|
|
294
|
+
let result = ''
|
|
295
|
+
for (let i = 0; i < str.length; i += 2) {
|
|
296
|
+
const ascii = parseInt(str.slice(i, i + 2), 16)
|
|
297
|
+
result += String.fromCharCode(ascii)
|
|
298
|
+
}
|
|
299
|
+
return result
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
export function calcFieldSize(
|
|
303
|
+
type: string,
|
|
304
|
+
isMutable: boolean,
|
|
305
|
+
structs: Struct[]
|
|
306
|
+
): { immFields: number; mutFields: number } {
|
|
307
|
+
const struct = structs.find((s) => s.name === type)
|
|
308
|
+
if (struct !== undefined) {
|
|
309
|
+
return struct.fieldTypes.reduce(
|
|
310
|
+
(acc, fieldType, index) => {
|
|
311
|
+
const isFieldMutable = isMutable && struct.isMutable[`${index}`]
|
|
312
|
+
const subFieldSize = calcFieldSize(fieldType, isFieldMutable, structs)
|
|
313
|
+
return {
|
|
314
|
+
immFields: acc.immFields + subFieldSize.immFields,
|
|
315
|
+
mutFields: acc.mutFields + subFieldSize.mutFields
|
|
316
|
+
}
|
|
317
|
+
},
|
|
318
|
+
{ immFields: 0, mutFields: 0 }
|
|
319
|
+
)
|
|
320
|
+
}
|
|
321
|
+
if (type.startsWith('[')) {
|
|
322
|
+
const [baseType, size] = decodeArrayType(type)
|
|
323
|
+
const base = calcFieldSize(baseType, isMutable, structs)
|
|
324
|
+
return { immFields: base.immFields * size, mutFields: base.mutFields * size }
|
|
325
|
+
}
|
|
326
|
+
return isMutable ? { immFields: 0, mutFields: 1 } : { immFields: 1, mutFields: 0 }
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
export function tryDecodeMapDebugLog(
|
|
330
|
+
message: string
|
|
331
|
+
): { path: string; mapIndex: number; encodedKey: Uint8Array; isInsert: boolean } | undefined {
|
|
332
|
+
const prefix = '5f5f6d61705f5f' // __map__
|
|
333
|
+
const parts = message.split(',')
|
|
334
|
+
if (!message.startsWith(prefix) || parts.length !== 2) return undefined
|
|
335
|
+
if (parts[1] !== 'true' && parts[1] !== 'false') return undefined
|
|
336
|
+
|
|
337
|
+
if (!isHexString(parts[0])) return undefined
|
|
338
|
+
const remain = parts[0].slice(prefix.length)
|
|
339
|
+
const suffixIndex = remain.indexOf('5f5f') // __
|
|
340
|
+
if (suffixIndex === -1) return undefined
|
|
341
|
+
|
|
342
|
+
const encodedMapIndex = remain.slice(0, suffixIndex)
|
|
343
|
+
const mapIndex = parseInt(fromAscii(encodedMapIndex))
|
|
344
|
+
const encodedKey = hexToBinUnsafe(remain.slice(suffixIndex + 4))
|
|
345
|
+
const isInsert = parts[1] === 'true' ? true : false
|
|
346
|
+
return { path: parts[0], mapIndex, encodedKey, isInsert }
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
export function decodePrimitive(value: Uint8Array, type: string): Val {
|
|
350
|
+
switch (type) {
|
|
351
|
+
case 'Bool':
|
|
352
|
+
return decodeBool(value)
|
|
353
|
+
case 'I256':
|
|
354
|
+
return compactSignedIntCodec.decodeI256(Buffer.from(value))
|
|
355
|
+
case 'U256':
|
|
356
|
+
return compactUnsignedIntCodec.decodeU256(Buffer.from(value))
|
|
357
|
+
case 'ByteVec':
|
|
358
|
+
return binToHex(value)
|
|
359
|
+
case 'Address':
|
|
360
|
+
return bs58.encode(value)
|
|
361
|
+
default:
|
|
362
|
+
throw Error(`Expected primitive type, got ${type}`)
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
export function primitiveToByteVec(value: Val, type: string): Uint8Array {
|
|
367
|
+
switch (type) {
|
|
368
|
+
case 'Bool':
|
|
369
|
+
const byte = toApiBoolean(value) ? 1 : 0
|
|
370
|
+
return new Uint8Array([byte])
|
|
371
|
+
case 'I256':
|
|
372
|
+
const i256 = toApiNumber256(value)
|
|
373
|
+
return encodeI256(BigInt(i256))
|
|
374
|
+
case 'U256':
|
|
375
|
+
const u256 = toApiNumber256(value)
|
|
376
|
+
return encodeU256(BigInt(u256))
|
|
377
|
+
case 'ByteVec':
|
|
378
|
+
const hexStr = toApiByteVec(value)
|
|
379
|
+
return encodeByteVec(hexStr)
|
|
380
|
+
case 'Address':
|
|
381
|
+
const address = toApiAddress(value)
|
|
382
|
+
return encodeAddress(address)
|
|
383
|
+
default:
|
|
384
|
+
throw Error(`Expected primitive type, got ${type}`)
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
250
388
|
export function flattenFields(
|
|
251
389
|
fields: Fields,
|
|
252
390
|
names: string[],
|