Skip to content

Commit

Permalink
Merge pull request #1350 from Phala-Network/fix-jssdk-unpload-code-wi…
Browse files Browse the repository at this point in the history
…th-uint8array

fix: PinkCodePromise does not handle the uint8array wasm source code as expected
  • Loading branch information
Leechael authored Jul 28, 2023
2 parents 2cb5c1b + 8878507 commit ac4ecec
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
2 changes: 1 addition & 1 deletion frontend/packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@phala/sdk",
"version": "0.4.1",
"version": "0.5.0",
"description": "Phala Phat Contract JS SDK",
"license": "Apache-2.0",
"homepage": "https://github.com/Phala-Network/js-sdk/tree/main/packages/sdk#readme",
Expand Down
19 changes: 13 additions & 6 deletions frontend/packages/sdk/src/contracts/PinkCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { SubmittableResult, toPromiseMethod } from '@polkadot/api';
import { ApiBase } from '@polkadot/api/base';
import { Abi } from '@polkadot/api-contract/Abi';
import { createBluePrintTx } from '@polkadot/api-contract/base/util';
import { isUndefined, isWasm, u8aToU8a } from '@polkadot/util';
import { hexToU8a, isU8a, isUndefined, isWasm, u8aToHex } from '@polkadot/util';

import { PinkBlueprintPromise } from './PinkBlueprint';

Expand All @@ -22,7 +22,7 @@ export class InkCodeSubmittableResult extends SubmittableResult {
readonly registry: OnChainRegistry;
readonly abi: Abi;
readonly blueprint?: PinkBlueprintPromise;

#isFinalized: boolean = false

constructor (result: ISubmittableResult, abi: Abi, registry: OnChainRegistry) {
Expand Down Expand Up @@ -71,7 +71,7 @@ export class PinkCodePromise {

readonly #tx: MapConstructorExec<'promise'> = {};

constructor (api: ApiBase<'promise'>, phatRegistry: OnChainRegistry, abi: AbiLike, wasm: WasmLike) {
constructor (api: ApiBase<'promise'>, phatRegistry: OnChainRegistry, abi: AbiLike, wasm: Uint8Array | string | Buffer | null | undefined) {
if (!api || !api.isConnected || !api.tx) {
throw new Error('Your API has not been initialized correctly and is not connected to a chain');
}
Expand All @@ -86,9 +86,16 @@ export class PinkCodePromise {
this._decorateMethod = toPromiseMethod;
this.phatRegistry = phatRegistry

this.code = isWasm(this.abi.info.source.wasm)
? this.abi.info.source.wasm
: u8aToU8a(wasm);
// NOTE: we only tested with the .contract file & wasm in Uint8Array.
if (isWasm(this.abi.info.source.wasm)) {
this.code = this.abi.info.source.wasm;
} else if (isU8a(wasm)) {
this.code = wasm;
} else if (typeof wasm === 'string' && wasm.substring(0, 2) === '0x') {
this.code = hexToU8a(wasm);
} else {
throw new Error('`wasm` should hex encoded string or Uint8Array.');
}

if (!isWasm(this.code)) {
throw new Error('No WASM code provided');
Expand Down
19 changes: 15 additions & 4 deletions frontend/packages/sdk/src/contracts/PinkContract.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import type { Bytes } from '@polkadot/types';
import type { Codec, IEnum, Registry, ISubmittableResult } from '@polkadot/types/types';
import type { SubmittableExtrinsic } from '@polkadot/api/submittable/types';
import type { AccountId, ContractExecResult, EventRecord } from '@polkadot/types/interfaces';
import type { ApiPromise } from '@polkadot/api';
import type { ApiBase } from '@polkadot/api/base';
import type { ISubmittableResult } from '@polkadot/types/types';
import type { AbiMessage, ContractOptions, ContractCallOutcome, DecodedEvent } from '@polkadot/api-contract/types';
import type { ContractCallResult, ContractCallSend, MessageMeta, ContractTx, MapMessageTx } from '@polkadot/api-contract/base/types';
import type { Registry } from '@polkadot/types/types';
import type { DecorateMethod, ApiTypes } from '@polkadot/api/types';
import type { KeyringPair } from '@polkadot/keyring/types';

import type { OnChainRegistry } from '../OnChainRegistry';
import type { AbiLike } from '../types';
Expand All @@ -29,8 +27,21 @@ import { randomHex } from "../lib/hex";
import assert from '../lib/assert';


export type PinkContractCallOutcome<ResultType> = {
output: ResultType
} & Omit<ContractCallOutcome, 'output'>;

export interface ILooseResult<O, E extends Codec = Codec> extends IEnum {
readonly asErr: E;
readonly asOk: O;
readonly isErr: boolean;
readonly isOk: boolean;
}

export interface ContractInkQuery<ApiType extends ApiTypes> extends MessageMeta {
(origin: string | AccountId | Uint8Array, ...params: unknown[]): ContractCallResult<ApiType, ContractCallOutcome>;
<ResultType = Codec, ErrType extends Codec = Codec>(origin: string | AccountId | Uint8Array, ...params: unknown[]): ContractCallResult<
ApiType, PinkContractCallOutcome<ILooseResult<ResultType, ErrType>>
>;
}

export interface MapMessageInkQuery<ApiType extends ApiTypes> {
Expand Down

0 comments on commit ac4ecec

Please sign in to comment.