Skip to content

Commit

Permalink
add risc v check
Browse files Browse the repository at this point in the history
  • Loading branch information
statictype committed Sep 4, 2023
1 parent 257c957 commit 1c42041
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions packages/api-contract/src/base/Code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -34,6 +34,15 @@ export class CodeSubmittableResult<ApiType extends ApiTypes> extends Submittable
}
}

function isRiscV(bytes: unknown) {

Check failure on line 37 in packages/api-contract/src/base/Code.ts

View workflow job for this annotation

GitHub Actions / pr (lint)

Missing space before function parentheses
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);

Check failure on line 39 in packages/api-contract/src/base/Code.ts

View workflow job for this annotation

GitHub Actions / pr (lint)

Expected blank line before this statement
}

function isValidCode(code: Uint8Array) {

Check failure on line 42 in packages/api-contract/src/base/Code.ts

View workflow job for this annotation

GitHub Actions / pr (lint)

Missing space before function parentheses
return isWasm(code) || isRiscV(code)

Check failure on line 43 in packages/api-contract/src/base/Code.ts

View workflow job for this annotation

GitHub Actions / pr (lint)

Missing semicolon
}

export class Code<ApiType extends ApiTypes> extends Base<ApiType> {
readonly code: Uint8Array;

Expand All @@ -42,12 +51,12 @@ export class Code<ApiType extends ApiTypes> extends Base<ApiType> {
constructor (api: ApiBase<ApiType>, abi: string | Record<string, unknown> | Abi, wasm: Uint8Array | string | Buffer | null | undefined, decorateMethod: DecorateMethod<ApiType>) {
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 => {
Expand Down

0 comments on commit 1c42041

Please sign in to comment.