@alephium/web3 0.38.2 → 0.39.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.
@@ -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
  }
@@ -318,23 +342,26 @@ function calcFieldSize(type, isMutable, structs) {
318
342
  }
319
343
  exports.calcFieldSize = calcFieldSize;
320
344
  function tryDecodeMapDebugLog(message) {
321
- const prefix = '5f5f6d61705f5f'; // __map__
322
- const parts = message.split(',');
323
- if (!message.startsWith(prefix) || parts.length !== 2)
345
+ if (!message.startsWith('insert at map path: ') && !message.startsWith('remove at map path: ')) {
324
346
  return undefined;
325
- if (parts[1] !== 'true' && parts[1] !== 'false')
347
+ }
348
+ const parts = message.split(':');
349
+ if (parts.length !== 2)
326
350
  return undefined;
327
- if (!(0, utils_1.isHexString)(parts[0]))
351
+ const pathString = parts[1].slice(1);
352
+ if (!(0, utils_1.isHexString)(pathString))
328
353
  return undefined;
329
- const remain = parts[0].slice(prefix.length);
330
- const suffixIndex = remain.indexOf('5f5f'); // __
354
+ const prefix = '5f5f6d61705f5f'; // __map__
355
+ const remain = pathString.slice(prefix.length);
356
+ const suffix = '5f5f'; // __
357
+ const suffixIndex = remain.indexOf(suffix);
331
358
  if (suffixIndex === -1)
332
359
  return undefined;
333
360
  const encodedMapIndex = remain.slice(0, suffixIndex);
334
361
  const mapIndex = parseInt(fromAscii(encodedMapIndex));
335
- const encodedKey = (0, utils_1.hexToBinUnsafe)(remain.slice(suffixIndex + 4));
336
- const isInsert = parts[1] === 'true' ? true : false;
337
- return { path: parts[0], mapIndex, encodedKey, isInsert };
362
+ const encodedKey = (0, utils_1.hexToBinUnsafe)(remain.slice(suffixIndex + suffix.length));
363
+ const isInsert = message.startsWith('insert');
364
+ return { path: pathString, mapIndex, encodedKey, isInsert };
338
365
  }
339
366
  exports.tryDecodeMapDebugLog = tryDecodeMapDebugLog;
340
367
  function decodePrimitive(value, type) {
@@ -452,50 +479,40 @@ function _encodeField(fieldName, encodeFunc) {
452
479
  }
453
480
  }
454
481
  function encodeFields(fields) {
455
- const prefix = (0, utils_1.binToHex)(encodeI256(BigInt(fields.length)));
456
- const encoded = fields
457
- .map((field) => (0, utils_1.binToHex)(_encodeField(field.name, () => encodeContractField(field.type, field.value))))
458
- .join('');
459
- 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
+ };
460
497
  }
498
+ exports.encodeContractFields = encodeContractFields;
461
499
  function buildContractByteCode(bytecode, fields, fieldsSig, structs) {
462
- const allFields = flattenFields(fields, fieldsSig.names, fieldsSig.types, fieldsSig.isMutable, structs);
463
- const encodedImmFields = encodeFields(allFields.filter((f) => !f.isMutable));
464
- const encodedMutFields = encodeFields(allFields.filter((f) => f.isMutable));
465
- 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);
466
502
  }
467
503
  exports.buildContractByteCode = buildContractByteCode;
468
- var ApiValType;
469
- (function (ApiValType) {
470
- ApiValType[ApiValType["Bool"] = 0] = "Bool";
471
- ApiValType[ApiValType["I256"] = 1] = "I256";
472
- ApiValType[ApiValType["U256"] = 2] = "U256";
473
- ApiValType[ApiValType["ByteVec"] = 3] = "ByteVec";
474
- ApiValType[ApiValType["Address"] = 4] = "Address";
475
- })(ApiValType || (ApiValType = {}));
476
- function encodeContractFieldI256(value) {
477
- return new Uint8Array([ApiValType.I256, ...encodeI256(value)]);
478
- }
479
- function encodeContractFieldU256(value) {
480
- return new Uint8Array([ApiValType.U256, ...encodeU256(value)]);
481
- }
482
504
  function encodeContractField(tpe, value) {
483
505
  switch (tpe) {
484
506
  case 'Bool':
485
- const byte = (0, api_1.toApiBoolean)(value) ? 1 : 0;
486
- return new Uint8Array([ApiValType.Bool, byte]);
507
+ return encodeVmBool((0, api_1.toApiBoolean)(value));
487
508
  case 'I256':
488
- const i256 = (0, api_1.toApiNumber256)(value);
489
- return encodeContractFieldI256(BigInt(i256));
509
+ return encodeVmI256(BigInt((0, api_1.toApiNumber256)(value)));
490
510
  case 'U256':
491
- const u256 = (0, api_1.toApiNumber256)(value);
492
- return encodeContractFieldU256(BigInt(u256));
511
+ return encodeVmU256(BigInt((0, api_1.toApiNumber256)(value)));
493
512
  case 'ByteVec':
494
- const hexStr = (0, api_1.toApiByteVec)(value);
495
- return new Uint8Array([ApiValType.ByteVec, ...encodeByteVec(hexStr)]);
513
+ return encodeVmByteVec((0, api_1.toApiByteVec)(value));
496
514
  case 'Address':
497
- const address = (0, api_1.toApiAddress)(value);
498
- return new Uint8Array([ApiValType.Address, ...encodeAddress(address)]);
515
+ return encodeVmAddress((0, api_1.toApiAddress)(value));
499
516
  default:
500
517
  throw Error(`Expected primitive type, got ${tpe}`);
501
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.38.2",
3
+ "version": "0.39.1",
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 {
@@ -329,21 +352,25 @@ export function calcFieldSize(
329
352
  export function tryDecodeMapDebugLog(
330
353
  message: string
331
354
  ): { 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
355
+ if (!message.startsWith('insert at map path: ') && !message.startsWith('remove at map path: ')) {
356
+ return undefined
357
+ }
358
+ const parts = message.split(':')
359
+ if (parts.length !== 2) return undefined
360
+ const pathString = parts[1].slice(1)
361
+ if (!isHexString(pathString)) return undefined
336
362
 
337
- if (!isHexString(parts[0])) return undefined
338
- const remain = parts[0].slice(prefix.length)
339
- const suffixIndex = remain.indexOf('5f5f') // __
363
+ const prefix = '5f5f6d61705f5f' // __map__
364
+ const remain = pathString.slice(prefix.length)
365
+ const suffix = '5f5f' // __
366
+ const suffixIndex = remain.indexOf(suffix)
340
367
  if (suffixIndex === -1) return undefined
341
368
 
342
369
  const encodedMapIndex = remain.slice(0, suffixIndex)
343
370
  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 }
371
+ const encodedKey = hexToBinUnsafe(remain.slice(suffixIndex + suffix.length))
372
+ const isInsert = message.startsWith('insert')
373
+ return { path: pathString, mapIndex, encodedKey, isInsert }
347
374
  }
348
375
 
349
376
  export function decodePrimitive(value: Uint8Array, type: string): Val {
@@ -482,59 +509,51 @@ function _encodeField<T>(fieldName: string, encodeFunc: () => T): T {
482
509
  }
483
510
  }
484
511
 
485
- function encodeFields(fields: { name: string; type: string; value: Val }[]): string {
486
- const prefix = binToHex(encodeI256(BigInt(fields.length)))
487
- const encoded = fields
488
- .map((field) => binToHex(_encodeField(field.name, () => encodeContractField(field.type, field.value))))
489
- .join('')
490
- 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)
491
521
  }
492
522
 
493
- export function buildContractByteCode(
494
- bytecode: string,
523
+ export function encodeContractFields(
495
524
  fields: Fields,
496
525
  fieldsSig: FieldsSig,
497
526
  structs: Struct[]
498
- ): string {
527
+ ): { encodedImmFields: Uint8Array; encodedMutFields: Uint8Array } {
499
528
  const allFields = flattenFields(fields, fieldsSig.names, fieldsSig.types, fieldsSig.isMutable, structs)
500
- const encodedImmFields = encodeFields(allFields.filter((f) => !f.isMutable))
501
- const encodedMutFields = encodeFields(allFields.filter((f) => f.isMutable))
502
- return bytecode + encodedImmFields + encodedMutFields
503
- }
504
-
505
- enum ApiValType {
506
- Bool = 0,
507
- I256 = 1,
508
- U256 = 2,
509
- ByteVec = 3,
510
- Address = 4
511
- }
512
-
513
- function encodeContractFieldI256(value: bigint): Uint8Array {
514
- 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
+ }
515
533
  }
516
534
 
517
- function encodeContractFieldU256(value: bigint): Uint8Array {
518
- 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)
519
543
  }
520
544
 
521
545
  export function encodeContractField(tpe: string, value: Val): Uint8Array {
522
546
  switch (tpe) {
523
547
  case 'Bool':
524
- const byte = toApiBoolean(value) ? 1 : 0
525
- return new Uint8Array([ApiValType.Bool, byte])
548
+ return encodeVmBool(toApiBoolean(value))
526
549
  case 'I256':
527
- const i256 = toApiNumber256(value)
528
- return encodeContractFieldI256(BigInt(i256))
550
+ return encodeVmI256(BigInt(toApiNumber256(value)))
529
551
  case 'U256':
530
- const u256 = toApiNumber256(value)
531
- return encodeContractFieldU256(BigInt(u256))
552
+ return encodeVmU256(BigInt(toApiNumber256(value)))
532
553
  case 'ByteVec':
533
- const hexStr = toApiByteVec(value)
534
- return new Uint8Array([ApiValType.ByteVec, ...encodeByteVec(hexStr)])
554
+ return encodeVmByteVec(toApiByteVec(value))
535
555
  case 'Address':
536
- const address = toApiAddress(value)
537
- return new Uint8Array([ApiValType.Address, ...encodeAddress(address)])
556
+ return encodeVmAddress(toApiAddress(value))
538
557
  default:
539
558
  throw Error(`Expected primitive type, got ${tpe}`)
540
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
 
@@ -1,3 +1,5 @@
1
+ import "std/fungible_token_interface"
2
+
1
3
  @std(enabled = false)
2
4
  Abstract Contract FungibleTokenUnimplemented() implements IFungibleToken {
3
5
  pub fn getSymbol() -> ByteVec {
@@ -1,3 +1,5 @@
1
+ import "std/nft_interface"
2
+
1
3
  @std(id = #0002)
2
4
  Interface INFTCollection {
3
5
  // Collection Uri points to a json file containing metadata for the NFT collection.
@@ -1,3 +1,5 @@
1
+ import "std/nft_collection_interface"
2
+
1
3
  @std(id = #000201)
2
4
  Interface INFTCollectionWithRoyalty extends INFTCollection {
3
5
  pub fn royaltyAmount(tokenId: ByteVec, salePrice: U256) -> (U256)