@alephium/web3 0.39.0 → 0.39.2

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.
@@ -1,6 +1,6 @@
1
1
  import { ApiRequestArguments, ApiRequestHandler, FungibleTokenMetaData, NFTMetaData, NFTCollectionMetaData } from './types';
2
2
  import { Api as NodeApi } from './api-alephium';
3
- import { HexString } from '../contract';
3
+ import { HexString } from '../utils';
4
4
  interface NodeProviderApis {
5
5
  wallets: NodeApi<string>['wallets'];
6
6
  infos: NodeApi<string>['infos'];
@@ -1,6 +1,6 @@
1
1
  import { NamedVals, node, NodeProvider, Number256, Token, Val } from '../api';
2
2
  import { SignDeployContractTxParams, SignDeployContractTxResult, SignExecuteScriptTxParams, SignerProvider, Address } from '../signer';
3
- import { Optional } from '../utils';
3
+ import { Optional, HexString } from '../utils';
4
4
  import { EventSubscribeOptions, EventSubscription } from './events';
5
5
  export type FieldsSig = node.FieldsSig;
6
6
  export type MapsSig = node.MapsSig;
@@ -8,7 +8,6 @@ export type EventSig = node.EventSig;
8
8
  export type FunctionSig = node.FunctionSig;
9
9
  export type Fields = NamedVals;
10
10
  export type Arguments = NamedVals;
11
- export type HexString = string;
12
11
  export type Constant = node.Constant;
13
12
  export type Enum = node.Enum;
14
13
  export declare const StdIdFieldName = "__stdInterfaceId";
@@ -1,4 +1,5 @@
1
1
  import { Val } from '../api';
2
+ import { HexString } from '../utils';
2
3
  import { Fields, FieldsSig, Struct } from './contract';
3
4
  export declare function encodeBool(bool: boolean): Uint8Array;
4
5
  export declare function decodeBool(bytes: Uint8Array): boolean;
@@ -18,6 +19,30 @@ export declare function encodeVmI256(i256: bigint): Uint8Array;
18
19
  export declare function encodeVmU256(u256: bigint): Uint8Array;
19
20
  export declare function encodeVmByteVec(bytes: string): Uint8Array;
20
21
  export declare function encodeVmAddress(address: string): Uint8Array;
22
+ export declare function boolVal(value: boolean): {
23
+ type: 'Bool';
24
+ value: Val;
25
+ };
26
+ export declare function i256Val(value: bigint | number): {
27
+ type: 'I256';
28
+ value: Val;
29
+ };
30
+ export declare function u256Val(value: bigint | number): {
31
+ type: 'U256';
32
+ value: Val;
33
+ };
34
+ export declare function byteVecVal(value: HexString): {
35
+ type: 'ByteVec';
36
+ value: Val;
37
+ };
38
+ export declare function addressVal(value: string): {
39
+ type: 'Address';
40
+ value: Val;
41
+ };
42
+ export declare function encodePrimitiveValues(values: {
43
+ type: string;
44
+ value: Val;
45
+ }[]): Uint8Array;
21
46
  export declare function encodeScriptFieldAsString(tpe: string, value: Val): string;
22
47
  export declare function encodeScriptField(tpe: string, value: Val): Uint8Array;
23
48
  export declare function splitFields(fieldsSig: FieldsSig): [FieldsSig, FieldsSig];
@@ -42,6 +67,10 @@ export declare function flattenFields(fields: Fields, names: string[], types: st
42
67
  isMutable: boolean;
43
68
  }[];
44
69
  export declare function buildScriptByteCode(bytecodeTemplate: string, fields: Fields, fieldsSig: FieldsSig, structs: Struct[]): string;
70
+ export declare function encodeContractFields(fields: Fields, fieldsSig: FieldsSig, structs: Struct[]): {
71
+ encodedImmFields: Uint8Array;
72
+ encodedMutFields: Uint8Array;
73
+ };
45
74
  export declare function buildContractByteCode(bytecode: string, fields: Fields, fieldsSig: FieldsSig, structs: Struct[]): string;
46
75
  export declare function encodeContractField(tpe: string, value: Val): Uint8Array;
47
76
  export declare function buildDebugBytecode(bytecode: string, bytecodePatch: string): string;
@@ -18,7 +18,7 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
18
  */
19
19
  var _a;
20
20
  Object.defineProperty(exports, "__esModule", { value: true });
21
- exports.buildDebugBytecode = exports.encodeContractField = exports.buildContractByteCode = exports.buildScriptByteCode = exports.flattenFields = exports.primitiveToByteVec = exports.decodePrimitive = exports.tryDecodeMapDebugLog = exports.calcFieldSize = exports.encodeMapPrefix = exports.parseMapType = exports.splitFields = exports.encodeScriptField = exports.encodeScriptFieldAsString = exports.encodeVmAddress = exports.encodeVmByteVec = exports.encodeVmU256 = exports.encodeVmI256 = exports.encodeVmBool = exports.VmValType = exports.encodeAddress = exports.encodeByteVec = exports.encodeU256 = exports.encodeI256 = exports.decodeBool = exports.encodeBool = void 0;
21
+ exports.buildDebugBytecode = exports.encodeContractField = exports.buildContractByteCode = exports.encodeContractFields = exports.buildScriptByteCode = exports.flattenFields = exports.primitiveToByteVec = exports.decodePrimitive = exports.tryDecodeMapDebugLog = exports.calcFieldSize = exports.encodeMapPrefix = exports.parseMapType = exports.splitFields = exports.encodeScriptField = exports.encodeScriptFieldAsString = exports.encodePrimitiveValues = exports.addressVal = exports.byteVecVal = exports.u256Val = exports.i256Val = exports.boolVal = exports.encodeVmAddress = exports.encodeVmByteVec = exports.encodeVmU256 = exports.encodeVmI256 = exports.encodeVmBool = exports.VmValType = exports.encodeAddress = exports.encodeByteVec = exports.encodeU256 = exports.encodeI256 = exports.decodeBool = exports.encodeBool = void 0;
22
22
  const buffer_1 = require("buffer/");
23
23
  const api_1 = require("../api");
24
24
  const utils_1 = require("../utils");
@@ -181,25 +181,49 @@ var VmValType;
181
181
  VmValType[VmValType["Address"] = 4] = "Address";
182
182
  })(VmValType = exports.VmValType || (exports.VmValType = {}));
183
183
  function encodeVmBool(bool) {
184
- return buffer_1.Buffer.concat([encodeU256(BigInt(VmValType.Bool)), encodeBool(bool)]);
184
+ return new Uint8Array([VmValType.Bool, ...encodeBool(bool)]);
185
185
  }
186
186
  exports.encodeVmBool = encodeVmBool;
187
187
  function encodeVmI256(i256) {
188
- return buffer_1.Buffer.concat([encodeU256(BigInt(VmValType.I256)), encodeI256(i256)]);
188
+ return new Uint8Array([VmValType.I256, ...encodeI256(i256)]);
189
189
  }
190
190
  exports.encodeVmI256 = encodeVmI256;
191
191
  function encodeVmU256(u256) {
192
- return buffer_1.Buffer.concat([encodeU256(BigInt(VmValType.U256)), encodeU256(u256)]);
192
+ return new Uint8Array([VmValType.U256, ...encodeU256(u256)]);
193
193
  }
194
194
  exports.encodeVmU256 = encodeVmU256;
195
195
  function encodeVmByteVec(bytes) {
196
- return buffer_1.Buffer.concat([encodeU256(BigInt(VmValType.ByteVec)), encodeByteVec(bytes)]);
196
+ return new Uint8Array([VmValType.ByteVec, ...encodeByteVec(bytes)]);
197
197
  }
198
198
  exports.encodeVmByteVec = encodeVmByteVec;
199
199
  function encodeVmAddress(address) {
200
- return buffer_1.Buffer.concat([encodeU256(BigInt(VmValType.Address)), encodeAddress(address)]);
200
+ return new Uint8Array([VmValType.Address, ...encodeAddress(address)]);
201
201
  }
202
202
  exports.encodeVmAddress = encodeVmAddress;
203
+ function boolVal(value) {
204
+ return { type: 'Bool', value };
205
+ }
206
+ exports.boolVal = boolVal;
207
+ function i256Val(value) {
208
+ return { type: 'I256', value: BigInt(value) };
209
+ }
210
+ exports.i256Val = i256Val;
211
+ function u256Val(value) {
212
+ return { type: 'U256', value: BigInt(value) };
213
+ }
214
+ exports.u256Val = u256Val;
215
+ function byteVecVal(value) {
216
+ return { type: 'ByteVec', value };
217
+ }
218
+ exports.byteVecVal = byteVecVal;
219
+ function addressVal(value) {
220
+ return { type: 'Address', value };
221
+ }
222
+ exports.addressVal = addressVal;
223
+ function encodePrimitiveValues(values) {
224
+ return encodeFields(values.map(({ type, value }) => ({ name: `${value}`, type, value })));
225
+ }
226
+ exports.encodePrimitiveValues = encodePrimitiveValues;
203
227
  function invalidScriptField(tpe, value) {
204
228
  return Error(`Invalid script field ${value} for type ${tpe}`);
205
229
  }
@@ -455,50 +479,40 @@ function _encodeField(fieldName, encodeFunc) {
455
479
  }
456
480
  }
457
481
  function encodeFields(fields) {
458
- const prefix = (0, utils_1.binToHex)(encodeI256(BigInt(fields.length)));
459
- const encoded = fields
460
- .map((field) => (0, utils_1.binToHex)(_encodeField(field.name, () => encodeContractField(field.type, field.value))))
461
- .join('');
462
- return prefix + encoded;
482
+ const prefix = encodeI256(BigInt(fields.length));
483
+ return fields.reduce((acc, field) => {
484
+ const encoded = _encodeField(field.name, () => encodeContractField(field.type, field.value));
485
+ const bytes = new Uint8Array(acc.byteLength + encoded.byteLength);
486
+ bytes.set(acc, 0);
487
+ bytes.set(encoded, acc.byteLength);
488
+ return bytes;
489
+ }, prefix);
490
+ }
491
+ function encodeContractFields(fields, fieldsSig, structs) {
492
+ const allFields = flattenFields(fields, fieldsSig.names, fieldsSig.types, fieldsSig.isMutable, structs);
493
+ return {
494
+ encodedImmFields: encodeFields(allFields.filter((f) => !f.isMutable)),
495
+ encodedMutFields: encodeFields(allFields.filter((f) => f.isMutable))
496
+ };
463
497
  }
498
+ exports.encodeContractFields = encodeContractFields;
464
499
  function buildContractByteCode(bytecode, fields, fieldsSig, structs) {
465
- const allFields = flattenFields(fields, fieldsSig.names, fieldsSig.types, fieldsSig.isMutable, structs);
466
- const encodedImmFields = encodeFields(allFields.filter((f) => !f.isMutable));
467
- const encodedMutFields = encodeFields(allFields.filter((f) => f.isMutable));
468
- return bytecode + encodedImmFields + encodedMutFields;
500
+ const { encodedImmFields, encodedMutFields } = encodeContractFields(fields, fieldsSig, structs);
501
+ return bytecode + (0, utils_1.binToHex)(encodedImmFields) + (0, utils_1.binToHex)(encodedMutFields);
469
502
  }
470
503
  exports.buildContractByteCode = buildContractByteCode;
471
- var ApiValType;
472
- (function (ApiValType) {
473
- ApiValType[ApiValType["Bool"] = 0] = "Bool";
474
- ApiValType[ApiValType["I256"] = 1] = "I256";
475
- ApiValType[ApiValType["U256"] = 2] = "U256";
476
- ApiValType[ApiValType["ByteVec"] = 3] = "ByteVec";
477
- ApiValType[ApiValType["Address"] = 4] = "Address";
478
- })(ApiValType || (ApiValType = {}));
479
- function encodeContractFieldI256(value) {
480
- return new Uint8Array([ApiValType.I256, ...encodeI256(value)]);
481
- }
482
- function encodeContractFieldU256(value) {
483
- return new Uint8Array([ApiValType.U256, ...encodeU256(value)]);
484
- }
485
504
  function encodeContractField(tpe, value) {
486
505
  switch (tpe) {
487
506
  case 'Bool':
488
- const byte = (0, api_1.toApiBoolean)(value) ? 1 : 0;
489
- return new Uint8Array([ApiValType.Bool, byte]);
507
+ return encodeVmBool((0, api_1.toApiBoolean)(value));
490
508
  case 'I256':
491
- const i256 = (0, api_1.toApiNumber256)(value);
492
- return encodeContractFieldI256(BigInt(i256));
509
+ return encodeVmI256(BigInt((0, api_1.toApiNumber256)(value)));
493
510
  case 'U256':
494
- const u256 = (0, api_1.toApiNumber256)(value);
495
- return encodeContractFieldU256(BigInt(u256));
511
+ return encodeVmU256(BigInt((0, api_1.toApiNumber256)(value)));
496
512
  case 'ByteVec':
497
- const hexStr = (0, api_1.toApiByteVec)(value);
498
- return new Uint8Array([ApiValType.ByteVec, ...encodeByteVec(hexStr)]);
513
+ return encodeVmByteVec((0, api_1.toApiByteVec)(value));
499
514
  case 'Address':
500
- const address = (0, api_1.toApiAddress)(value);
501
- return new Uint8Array([ApiValType.Address, ...encodeAddress(address)]);
515
+ return encodeVmAddress((0, api_1.toApiAddress)(value));
502
516
  default:
503
517
  throw Error(`Expected primitive type, got ${tpe}`);
504
518
  }
@@ -1,8 +1,8 @@
1
1
  import { ec as EC, SignatureInput } from 'elliptic';
2
2
  import BN from 'bn.js';
3
- import { HexString } from '../contract';
4
3
  export declare const networkIds: readonly ["mainnet", "testnet", "devnet"];
5
4
  export type NetworkId = (typeof networkIds)[number];
5
+ export type HexString = string;
6
6
  export declare function encodeSignature(signature: EC.Signature | {
7
7
  r: BN;
8
8
  s: BN;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alephium/web3",
3
- "version": "0.39.0",
3
+ "version": "0.39.2",
4
4
  "description": "A JS/TS library to interact with the Alephium platform",
5
5
  "license": "GPL",
6
6
  "main": "dist/src/index.js",
@@ -28,8 +28,9 @@ import {
28
28
  requestWithLog
29
29
  } from './types'
30
30
  import { Api as NodeApi, CallContractFailed, CallContractSucceeded } from './api-alephium'
31
- import { HexString, tryGetCallResult } from '../contract'
31
+ import { tryGetCallResult } from '../contract'
32
32
  import {
33
+ HexString,
33
34
  addressFromContractId,
34
35
  addressFromTokenId,
35
36
  groupOfAddress,
@@ -58,7 +58,8 @@ import {
58
58
  hexToBinUnsafe,
59
59
  isDevnet,
60
60
  addressFromContractId,
61
- subContractId
61
+ subContractId,
62
+ HexString
62
63
  } from '../utils'
63
64
  import { getCurrentNodeProvider } from '../global'
64
65
  import * as path from 'path'
@@ -89,7 +90,6 @@ export type EventSig = node.EventSig
89
90
  export type FunctionSig = node.FunctionSig
90
91
  export type Fields = NamedVals
91
92
  export type Arguments = NamedVals
92
- export type HexString = string
93
93
  export type Constant = node.Constant
94
94
  export type Enum = node.Enum
95
95
 
@@ -18,10 +18,9 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
18
 
19
19
  import { Buffer } from 'buffer/'
20
20
  import { Val, decodeArrayType, toApiAddress, toApiBoolean, toApiByteVec, toApiNumber256 } from '../api'
21
- import { binToHex, bs58, hexToBinUnsafe, isHexString } from '../utils'
21
+ import { HexString, binToHex, bs58, hexToBinUnsafe, isHexString } from '../utils'
22
22
  import { Fields, FieldsSig, Struct } from './contract'
23
23
  import { compactSignedIntCodec, compactUnsignedIntCodec } from '../codec'
24
- import { lockupScriptCodec } from '../codec/lockup-script-codec'
25
24
 
26
25
  const bigIntZero = BigInt(0)
27
26
 
@@ -175,23 +174,47 @@ export enum VmValType {
175
174
  }
176
175
 
177
176
  export function encodeVmBool(bool: boolean): Uint8Array {
178
- return Buffer.concat([encodeU256(BigInt(VmValType.Bool)), encodeBool(bool)])
177
+ return new Uint8Array([VmValType.Bool, ...encodeBool(bool)])
179
178
  }
180
179
 
181
180
  export function encodeVmI256(i256: bigint): Uint8Array {
182
- return Buffer.concat([encodeU256(BigInt(VmValType.I256)), encodeI256(i256)])
181
+ return new Uint8Array([VmValType.I256, ...encodeI256(i256)])
183
182
  }
184
183
 
185
184
  export function encodeVmU256(u256: bigint): Uint8Array {
186
- return Buffer.concat([encodeU256(BigInt(VmValType.U256)), encodeU256(u256)])
185
+ return new Uint8Array([VmValType.U256, ...encodeU256(u256)])
187
186
  }
188
187
 
189
188
  export function encodeVmByteVec(bytes: string): Uint8Array {
190
- return Buffer.concat([encodeU256(BigInt(VmValType.ByteVec)), encodeByteVec(bytes)])
189
+ return new Uint8Array([VmValType.ByteVec, ...encodeByteVec(bytes)])
191
190
  }
192
191
 
193
192
  export function encodeVmAddress(address: string): Uint8Array {
194
- return Buffer.concat([encodeU256(BigInt(VmValType.Address)), encodeAddress(address)])
193
+ return new Uint8Array([VmValType.Address, ...encodeAddress(address)])
194
+ }
195
+
196
+ export function boolVal(value: boolean): { type: 'Bool'; value: Val } {
197
+ return { type: 'Bool', value }
198
+ }
199
+
200
+ export function i256Val(value: bigint | number): { type: 'I256'; value: Val } {
201
+ return { type: 'I256', value: BigInt(value) }
202
+ }
203
+
204
+ export function u256Val(value: bigint | number): { type: 'U256'; value: Val } {
205
+ return { type: 'U256', value: BigInt(value) }
206
+ }
207
+
208
+ export function byteVecVal(value: HexString): { type: 'ByteVec'; value: Val } {
209
+ return { type: 'ByteVec', value }
210
+ }
211
+
212
+ export function addressVal(value: string): { type: 'Address'; value: Val } {
213
+ return { type: 'Address', value }
214
+ }
215
+
216
+ export function encodePrimitiveValues(values: { type: string; value: Val }[]): Uint8Array {
217
+ return encodeFields(values.map(({ type, value }) => ({ name: `${value}`, type, value })))
195
218
  }
196
219
 
197
220
  function invalidScriptField(tpe: string, value: Val): Error {
@@ -486,59 +509,51 @@ function _encodeField<T>(fieldName: string, encodeFunc: () => T): T {
486
509
  }
487
510
  }
488
511
 
489
- function encodeFields(fields: { name: string; type: string; value: Val }[]): string {
490
- const prefix = binToHex(encodeI256(BigInt(fields.length)))
491
- const encoded = fields
492
- .map((field) => binToHex(_encodeField(field.name, () => encodeContractField(field.type, field.value))))
493
- .join('')
494
- return prefix + encoded
512
+ function encodeFields(fields: { name: string; type: string; value: Val }[]): Uint8Array {
513
+ const prefix = encodeI256(BigInt(fields.length))
514
+ return fields.reduce((acc, field) => {
515
+ const encoded = _encodeField(field.name, () => encodeContractField(field.type, field.value))
516
+ const bytes = new Uint8Array(acc.byteLength + encoded.byteLength)
517
+ bytes.set(acc, 0)
518
+ bytes.set(encoded, acc.byteLength)
519
+ return bytes
520
+ }, prefix)
495
521
  }
496
522
 
497
- export function buildContractByteCode(
498
- bytecode: string,
523
+ export function encodeContractFields(
499
524
  fields: Fields,
500
525
  fieldsSig: FieldsSig,
501
526
  structs: Struct[]
502
- ): string {
527
+ ): { encodedImmFields: Uint8Array; encodedMutFields: Uint8Array } {
503
528
  const allFields = flattenFields(fields, fieldsSig.names, fieldsSig.types, fieldsSig.isMutable, structs)
504
- const encodedImmFields = encodeFields(allFields.filter((f) => !f.isMutable))
505
- const encodedMutFields = encodeFields(allFields.filter((f) => f.isMutable))
506
- return bytecode + encodedImmFields + encodedMutFields
507
- }
508
-
509
- enum ApiValType {
510
- Bool = 0,
511
- I256 = 1,
512
- U256 = 2,
513
- ByteVec = 3,
514
- Address = 4
515
- }
516
-
517
- function encodeContractFieldI256(value: bigint): Uint8Array {
518
- return new Uint8Array([ApiValType.I256, ...encodeI256(value)])
529
+ return {
530
+ encodedImmFields: encodeFields(allFields.filter((f) => !f.isMutable)),
531
+ encodedMutFields: encodeFields(allFields.filter((f) => f.isMutable))
532
+ }
519
533
  }
520
534
 
521
- function encodeContractFieldU256(value: bigint): Uint8Array {
522
- return new Uint8Array([ApiValType.U256, ...encodeU256(value)])
535
+ export function buildContractByteCode(
536
+ bytecode: string,
537
+ fields: Fields,
538
+ fieldsSig: FieldsSig,
539
+ structs: Struct[]
540
+ ): string {
541
+ const { encodedImmFields, encodedMutFields } = encodeContractFields(fields, fieldsSig, structs)
542
+ return bytecode + binToHex(encodedImmFields) + binToHex(encodedMutFields)
523
543
  }
524
544
 
525
545
  export function encodeContractField(tpe: string, value: Val): Uint8Array {
526
546
  switch (tpe) {
527
547
  case 'Bool':
528
- const byte = toApiBoolean(value) ? 1 : 0
529
- return new Uint8Array([ApiValType.Bool, byte])
548
+ return encodeVmBool(toApiBoolean(value))
530
549
  case 'I256':
531
- const i256 = toApiNumber256(value)
532
- return encodeContractFieldI256(BigInt(i256))
550
+ return encodeVmI256(BigInt(toApiNumber256(value)))
533
551
  case 'U256':
534
- const u256 = toApiNumber256(value)
535
- return encodeContractFieldU256(BigInt(u256))
552
+ return encodeVmU256(BigInt(toApiNumber256(value)))
536
553
  case 'ByteVec':
537
- const hexStr = toApiByteVec(value)
538
- return new Uint8Array([ApiValType.ByteVec, ...encodeByteVec(hexStr)])
554
+ return encodeVmByteVec(toApiByteVec(value))
539
555
  case 'Address':
540
- const address = toApiAddress(value)
541
- return new Uint8Array([ApiValType.Address, ...encodeAddress(address)])
556
+ return encodeVmAddress(toApiAddress(value))
542
557
  default:
543
558
  throw Error(`Expected primitive type, got ${tpe}`)
544
559
  }
@@ -18,15 +18,13 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
18
 
19
19
  import { ec as EC, SignatureInput } from 'elliptic'
20
20
  import BN from 'bn.js'
21
- import bs58 from './bs58'
22
21
  import { Buffer } from 'buffer/'
23
22
 
24
23
  import { TOTAL_NUMBER_OF_GROUPS, TOTAL_NUMBER_OF_CHAINS } from '../constants'
25
- import { KeyType } from '../signer'
26
- import { HexString } from '../contract'
27
24
 
28
25
  export const networkIds = ['mainnet', 'testnet', 'devnet'] as const
29
26
  export type NetworkId = (typeof networkIds)[number]
27
+ export type HexString = string
30
28
 
31
29
  const ec = new EC('secp256k1')
32
30