@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.
Files changed (94) 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/package.json +4 -3
  64. package/src/api/api-alephium.ts +6 -0
  65. package/src/api/types.ts +1 -1
  66. package/src/codec/array-codec.ts +63 -0
  67. package/src/codec/asset-output-codec.ts +149 -0
  68. package/src/codec/bigint-codec.ts +92 -0
  69. package/src/codec/bytestring-codec.ts +56 -0
  70. package/src/codec/codec.ts +31 -0
  71. package/src/codec/compact-int-codec.ts +316 -0
  72. package/src/codec/contract-codec.ts +95 -0
  73. package/src/codec/contract-output-codec.ts +95 -0
  74. package/src/codec/contract-output-ref-codec.ts +42 -0
  75. package/src/codec/either-codec.ts +74 -0
  76. package/src/codec/hash.ts +35 -0
  77. package/src/codec/index.ts +41 -0
  78. package/src/codec/input-codec.ts +81 -0
  79. package/src/codec/instr-codec.ts +479 -0
  80. package/src/codec/lockup-script-codec.ts +99 -0
  81. package/src/codec/long-codec.ts +59 -0
  82. package/src/codec/method-codec.ts +97 -0
  83. package/src/codec/option-codec.ts +60 -0
  84. package/src/codec/output-codec.ts +26 -0
  85. package/src/codec/script-codec.ts +45 -0
  86. package/src/codec/signature-codec.ts +40 -0
  87. package/src/codec/signed-int-codec.ts +37 -0
  88. package/src/codec/token-codec.ts +51 -0
  89. package/src/codec/transaction-codec.ts +147 -0
  90. package/src/codec/unlock-script-codec.ts +194 -0
  91. package/src/codec/unsigned-tx-codec.ts +124 -0
  92. package/src/contract/contract.ts +271 -14
  93. package/src/contract/ralph.ts +140 -2
  94. package/src/index.ts +1 -1
@@ -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 { PrimitiveTypes, Val, decodeArrayType, toApiAddress, toApiBoolean, toApiByteVec, toApiNumber256 } from '../api'
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[],
package/src/index.ts CHANGED
@@ -29,6 +29,6 @@ export * from './token'
29
29
 
30
30
  export * from './constants'
31
31
  export * as web3 from './global'
32
-
32
+ export * as codec from './codec'
33
33
  export * as utils from './utils'
34
34
  export * from './debug'