Skip to content

Commit

Permalink
Merge pull request EOSIO#384 from EOSIO/abiProvider
Browse files Browse the repository at this point in the history
added optional abiProvider to Api constructor
  • Loading branch information
c0d3ster authored Oct 5, 2018
2 parents 73725d6 + a6def15 commit d7399f5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
18 changes: 14 additions & 4 deletions src/eosjs-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"use strict";

import { Abi, GetInfoResult, JsonRpc, PushTransactionArgs } from "./eosjs-jsonrpc";
import { base64ToBinary } from "./eosjs-numeric";
import * as ser from "./eosjs-serialize";

// tslint:disable-next-line
Expand All @@ -33,6 +32,12 @@ export interface AuthorityProvider {
getRequiredKeys: (args: AuthorityProviderArgs) => Promise<string[]>;
}

/** Retrieves raw ABIs for a specified accountName */
export interface AbiProvider {
getRawAbi: (accountName: string) => Promise<BinaryAbi>;
}

/** Structure for the raw form of ABIs */
export interface BinaryAbi {
account_name: string;
abi: Uint8Array;
Expand Down Expand Up @@ -78,6 +83,9 @@ export class Api {
/** Get subset of `availableKeys` needed to meet authorities in a `transaction` */
public authorityProvider: AuthorityProvider;

/** Supplies ABIs in raw form (binary) */
public abiProvider: AbiProvider;

/** Signs transactions */
public signatureProvider: SignatureProvider;

Expand All @@ -103,6 +111,7 @@ export class Api {
* @param args
* * `rpc`: Issues RPC calls
* * `authorityProvider`: Get public keys needed to meet authorities in a transaction
* * `abiProvider`: Supplies ABIs in raw form (binary)
* * `signatureProvider`: Signs transactions
* * `chainId`: Identifies chain
* * `textEncoder`: `TextEncoder` instance to use. Pass in `null` if running in a browser
Expand All @@ -111,13 +120,15 @@ export class Api {
constructor(args: {
rpc: JsonRpc,
authorityProvider?: AuthorityProvider,
abiProvider?: AbiProvider,
signatureProvider: SignatureProvider,
chainId: string,
chainId?: string,
textEncoder?: TextEncoder,
textDecoder?: TextDecoder,
}) {
this.rpc = args.rpc;
this.authorityProvider = args.authorityProvider || args.rpc;
this.abiProvider = args.abiProvider || args.rpc;
this.signatureProvider = args.signatureProvider;
this.chainId = args.chainId;
this.textEncoder = args.textEncoder;
Expand Down Expand Up @@ -148,8 +159,7 @@ export class Api {
}
let cachedAbi: CachedAbi;
try {
// todo: use get_raw_abi when it becomes available
const rawAbi = base64ToBinary((await this.rpc.get_raw_code_and_abi(accountName)).abi);
const rawAbi = (await this.abiProvider.getRawAbi(accountName)).abi;
const abi = this.rawAbiToJson(rawAbi);
cachedAbi = { rawAbi, abi };
} catch (e) {
Expand Down
13 changes: 11 additions & 2 deletions src/eosjs-jsonrpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

// copyright defined in eosjs/LICENSE.txt

import { AuthorityProvider, AuthorityProviderArgs } from "./eosjs-api";
import { AbiProvider, AuthorityProvider, AuthorityProviderArgs, BinaryAbi } from "./eosjs-api";
import { base64ToBinary } from "./eosjs-numeric";
import { convertLegacyPublicKeys } from "./eosjs-numeric";
import { RpcError } from "./eosjs-rpcerror";

Expand Down Expand Up @@ -96,7 +97,7 @@ function arrayToHex(data: Uint8Array) {
}

/** Make RPC calls */
export class JsonRpc implements AuthorityProvider {
export class JsonRpc implements AuthorityProvider, AbiProvider {
public endpoint: string;
public fetchBuiltin: (input?: Request | string, init?: RequestInit) => Promise<Response>;

Expand Down Expand Up @@ -203,6 +204,14 @@ export class JsonRpc implements AuthorityProvider {
return await this.fetch("/v1/chain/get_raw_code_and_abi", { account_name });
}

/** calls `/v1/chain/get_raw_code_and_abi` and pulls out unneeded raw wasm code */
// TODO: use `/v1/chain/get_raw_abi` directly when it becomes available
public async getRawAbi(accountName: string): Promise<BinaryAbi> {
const rawCodeAndAbi = await this.get_raw_code_and_abi(accountName);
const abi = base64ToBinary(rawCodeAndAbi.abi);
return { account_name: rawCodeAndAbi.account_name, abi };
}

/** Raw call to `/v1/chain/get_table_rows` */
public async get_table_rows({
json = true,
Expand Down

0 comments on commit d7399f5

Please sign in to comment.