@alephium/web3 2.0.8 → 2.0.10
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.
|
@@ -1252,13 +1252,39 @@ exports.subscribeContractEvents = subscribeContractEvents;
|
|
|
1252
1252
|
async function callMethod(contract, instance, methodName, params, getContractByCodeHash) {
|
|
1253
1253
|
const methodIndex = contract.contract.getMethodIndex(methodName);
|
|
1254
1254
|
const txId = params?.txId ?? randomTxId();
|
|
1255
|
-
const
|
|
1255
|
+
const callArgs = getCallMethodArgs(params.args ?? {}, instance.groupIndex);
|
|
1256
|
+
const callParams = contract.contract.toApiCallContract({ ...params, txId: txId, args: callArgs }, instance.groupIndex, instance.address, methodIndex);
|
|
1256
1257
|
const result = await (0, global_1.getCurrentNodeProvider)().contracts.postContractsCallContract(callParams);
|
|
1257
1258
|
const callResult = contract.contract.fromApiCallContractResult(result, txId, methodIndex, getContractByCodeHash);
|
|
1258
1259
|
contract.contract.printDebugMessages(methodName, callResult.debugMessages);
|
|
1259
1260
|
return callResult;
|
|
1260
1261
|
}
|
|
1261
1262
|
exports.callMethod = callMethod;
|
|
1263
|
+
function tryGetCallMethodAddressArg(maybeAddress, groupIndex) {
|
|
1264
|
+
if (!(0, address_1.isValidAddress)(maybeAddress))
|
|
1265
|
+
return maybeAddress;
|
|
1266
|
+
if ((0, address_1.isGrouplessAddress)(maybeAddress)) {
|
|
1267
|
+
const rawAddress = (0, address_1.addressWithoutExplicitGroupIndex)(maybeAddress);
|
|
1268
|
+
return `${rawAddress}:${groupIndex}`;
|
|
1269
|
+
}
|
|
1270
|
+
return maybeAddress;
|
|
1271
|
+
}
|
|
1272
|
+
function getCallMethodArgsFromVal(val, groupIndex) {
|
|
1273
|
+
if (typeof val === 'string') {
|
|
1274
|
+
return tryGetCallMethodAddressArg(val, groupIndex);
|
|
1275
|
+
}
|
|
1276
|
+
if (Array.isArray(val)) {
|
|
1277
|
+
return val.map((v) => getCallMethodArgsFromVal(v, groupIndex));
|
|
1278
|
+
}
|
|
1279
|
+
return val;
|
|
1280
|
+
}
|
|
1281
|
+
function getCallMethodArgs(args, groupIndex) {
|
|
1282
|
+
const newArgs = {};
|
|
1283
|
+
for (const [key, value] of Object.entries(args)) {
|
|
1284
|
+
newArgs[`${key}`] = getCallMethodArgsFromVal(value, groupIndex);
|
|
1285
|
+
}
|
|
1286
|
+
return newArgs;
|
|
1287
|
+
}
|
|
1262
1288
|
async function signExecuteMethod(contract, instance, methodName, params) {
|
|
1263
1289
|
const methodIndex = contract.contract.getMethodIndex(methodName);
|
|
1264
1290
|
const functionSig = contract.contract.functions[methodIndex];
|
|
@@ -1451,7 +1477,8 @@ async function multicallMethods(contract, instance, _callss, getContractByCodeHa
|
|
|
1451
1477
|
const [methodName, params] = entry;
|
|
1452
1478
|
const methodIndex = contract.contract.getMethodIndex(methodName);
|
|
1453
1479
|
const txId = params?.txId ?? randomTxId();
|
|
1454
|
-
|
|
1480
|
+
const callArgs = getCallMethodArgs(params.args ?? {}, instance.groupIndex);
|
|
1481
|
+
return contract.contract.toApiCallContract({ ...params, txId: txId, args: callArgs }, instance.groupIndex, instance.address, methodIndex);
|
|
1455
1482
|
});
|
|
1456
1483
|
});
|
|
1457
1484
|
const result = await (0, global_1.getCurrentNodeProvider)().contracts.postContractsMulticallContract({ calls: callsParams.flat() });
|
|
@@ -29,7 +29,8 @@ class WebCrypto {
|
|
|
29
29
|
throw new TypeError("Failed to execute 'getRandomValues' on 'Crypto': parameter 1 is not of type 'ArrayBufferView'");
|
|
30
30
|
}
|
|
31
31
|
const bytes = new Uint8Array(array.buffer, array.byteOffset, array.byteLength);
|
|
32
|
-
|
|
32
|
+
// Prefer global Web Crypto (e.g. react-native-get-random-values) for mobile compatibility
|
|
33
|
+
if (globalThis.crypto && typeof globalThis.crypto.getRandomValues === 'function') {
|
|
33
34
|
globalThis.crypto.getRandomValues(bytes);
|
|
34
35
|
}
|
|
35
36
|
else {
|
package/package.json
CHANGED
package/src/contract/contract.ts
CHANGED
|
@@ -59,7 +59,15 @@ import {
|
|
|
59
59
|
isHexString,
|
|
60
60
|
hexToString
|
|
61
61
|
} from '../utils'
|
|
62
|
-
import {
|
|
62
|
+
import {
|
|
63
|
+
contractIdFromAddress,
|
|
64
|
+
groupOfAddress,
|
|
65
|
+
addressFromContractId,
|
|
66
|
+
subContractId,
|
|
67
|
+
isGrouplessAddress,
|
|
68
|
+
isValidAddress,
|
|
69
|
+
addressWithoutExplicitGroupIndex
|
|
70
|
+
} from '../address'
|
|
63
71
|
import { getCurrentNodeProvider } from '../global'
|
|
64
72
|
import { EventSubscribeOptions, EventSubscription, subscribeToEvents } from './events'
|
|
65
73
|
import { MINIMAL_CONTRACT_DEPOSIT, ONE_ALPH, TOTAL_NUMBER_OF_GROUPS } from '../constants'
|
|
@@ -1923,8 +1931,9 @@ export async function callMethod<I extends ContractInstance, F extends Fields, A
|
|
|
1923
1931
|
): Promise<CallContractResult<R>> {
|
|
1924
1932
|
const methodIndex = contract.contract.getMethodIndex(methodName)
|
|
1925
1933
|
const txId = params?.txId ?? randomTxId()
|
|
1934
|
+
const callArgs = getCallMethodArgs(params.args ?? {}, instance.groupIndex)
|
|
1926
1935
|
const callParams = contract.contract.toApiCallContract(
|
|
1927
|
-
{ ...params, txId: txId, args:
|
|
1936
|
+
{ ...params, txId: txId, args: callArgs },
|
|
1928
1937
|
instance.groupIndex,
|
|
1929
1938
|
instance.address,
|
|
1930
1939
|
methodIndex
|
|
@@ -1935,6 +1944,33 @@ export async function callMethod<I extends ContractInstance, F extends Fields, A
|
|
|
1935
1944
|
return callResult as CallContractResult<R>
|
|
1936
1945
|
}
|
|
1937
1946
|
|
|
1947
|
+
function tryGetCallMethodAddressArg(maybeAddress: string, groupIndex: number): Val {
|
|
1948
|
+
if (!isValidAddress(maybeAddress)) return maybeAddress
|
|
1949
|
+
if (isGrouplessAddress(maybeAddress)) {
|
|
1950
|
+
const rawAddress = addressWithoutExplicitGroupIndex(maybeAddress)
|
|
1951
|
+
return `${rawAddress}:${groupIndex}`
|
|
1952
|
+
}
|
|
1953
|
+
return maybeAddress
|
|
1954
|
+
}
|
|
1955
|
+
|
|
1956
|
+
function getCallMethodArgsFromVal(val: Val, groupIndex: number): Val {
|
|
1957
|
+
if (typeof val === 'string') {
|
|
1958
|
+
return tryGetCallMethodAddressArg(val, groupIndex)
|
|
1959
|
+
}
|
|
1960
|
+
if (Array.isArray(val)) {
|
|
1961
|
+
return val.map((v) => getCallMethodArgsFromVal(v, groupIndex))
|
|
1962
|
+
}
|
|
1963
|
+
return val
|
|
1964
|
+
}
|
|
1965
|
+
|
|
1966
|
+
function getCallMethodArgs<A extends Arguments>(args: A, groupIndex: number): A {
|
|
1967
|
+
const newArgs: Arguments = {}
|
|
1968
|
+
for (const [key, value] of Object.entries(args)) {
|
|
1969
|
+
newArgs[`${key}`] = getCallMethodArgsFromVal(value, groupIndex)
|
|
1970
|
+
}
|
|
1971
|
+
return newArgs as A
|
|
1972
|
+
}
|
|
1973
|
+
|
|
1938
1974
|
export async function signExecuteMethod<I extends ContractInstance, F extends Fields, A extends Arguments, R>(
|
|
1939
1975
|
contract: ContractFactory<I, F>,
|
|
1940
1976
|
instance: ContractInstance,
|
|
@@ -2199,8 +2235,9 @@ export async function multicallMethods<I extends ContractInstance, F extends Fie
|
|
|
2199
2235
|
const [methodName, params] = entry
|
|
2200
2236
|
const methodIndex = contract.contract.getMethodIndex(methodName)
|
|
2201
2237
|
const txId = params?.txId ?? randomTxId()
|
|
2238
|
+
const callArgs = getCallMethodArgs(params.args ?? {}, instance.groupIndex)
|
|
2202
2239
|
return contract.contract.toApiCallContract(
|
|
2203
|
-
{ ...params, txId: txId, args:
|
|
2240
|
+
{ ...params, txId: txId, args: callArgs },
|
|
2204
2241
|
instance.groupIndex,
|
|
2205
2242
|
instance.address,
|
|
2206
2243
|
methodIndex
|
package/src/utils/webcrypto.ts
CHANGED
|
@@ -31,7 +31,8 @@ export class WebCrypto {
|
|
|
31
31
|
}
|
|
32
32
|
const bytes = new Uint8Array(array.buffer, array.byteOffset, array.byteLength)
|
|
33
33
|
|
|
34
|
-
|
|
34
|
+
// Prefer global Web Crypto (e.g. react-native-get-random-values) for mobile compatibility
|
|
35
|
+
if (globalThis.crypto && typeof globalThis.crypto.getRandomValues === 'function') {
|
|
35
36
|
globalThis.crypto.getRandomValues(bytes)
|
|
36
37
|
} else {
|
|
37
38
|
randomFillSync(bytes)
|