From 1c420415f7efb1890e69ae258a018c0358d7437e Mon Sep 17 00:00:00 2001 From: Andreea Eftene Date: Mon, 4 Sep 2023 20:42:40 +0200 Subject: [PATCH 1/4] add risc v check --- packages/api-contract/src/base/Code.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/packages/api-contract/src/base/Code.ts b/packages/api-contract/src/base/Code.ts index ef5a34691302..a34b7ddb43a4 100644 --- a/packages/api-contract/src/base/Code.ts +++ b/packages/api-contract/src/base/Code.ts @@ -12,7 +12,7 @@ import type { AbiConstructor, BlueprintOptions } from '../types.js'; import type { MapConstructorExec } from './types.js'; import { SubmittableResult } from '@polkadot/api'; -import { BN_ZERO, compactAddLength, isUndefined, isWasm, u8aToU8a } from '@polkadot/util'; +import { BN_ZERO, compactAddLength, isU8a, isUndefined, isWasm, u8aEq, u8aToU8a } from '@polkadot/util'; import { applyOnEvent } from '../util.js'; import { Base } from './Base.js'; @@ -34,6 +34,15 @@ export class CodeSubmittableResult extends Submittable } } +function isRiscV(bytes: unknown) { + const ELF_MAGIC = new Uint8Array([0x7f, 0x45, 0x4c, 0x46]); // ELF magic bytes: 0x7f, 'E', 'L', 'F' + return isU8a(bytes) && u8aEq(bytes.subarray(0, 4), ELF_MAGIC); +} + +function isValidCode(code: Uint8Array) { + return isWasm(code) || isRiscV(code) +} + export class Code extends Base { readonly code: Uint8Array; @@ -42,12 +51,12 @@ export class Code extends Base { constructor (api: ApiBase, abi: string | Record | Abi, wasm: Uint8Array | string | Buffer | null | undefined, decorateMethod: DecorateMethod) { super(api, abi, decorateMethod); - this.code = isWasm(this.abi.info.source.wasm) + this.code = isValidCode(this.abi.info.source.wasm) ? this.abi.info.source.wasm : u8aToU8a(wasm); - if (!isWasm(this.code)) { - throw new Error('No WASM code provided'); + if (!isValidCode(this.code)) { + throw new Error('Invalid code provided'); } this.abi.constructors.forEach((c): void => { From 0746103e529dda08114e464827194c588ba8c834 Mon Sep 17 00:00:00 2001 From: Andreea Eftene Date: Mon, 4 Sep 2023 20:48:10 +0200 Subject: [PATCH 2/4] run lint --- packages/api-contract/src/base/Code.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/api-contract/src/base/Code.ts b/packages/api-contract/src/base/Code.ts index a34b7ddb43a4..1f2bbe5d0486 100644 --- a/packages/api-contract/src/base/Code.ts +++ b/packages/api-contract/src/base/Code.ts @@ -34,13 +34,14 @@ export class CodeSubmittableResult extends Submittable } } -function isRiscV(bytes: unknown) { +function isRiscV (bytes: unknown) { const ELF_MAGIC = new Uint8Array([0x7f, 0x45, 0x4c, 0x46]); // ELF magic bytes: 0x7f, 'E', 'L', 'F' + return isU8a(bytes) && u8aEq(bytes.subarray(0, 4), ELF_MAGIC); } -function isValidCode(code: Uint8Array) { - return isWasm(code) || isRiscV(code) +function isValidCode (code: Uint8Array) { + return isWasm(code) || isRiscV(code); } export class Code extends Base { From b1bc59efb6a7cee9c44be47daa6b1dbbaec91067 Mon Sep 17 00:00:00 2001 From: Andreea Eftene <42985153+statictype@users.noreply.github.com> Date: Mon, 4 Sep 2023 22:31:12 +0200 Subject: [PATCH 3/4] Update packages/api-contract/src/base/Code.ts Co-authored-by: Jaco --- packages/api-contract/src/base/Code.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/api-contract/src/base/Code.ts b/packages/api-contract/src/base/Code.ts index 1f2bbe5d0486..665c3edc2f3f 100644 --- a/packages/api-contract/src/base/Code.ts +++ b/packages/api-contract/src/base/Code.ts @@ -34,7 +34,7 @@ export class CodeSubmittableResult extends Submittable } } -function isRiscV (bytes: unknown) { +function isRiscV (bytes: unknown): bytes is Uint8Array { const ELF_MAGIC = new Uint8Array([0x7f, 0x45, 0x4c, 0x46]); // ELF magic bytes: 0x7f, 'E', 'L', 'F' return isU8a(bytes) && u8aEq(bytes.subarray(0, 4), ELF_MAGIC); From 6644854da80d50dc28ca092eac3d4801c83f653a Mon Sep 17 00:00:00 2001 From: Andreea Eftene <42985153+statictype@users.noreply.github.com> Date: Mon, 4 Sep 2023 22:32:14 +0200 Subject: [PATCH 4/4] add explicit return type Co-authored-by: Jaco --- packages/api-contract/src/base/Code.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/api-contract/src/base/Code.ts b/packages/api-contract/src/base/Code.ts index 665c3edc2f3f..ecfe2f8ab5f2 100644 --- a/packages/api-contract/src/base/Code.ts +++ b/packages/api-contract/src/base/Code.ts @@ -40,7 +40,7 @@ function isRiscV (bytes: unknown): bytes is Uint8Array { return isU8a(bytes) && u8aEq(bytes.subarray(0, 4), ELF_MAGIC); } -function isValidCode (code: Uint8Array) { +function isValidCode (code: Uint8Array): boolean { return isWasm(code) || isRiscV(code); }