Skip to content

Commit

Permalink
Merge branch 'main' into feat/serialize-enum
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Vergez - PhyloFane committed Jul 13, 2023
2 parents b786dc8 + 0e2fcd9 commit 1fe295d
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 69 deletions.
75 changes: 31 additions & 44 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@multiversx/sdk-core",
"version": "12.4.1",
"version": "12.5.0",
"description": "MultiversX SDK for JavaScript and TypeScript",
"main": "out/index.js",
"types": "out/index.d.js",
Expand Down Expand Up @@ -32,7 +32,7 @@
"buffer": "6.0.3",
"json-duplicate-key-handle": "1.0.0",
"keccak": "3.0.2",
"protobufjs": "6.11.3"
"protobufjs": "7.2.4"
},
"devDependencies": {
"@multiversx/sdk-network-providers": "1.2.1",
Expand Down
32 changes: 32 additions & 0 deletions src/smartcontracts/nativeSerializer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,38 @@ describe("test native serializer", () => {
assert.deepEqual(typedValues[2].valueOf(), [{ field0: new BigNumber(44), field1: false }, { field0: new BigNumber(45), field1: true }]);
});

it('should accept no value for variadic types', async () => {
const endpoint = AbiRegistry.create({
endpoints: [
{
name: 'foo',
inputs: [
{
type: 'u64',
},
{
name: 'features',
type: 'variadic<bytes>',
multi_arg: true,
},
],
outputs: [],
},
],
}).getEndpoint('foo');

// Using both native JavaScript objects and typed values
const typedValues = NativeSerializer.nativeToTypedValues(
[42],
endpoint
);

assert.deepEqual(typedValues[0].getType(), new U64Type());
assert.deepEqual(typedValues[0].valueOf(), new BigNumber(42));
assert.deepEqual(typedValues[1].getType(), new VariadicType(new BytesType()));
assert.deepEqual(typedValues[1].valueOf(), []);
});

it("should perform type inference (enums)", async () => {
const abiRegistry = AbiRegistry.create({
"endpoints": [
Expand Down
4 changes: 2 additions & 2 deletions src/smartcontracts/nativeSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export namespace NativeSerializer {
const lastEndpointParamIndex = parameters.length - 1;
const argAtIndex = args[lastEndpointParamIndex];

if (argAtIndex.belongsToTypesystem) {
if (argAtIndex?.belongsToTypesystem) {
const isVariadicValue = argAtIndex.hasClassOrSuperclass(VariadicValue.ClassName);
if (!isVariadicValue) {
throw new ErrInvalidArgument(`Wrong argument type for endpoint ${endpoint.name}: typed value provided; expected variadic type, have ${argAtIndex.getClassName()}`);
Expand Down Expand Up @@ -296,7 +296,7 @@ export namespace NativeSerializer {
}

// TODO: move logic to typesystem/numerical.ts
function convertNumericalType(number: BigNumber, type: Type, errorContext: ArgumentErrorContext): TypedValue {
function convertNumericalType(number: BigNumber.Value, type: Type, errorContext: ArgumentErrorContext): TypedValue {
switch (type.constructor) {
case U8Type:
return new U8Value(number);
Expand Down
8 changes: 4 additions & 4 deletions src/testutils/mockProvider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AccountOnNetwork, ContractResultItem, ContractResults, TransactionOnNetwork, TransactionStatus } from "@multiversx/sdk-network-providers";
import { ContractResultItem, ContractResults, TransactionOnNetwork, TransactionStatus } from "@multiversx/sdk-network-providers";
import { Address } from "../address";
import { AsyncTimer } from "../asyncTimer";
import * as errors from "../errors";
Expand Down Expand Up @@ -26,15 +26,15 @@ export class MockProvider {

this.accounts.set(
MockProvider.AddressOfAlice.bech32(),
new AccountOnNetwork({ nonce: 0, balance: createAccountBalance(1000) })
{ nonce: 0, balance: createAccountBalance(1000) }
);
this.accounts.set(
MockProvider.AddressOfBob.bech32(),
new AccountOnNetwork({ nonce: 5, balance: createAccountBalance(500) })
{ nonce: 5, balance: createAccountBalance(500) }
);
this.accounts.set(
MockProvider.AddressOfCarol.bech32(),
new AccountOnNetwork({ nonce: 42, balance: createAccountBalance(300) })
{ nonce: 42, balance: createAccountBalance(300) }
);
}

Expand Down
34 changes: 17 additions & 17 deletions src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,28 +371,28 @@ export class Transaction {
*
* @param signature The signature, as computed by a signer.
*/
applySignature(signature: ISignature | Buffer) {
if (signature instanceof Buffer) {
this.signature = signature;
} else {
this.signature = Buffer.from(signature.hex(), "hex");
}

applySignature(signature: ISignature | Uint8Array) {
this.signature = this.interpretSignatureAsBuffer(signature);
this.hash = TransactionHash.compute(this);
}

/**
* Applies the guardian signature on the transaction.
*
* @param guardianSignature The signature, as computed by a signer.
*/
applyGuardianSignature(guardianSignature: ISignature | Buffer) {
if (guardianSignature instanceof Buffer) {
this.guardianSignature = guardianSignature;
} else {
this.guardianSignature = Buffer.from(guardianSignature.hex(), "hex");
private interpretSignatureAsBuffer(signature: ISignature | Uint8Array): Buffer {
if (ArrayBuffer.isView(signature)) {
return Buffer.from(signature);
} else if ((<any>signature).hex != null) {
return Buffer.from(signature.hex(), "hex");
}

throw new Error(`Object cannot be interpreted as a signature: ${signature}`);
}

/**
* Applies the guardian signature on the transaction.
*
* @param guardianSignature The signature, as computed by a signer.
*/
applyGuardianSignature(guardianSignature: ISignature | Uint8Array) {
this.guardianSignature = this.interpretSignatureAsBuffer(guardianSignature);
this.hash = TransactionHash.compute(this);
}

Expand Down

0 comments on commit 1fe295d

Please sign in to comment.