Skip to content

Commit

Permalink
Merge branch 'main' into merge-main-into-next-30-ian
Browse files Browse the repository at this point in the history
  • Loading branch information
andreibancioiu committed Jan 30, 2024
2 parents 1dc461d + e48badc commit 55852b7
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 112 deletions.
34 changes: 17 additions & 17 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": "v13.0.0-alpha.2",
"version": "12.18.0",
"description": "MultiversX SDK for JavaScript and TypeScript",
"main": "out/index.js",
"types": "out/index.d.js",
Expand Down Expand Up @@ -43,7 +43,7 @@
"@types/mocha": "9.1.0",
"@types/node": "13.13.2",
"assert": "2.0.0",
"axios": "1.6.2",
"axios": "1.6.5",
"browserify": "17.0.0",
"chai": "4.2.0",
"esmify": "2.1.1",
Expand Down
6 changes: 6 additions & 0 deletions src/address.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,10 @@ describe("test address", () => {
assert.throw(() => new Address("erd1l453hd0gt5gzdp7czpuall8ggt2dcv5zwmfdf3sd3lguxseux2"), errors.ErrAddressCannotCreate);
assert.throw(() => new Address("xerd1l453hd0gt5gzdp7czpuall8ggt2dcv5zwmfdf3sd3lguxseux2fsmsgldz"), errors.ErrAddressCannotCreate);
});

it("should validate the address without throwing the error", () => {
assert.isTrue(Address.isValid(aliceBech32));
assert.isFalse(Address.isValid('xerd1l453hd0gt5gzdp7czpuall8ggt2dcv5zwmfdf3sd3lguxseux2fsmsgldz'));
assert.isFalse(Address.isValid('erd1l453hd0gt5gzdp7czpuall8ggt2dcv5zwmfdf3sd3lguxseux2'))
})
});
17 changes: 17 additions & 0 deletions src/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,23 @@ export class Address {
return Address.fromValidHex(pubkey.toString("hex"));
}

/**
* Performs address validation without throwing errors
*/
static isValid(value: string): boolean {
const decoded = bech32.decodeUnsafe(value);
const prefix = decoded?.prefix;
const pubkey = decoded
? Buffer.from(bech32.fromWords(decoded.words))
: undefined;

if (prefix !== HRP || pubkey?.length !== PUBKEY_LENGTH) {
return false;
}

return true;
}

/**
* Returns the hex representation of the address (pubkey)
*/
Expand Down
12 changes: 12 additions & 0 deletions src/smartcontracts/typesystem/abiRegistry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,16 @@ describe("test abi registry", () => {
assert.deepEqual(setStatusEvent.inputs[1].type, new U64Type());
assert.deepEqual(setStatusEvent.inputs[2].type, registry.getCustomType("TransactionStatus"));
});

it("should load ABI explicit-enum", async () => {
const registry = await loadAbiRegistry("src/testdata/explicit-enum.abi.json");

const enumType = registry.getEnum("OperationCompletionStatus");

assert.deepEqual(enumType.variants[0].name, "completed");
assert.deepEqual(enumType.variants[0].discriminant, 0);

assert.deepEqual(enumType.variants[1].name, "interrupted");
assert.deepEqual(enumType.variants[1].discriminant, 1);
});
});
2 changes: 1 addition & 1 deletion src/smartcontracts/typesystem/abiRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class AbiRegistry {

if (typeDefinition.type == "struct") {
customTypes.push(StructType.fromJSON({ name: customTypeName, fields: typeDefinition.fields }));
} else if (typeDefinition.type == "enum") {
} else if (typeDefinition.type == "enum" || typeDefinition.type == "explicit-enum") {
customTypes.push(EnumType.fromJSON({ name: customTypeName, variants: typeDefinition.variants }));
} else {
throw new errors.ErrTypingSystem(`Cannot handle custom type: ${customTypeName}`);
Expand Down
19 changes: 18 additions & 1 deletion src/smartcontracts/typesystem/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,27 @@ export class EnumType extends CustomType {
}

static fromJSON(json: { name: string; variants: any[] }): EnumType {
let variants = (json.variants || []).map((variant) => EnumVariantDefinition.fromJSON(variant));
const rawVariants = EnumType.assignMissingDiscriminants(json.variants || []);
const variants = rawVariants.map((variant) => EnumVariantDefinition.fromJSON(variant));
return new EnumType(json.name, variants);
}

// For some enums (e.g. some "explicit-enum" types), the discriminants are missing.
private static assignMissingDiscriminants(variants: any[]): any[] {
const allDiscriminantsAreMissing = variants.every((variant) => variant.discriminant == undefined);
if (!allDiscriminantsAreMissing) {
// We only assign discriminants if all of them are missing.
return variants;
}

return variants.map((variant, index) => {
return {
...variant,
discriminant: index
}
});
}

getVariantByDiscriminant(discriminant: number): EnumVariantDefinition {
let result = this.variants.find((e) => e.discriminant == discriminant);
guardValueIsSet(`variant by discriminant (${discriminant})`, result);
Expand Down
32 changes: 32 additions & 0 deletions src/testdata/explicit-enum.abi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"endpoints": [
{
"name": "foobar",
"inputs": [],
"outputs": [
{
"type": "OperationCompletionStatus"
}
]
}
],
"types": {
"OperationCompletionStatus": {
"type": "explicit-enum",
"variants": [
{
"docs": [
"indicates that operation was completed"
],
"name": "completed"
},
{
"docs": [
"indicates that operation was interrupted prematurely, due to low gas"
],
"name": "interrupted"
}
]
}
}
}
24 changes: 12 additions & 12 deletions src/tokenOperations/tokenOperationsFactory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ describe("test factory", () => {
canWipe: true,
canPause: true,
canChangeOwner: true,
canUpgrade: true,
canAddSpecialRoles: true,
canUpgrade: false,
canAddSpecialRoles: false,
transactionNonce: 42
});

assert.equal(transaction.getData().toString(), "issue@4652414e4b@4652414e4b@64@@63616e467265657a65@74727565@63616e57697065@74727565@63616e5061757365@74727565@63616e4368616e67654f776e6572@74727565@63616e55706772616465@74727565@63616e4164645370656369616c526f6c6573@74727565")
assert.equal(transaction.getData().toString(), "issue@4652414e4b@4652414e4b@64@@63616e467265657a65@74727565@63616e57697065@74727565@63616e5061757365@74727565@63616e4368616e67654f776e6572@74727565@63616e55706772616465@66616c7365@63616e4164645370656369616c526f6c6573@66616c7365")
assert.equal(transaction.getNonce(), 42);
assert.equal(transaction.getSender().toString(), frank.address.toString());
assert.equal(transaction.getReceiver().toString(), "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzllls8a5w6u");
Expand All @@ -60,12 +60,12 @@ describe("test factory", () => {
canPause: true,
canTransferNFTCreateRole: true,
canChangeOwner: true,
canUpgrade: true,
canAddSpecialRoles: true,
canUpgrade: false,
canAddSpecialRoles: false,
transactionNonce: 42
});

assert.equal(transaction.getData().toString(), "issueSemiFungible@4652414e4b@4652414e4b@63616e467265657a65@74727565@63616e57697065@74727565@63616e5061757365@74727565@63616e5472616e736665724e4654437265617465526f6c65@74727565@63616e4368616e67654f776e6572@74727565@63616e55706772616465@74727565@63616e4164645370656369616c526f6c6573@74727565")
assert.equal(transaction.getData().toString(), "issueSemiFungible@4652414e4b@4652414e4b@63616e467265657a65@74727565@63616e57697065@74727565@63616e5061757365@74727565@63616e5472616e736665724e4654437265617465526f6c65@74727565@63616e4368616e67654f776e6572@74727565@63616e55706772616465@66616c7365@63616e4164645370656369616c526f6c6573@66616c7365")
assert.equal(transaction.getNonce(), 42);
assert.equal(transaction.getSender().toString(), frank.address.toString());
assert.equal(transaction.getReceiver().toString(), "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzllls8a5w6u");
Expand All @@ -81,12 +81,12 @@ describe("test factory", () => {
canPause: true,
canTransferNFTCreateRole: true,
canChangeOwner: true,
canUpgrade: true,
canAddSpecialRoles: true,
canUpgrade: false,
canAddSpecialRoles: false,
transactionNonce: 42
});

assert.equal(transaction.getData().toString(), "issueNonFungible@4652414e4b@4652414e4b@63616e467265657a65@74727565@63616e57697065@74727565@63616e5061757365@74727565@63616e5472616e736665724e4654437265617465526f6c65@74727565@63616e4368616e67654f776e6572@74727565@63616e55706772616465@74727565@63616e4164645370656369616c526f6c6573@74727565")
assert.equal(transaction.getData().toString(), "issueNonFungible@4652414e4b@4652414e4b@63616e467265657a65@74727565@63616e57697065@74727565@63616e5061757365@74727565@63616e5472616e736665724e4654437265617465526f6c65@74727565@63616e4368616e67654f776e6572@74727565@63616e55706772616465@66616c7365@63616e4164645370656369616c526f6c6573@66616c7365")
assert.equal(transaction.getNonce(), 42);
assert.equal(transaction.getSender().toString(), frank.address.toString());
assert.equal(transaction.getReceiver().toString(), "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzllls8a5w6u");
Expand All @@ -103,12 +103,12 @@ describe("test factory", () => {
canPause: true,
canTransferNFTCreateRole: true,
canChangeOwner: true,
canUpgrade: true,
canAddSpecialRoles: true,
canUpgrade: false,
canAddSpecialRoles: false,
transactionNonce: 42
});

assert.equal(transaction.getData().toString(), "registerMetaESDT@4652414e4b@4652414e4b@0a@63616e467265657a65@74727565@63616e57697065@74727565@63616e5061757365@74727565@63616e5472616e736665724e4654437265617465526f6c65@74727565@63616e4368616e67654f776e6572@74727565@63616e55706772616465@74727565@63616e4164645370656369616c526f6c6573@74727565")
assert.equal(transaction.getData().toString(), "registerMetaESDT@4652414e4b@4652414e4b@0a@63616e467265657a65@74727565@63616e57697065@74727565@63616e5061757365@74727565@63616e5472616e736665724e4654437265617465526f6c65@74727565@63616e4368616e67654f776e6572@74727565@63616e55706772616465@66616c7365@63616e4164645370656369616c526f6c6573@66616c7365")
assert.equal(transaction.getNonce(), 42);
assert.equal(transaction.getSender().toString(), frank.address.toString());
assert.equal(transaction.getReceiver().toString(), "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzllls8a5w6u");
Expand Down
Loading

0 comments on commit 55852b7

Please sign in to comment.