@alephium/web3 2.0.1 → 2.0.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,5 +1,5 @@
1
1
  import { NamedVals, node, NodeProvider, Number256, Token, Val } from '../api';
2
- import { SignDeployContractTxParams, SignDeployContractTxResult, SignExecuteScriptTxParams, SignerProvider, Address, SignExecuteScriptTxResult } from '../signer';
2
+ import { SignDeployContractTxParams, SignDeployContractTxResult, SignExecuteScriptTxParams, SignerProvider, Address, SignExecuteScriptTxResult, Account } from '../signer';
3
3
  import { Optional, HexString } from '../utils';
4
4
  import { EventSubscribeOptions, EventSubscription } from './events';
5
5
  import { contract, Method } from '../codec';
@@ -101,6 +101,7 @@ export declare class Script extends Artifact {
101
101
  static fromJson(artifact: any, bytecodeDebugPatch?: string, structs?: Struct[]): Script;
102
102
  static fromArtifactFile(path: string, bytecodeDebugPatch: string, structs?: Struct[]): Promise<Script>;
103
103
  toString(): string;
104
+ getBytecodeAndGroup<P extends Fields>(account: Account, fields: P): [string, number];
104
105
  txParamsForExecution<P extends Fields>(params: ExecuteScriptParams<P>): Promise<SignExecuteScriptTxParams>;
105
106
  buildByteCodeToDeploy(initialFields: Fields): string;
106
107
  }
@@ -522,10 +522,23 @@ class Script extends Artifact {
522
522
  };
523
523
  return JSON.stringify(object, null, 2);
524
524
  }
525
+ getBytecodeAndGroup(account, fields) {
526
+ const bytecode = this.buildByteCodeToDeploy(fields);
527
+ if ((0, signer_1.isGroupedAccount)(account)) {
528
+ return [bytecode, account.group];
529
+ }
530
+ const group = getGroupFromTxScript(bytecode);
531
+ const defaultGroup = (0, address_1.groupOfAddress)(account.address);
532
+ if (group === undefined || group === defaultGroup) {
533
+ return [bytecode, defaultGroup];
534
+ }
535
+ const newFields = ralph.updateFieldsWithGroup(fields, group);
536
+ const newBytecode = this.buildByteCodeToDeploy(newFields);
537
+ return [newBytecode, group];
538
+ }
525
539
  async txParamsForExecution(params) {
526
540
  const selectedAccount = await params.signer.getSelectedAccount();
527
- const bytecode = this.buildByteCodeToDeploy(params.initialFields ?? {});
528
- const group = getGroupFromTxScript(bytecode, selectedAccount);
541
+ const [bytecode, group] = this.getBytecodeAndGroup(selectedAccount, params.initialFields ?? {});
529
542
  const signerParams = {
530
543
  signerAddress: selectedAccount.address,
531
544
  signerKeyType: selectedAccount.keyType,
@@ -549,9 +562,7 @@ class Script extends Artifact {
549
562
  }
550
563
  }
551
564
  exports.Script = Script;
552
- function getGroupFromTxScript(bytecode, account) {
553
- if ((0, signer_1.isGroupedAccount)(account))
554
- return account.group;
565
+ function getGroupFromTxScript(bytecode) {
555
566
  const script = script_codec_1.scriptCodec.decode((0, utils_1.hexToBinUnsafe)(bytecode));
556
567
  const instrs = script.methods.flatMap((method) => method.instrs);
557
568
  for (let index = 0; index < instrs.length - 1; index += 1) {
@@ -574,7 +585,7 @@ function getGroupFromTxScript(bytecode, account) {
574
585
  }
575
586
  }
576
587
  }
577
- return (0, address_1.groupOfAddress)(account.address);
588
+ return undefined;
578
589
  }
579
590
  function fromApiFields(immFields, mutFields, fieldsSig, structs) {
580
591
  let [immIndex, mutIndex] = [0, 0];
@@ -63,6 +63,7 @@ export declare function flattenFields(fields: Fields, names: string[], types: st
63
63
  value: Val;
64
64
  isMutable: boolean;
65
65
  }[];
66
+ export declare function updateFieldsWithGroup(fields: Fields, group: number): Fields;
66
67
  export declare function buildScriptByteCode(bytecodeTemplate: string, fields: Fields, fieldsSig: FieldsSig, structs: Struct[]): string;
67
68
  export declare function encodeContractFields(fields: Fields, fieldsSig: FieldsSig, structs: Struct[]): {
68
69
  encodedImmFields: Uint8Array;
@@ -17,7 +17,7 @@ You should have received a copy of the GNU Lesser General Public License
17
17
  along with the library. If not, see <http://www.gnu.org/licenses/>.
18
18
  */
19
19
  Object.defineProperty(exports, "__esModule", { value: true });
20
- exports.buildDebugBytecode = exports.encodeContractField = exports.buildContractByteCode = exports.encodeContractFields = exports.buildScriptByteCode = exports.flattenFields = exports.typeLength = exports.encodeMapKey = 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 = void 0;
20
+ exports.buildDebugBytecode = exports.encodeContractField = exports.buildContractByteCode = exports.encodeContractFields = exports.buildScriptByteCode = exports.updateFieldsWithGroup = exports.flattenFields = exports.typeLength = exports.encodeMapKey = 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 = void 0;
21
21
  const api_1 = require("../api");
22
22
  const utils_1 = require("../utils");
23
23
  const codec_1 = require("../codec");
@@ -336,6 +336,31 @@ function checkPrimitiveValue(name, ralphType, value) {
336
336
  }
337
337
  throw Error(`Invalid value ${value} for ${name}, expected a value of type ${ralphType}`);
338
338
  }
339
+ function updateFieldsWithGroup(fields, group) {
340
+ const newFields = {};
341
+ for (const key in fields) {
342
+ const value = fields[`${key}`];
343
+ newFields[`${key}`] = updateValWithGroup(value, group);
344
+ }
345
+ return newFields;
346
+ }
347
+ exports.updateFieldsWithGroup = updateFieldsWithGroup;
348
+ function updateValWithGroup(value, group) {
349
+ if (typeof value === 'string') {
350
+ if (!(0, address_1.isValidAddress)(value))
351
+ return value;
352
+ if ((0, address_1.isGrouplessAddressWithoutGroupIndex)(value))
353
+ return `${value}:${group}`;
354
+ return value;
355
+ }
356
+ if (Array.isArray(value)) {
357
+ return value.map((v) => updateValWithGroup(v, group));
358
+ }
359
+ if (typeof value === 'object') {
360
+ return updateFieldsWithGroup(value, group);
361
+ }
362
+ return value;
363
+ }
339
364
  const scriptFieldRegex = /\{([0-9]*)\}/g;
340
365
  function buildScriptByteCode(bytecodeTemplate, fields, fieldsSig, structs) {
341
366
  const allFields = flattenFields(fields, fieldsSig.names, fieldsSig.types, fieldsSig.isMutable, structs);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alephium/web3",
3
- "version": "2.0.1",
3
+ "version": "2.0.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",
@@ -796,10 +796,26 @@ export class Script extends Artifact {
796
796
  return JSON.stringify(object, null, 2)
797
797
  }
798
798
 
799
+ getBytecodeAndGroup<P extends Fields>(account: Account, fields: P): [string, number] {
800
+ const bytecode = this.buildByteCodeToDeploy(fields)
801
+ if (isGroupedAccount(account)) {
802
+ return [bytecode, account.group]
803
+ }
804
+
805
+ const group = getGroupFromTxScript(bytecode)
806
+ const defaultGroup = groupOfAddress(account.address)
807
+ if (group === undefined || group === defaultGroup) {
808
+ return [bytecode, defaultGroup]
809
+ }
810
+
811
+ const newFields = ralph.updateFieldsWithGroup(fields, group) as P
812
+ const newBytecode = this.buildByteCodeToDeploy(newFields)
813
+ return [newBytecode, group]
814
+ }
815
+
799
816
  async txParamsForExecution<P extends Fields>(params: ExecuteScriptParams<P>): Promise<SignExecuteScriptTxParams> {
800
817
  const selectedAccount = await params.signer.getSelectedAccount()
801
- const bytecode = this.buildByteCodeToDeploy(params.initialFields ?? {})
802
- const group = getGroupFromTxScript(bytecode, selectedAccount)
818
+ const [bytecode, group] = this.getBytecodeAndGroup(selectedAccount, params.initialFields ?? {})
803
819
  const signerParams: SignExecuteScriptTxParams = {
804
820
  signerAddress: selectedAccount.address,
805
821
  signerKeyType: selectedAccount.keyType,
@@ -823,9 +839,7 @@ export class Script extends Artifact {
823
839
  }
824
840
  }
825
841
 
826
- function getGroupFromTxScript(bytecode: string, account: Account): number {
827
- if (isGroupedAccount(account)) return account.group
828
-
842
+ function getGroupFromTxScript(bytecode: string): number | undefined {
829
843
  const script = scriptCodec.decode(hexToBinUnsafe(bytecode))
830
844
  const instrs = script.methods.flatMap((method) => method.instrs)
831
845
  for (let index = 0; index < instrs.length - 1; index += 1) {
@@ -850,7 +864,7 @@ function getGroupFromTxScript(bytecode: string, account: Account): number {
850
864
  }
851
865
  }
852
866
  }
853
- return groupOfAddress(account.address)
867
+ return undefined
854
868
  }
855
869
 
856
870
  export function fromApiFields(
@@ -39,13 +39,13 @@ import {
39
39
  instrCodec,
40
40
  u256Codec,
41
41
  toU256,
42
- toI256,
43
- intAs4BytesCodec
42
+ toI256
44
43
  } from '../codec'
45
44
  import { boolCodec } from '../codec/codec'
46
45
  import { TraceableError } from '../error'
47
- import djb2 from '../utils/djb2'
48
- import { addressToBytes, groupFromHint } from '../address'
46
+ import { addressToBytes, isGrouplessAddressWithoutGroupIndex, isValidAddress } from '../address'
47
+ import { scriptCodec } from '../codec/script-codec'
48
+ import { TOTAL_NUMBER_OF_GROUPS } from '../constants'
49
49
 
50
50
  export function encodeByteVec(hex: string): Uint8Array {
51
51
  if (!isHexString(hex)) {
@@ -399,6 +399,32 @@ function checkPrimitiveValue(name: string, ralphType: string, value: Val): strin
399
399
  throw Error(`Invalid value ${value} for ${name}, expected a value of type ${ralphType}`)
400
400
  }
401
401
 
402
+ export function updateFieldsWithGroup(fields: Fields, group: number): Fields {
403
+ const newFields: Fields = {}
404
+ for (const key in fields) {
405
+ const value = fields[`${key}`]
406
+ newFields[`${key}`] = updateValWithGroup(value, group)
407
+ }
408
+ return newFields
409
+ }
410
+
411
+ function updateValWithGroup(value: Val, group: number): Val {
412
+ if (typeof value === 'string') {
413
+ if (!isValidAddress(value)) return value
414
+ if (isGrouplessAddressWithoutGroupIndex(value)) return `${value}:${group}`
415
+ return value
416
+ }
417
+
418
+ if (Array.isArray(value)) {
419
+ return value.map((v) => updateValWithGroup(v, group))
420
+ }
421
+
422
+ if (typeof value === 'object') {
423
+ return updateFieldsWithGroup(value as Fields, group)
424
+ }
425
+ return value
426
+ }
427
+
402
428
  const scriptFieldRegex = /\{([0-9]*)\}/g
403
429
 
404
430
  export function buildScriptByteCode(