Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Additional types for documentation/development #784

Merged
merged 12 commits into from
Aug 19, 2020
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"cypress": "^4.12.1",
"eosjs-ecc": "^4.0.7",
"eslint": "^6.8.0",
"jest": "^26.3.0",
"jest": "^26.4.0",
"jest-fetch-mock": "^3.0.3",
"ts-jest": "^26.2.0",
"ts-loader": "^7.0.5",
Expand Down
92 changes: 89 additions & 3 deletions src/eosjs-api-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import { Abi, PushTransactionArgs } from './eosjs-rpc-interfaces';
import { Anyvar, Authorization } from './eosjs-serialize';
import { Anyvar, Authorization, Action } from './eosjs-serialize';
import { WasmAbi } from './eosjs-wasmabi';
import { ActionBuilder } from './eosjs-api';

Expand Down Expand Up @@ -83,6 +83,24 @@ export interface SignatureProvider {
sign: (args: SignatureProviderArgs) => Promise<PushTransactionArgs>;
}

export interface Extension {
type: number;
data: string;
}

export interface Transaction {
expiration?: string;
ref_block_num?: number;
ref_block_prefix?: number;
max_net_usage_words?: number;
max_cpu_usage_ms?: number;
delay_sec?: number;
context_free_actions?: Action[];
context_free_data?: Uint8Array[];
actions: Action[];
transaction_extensions?: Extension[];
}

/** Optional transact configuration object */
export interface TransactConfig {
broadcast?: boolean;
Expand All @@ -94,6 +112,74 @@ export interface TransactConfig {
expireSeconds?: number;
}

export interface AccountDelta {
account: string;
delta: number;
}

export interface AuthSequence {
account: string;
sequence: number;
}

export interface ActionReceipt {
receiver: string;
act_digest: string;
global_sequence: number;
recv_sequence: number;
auth_sequence: AuthSequence[];
code_sequence: number;
abi_sequence: number;
}

export interface ActionTrace {
action_ordinal: number;
creator_action_ordinal: number;
closest_unnotified_ancestor_action_ordinal: number;
receipt: ActionReceipt;
receiver: string;
act: Action;
context_free: boolean;
elapsed: number;
console: string;
trx_id: string;
block_num: number;
block_time: string;
producer_block_id: string|null;
account_ram_deltas: AccountDelta[];
account_disk_deltas: AccountDelta[];
except: any;
error_code: number|null;
return_value: any;
inline_traces: ActionTrace[];
}

export interface TransactionReceiptHeader {
status: string;
cpu_usage_us: number;
net_usage_words: number;
}

export interface TransactionTrace {
id: string;
block_num: number;
block_time: string;
producer_block_id: string|null;
receipt: TransactionReceiptHeader|null;
elapsed: number;
net_usage: number;
scheduled: boolean;
action_traces: ActionTrace[];
account_ram_delta: AccountDelta|null;
except: string|null;
error_code: number|null;
}

export interface TransactResult {
transaction_id: string;
processed: TransactionTrace;
}

/** Optional query configuration object */
export interface QueryConfig {
sign?: boolean;
Expand All @@ -115,9 +201,9 @@ export type ContextFreeGroupCallback =
(index: {cfa: number, cfd: number}) => {
action?: ActionBuilder;
contextFreeAction?: ActionBuilder;
contextFreeData?: any;
contextFreeData?: Uint8Array;
};

export interface ActionSerializerType {
[actionName: string]: any
[actionName: string]: any;
};
44 changes: 23 additions & 21 deletions src/eosjs-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
QueryConfig,
SignatureProvider,
TransactConfig,
Transaction,
TransactResult,
WasmAbiProvider
} from './eosjs-api-interfaces';
import { JsonRpc } from './eosjs-jsonrpc';
Expand Down Expand Up @@ -156,7 +158,7 @@ export class Api {
}

/** Get abis needed by a transaction */
public async getTransactionAbis(transaction: any, reload = false): Promise<BinaryAbi[]> {
public async getTransactionAbis(transaction: Transaction, reload = false): Promise<BinaryAbi[]> {
const actions = (transaction.context_free_actions || []).concat(transaction.actions);
const accounts: string[] = actions.map((action: ser.Action): string => action.account);
const uniqueAccounts: Set<string> = new Set(accounts);
Expand Down Expand Up @@ -195,7 +197,7 @@ export class Api {
}

/** Convert a transaction to binary */
public serializeTransaction(transaction: any): Uint8Array {
public serializeTransaction(transaction: Transaction): Uint8Array {
const buffer = new ser.SerialBuffer({ textEncoder: this.textEncoder, textDecoder: this.textDecoder });
this.serialize(buffer, 'transaction', {
max_net_usage_words: 0,
Expand Down Expand Up @@ -223,7 +225,7 @@ export class Api {
}

/** Convert a transaction from binary. Leaves actions in hex. */
public deserializeTransaction(transaction: Uint8Array): any {
public deserializeTransaction(transaction: Uint8Array): Transaction {
const buffer = new ser.SerialBuffer({ textEncoder: this.textEncoder, textDecoder: this.textDecoder });
buffer.pushArray(transaction);
return this.deserialize(buffer, 'transaction');
Expand Down Expand Up @@ -263,7 +265,7 @@ export class Api {
}

/** Convert a transaction from binary. Also deserializes actions. */
public async deserializeTransactionWithActions(transaction: Uint8Array | string): Promise<any> {
public async deserializeTransactionWithActions(transaction: Uint8Array | string): Promise<Transaction> {
if (typeof transaction === 'string') {
transaction = ser.hexToUint8Array(transaction);
}
Expand Down Expand Up @@ -304,9 +306,9 @@ export class Api {
* @returns node response if `broadcast`, `{signatures, serializedTransaction}` if `!broadcast`
*/
public async transact(
transaction: any,
transaction: Transaction,
{ broadcast = true, sign = true, requiredKeys, compression, blocksBehind, useLastIrreversible, expireSeconds }:
TransactConfig = {}): Promise<any>
TransactConfig = {}): Promise<TransactResult|PushTransactionArgs>
{
let info: GetInfoResult;

Expand Down Expand Up @@ -399,7 +401,7 @@ export class Api {

const transaction = {
...ser.transactionHeader(refBlock, 60 * 30),
context_free_actions: [] as any[],
context_free_actions: [] as ser.Action[],
actions: [{
account,
name: 'queryit',
Expand Down Expand Up @@ -454,7 +456,7 @@ export class Api {
/** Broadcast a signed transaction */
public async pushSignedTransaction(
{ signatures, serializedTransaction, serializedContextFreeData }: PushTransactionArgs
): Promise<any> {
): Promise<TransactResult> {
return this.rpc.push_transaction({
signatures,
serializedTransaction,
Expand All @@ -464,7 +466,7 @@ export class Api {

public async pushCompressedSignedTransaction(
{ signatures, serializedTransaction, serializedContextFreeData }: PushTransactionArgs
): Promise<any> {
): Promise<TransactResult> {
const compressedSerializedTransaction = this.deflateSerializedArray(serializedTransaction);
const compressedSerializedContextFreeData =
this.deflateSerializedArray(serializedContextFreeData || new Uint8Array(0));
Expand All @@ -479,7 +481,7 @@ export class Api {

private async generateTapos(
info: GetInfoResult | undefined,
transaction: any,
transaction: Transaction,
blocksBehind: number | undefined,
useLastIrreversible: boolean | undefined,
expireSeconds: number
Expand Down Expand Up @@ -545,21 +547,21 @@ export class TransactionBuilder {
return this;
}

public async send(config?: TransactConfig): Promise<any> {
const contextFreeDataSet: any[] = [];
const contextFreeActions: ActionBuilder[] = [];
const actions: any[] = this.actions.map((actionBuilder) => actionBuilder.serializedData);
public async send(config?: TransactConfig): Promise<PushTransactionArgs|TransactResult> {
const contextFreeDataSet: Uint8Array[] = [];
const contextFreeActions: ser.SerializedAction[] = [];
const actions: ser.SerializedAction[] = this.actions.map((actionBuilder) => actionBuilder.serializedData as ser.SerializedAction);
await Promise.all(this.contextFreeGroups.map(
async (contextFreeCallback: ContextFreeGroupCallback) => {
const { action, contextFreeAction, contextFreeData } = contextFreeCallback({
cfd: contextFreeDataSet.length,
cfa: contextFreeActions.length
});
if (action) {
actions.push(action);
actions.push(action.serializedData);
}
if (contextFreeAction) {
contextFreeActions.push(contextFreeAction);
contextFreeActions.push(contextFreeAction.serializedData);
}
if (contextFreeData) {
contextFreeDataSet.push(contextFreeData);
Expand All @@ -569,8 +571,8 @@ export class TransactionBuilder {
this.contextFreeGroups = [];
this.actions = [];
return await this.api.transact({
contextFreeData: contextFreeDataSet,
contextFreeActions,
context_free_data: contextFreeDataSet,
context_free_actions: contextFreeActions,
actions
}, config);
}
Expand All @@ -579,23 +581,23 @@ export class TransactionBuilder {
export class ActionBuilder {
private api: Api;
private readonly accountName: string;
public serializedData: any;
public serializedData: ser.SerializedAction;

constructor(api: Api, accountName: string) {
this.api = api;
this.accountName = accountName;
}

public as(actorName?: string | ser.Authorization[]) {
let authorization: any[] = [];
let authorization: ser.Authorization[] = [];
if (actorName && typeof actorName === 'string') {
authorization = [{ actor: actorName, permission: 'active'}];
} else {
authorization = actorName as ser.Authorization[];
}

const wasmAbi = this.api.wasmAbiProvider.wasmAbis.get(this.accountName);
return new ActionSerializer(this, this.api, this.accountName, authorization, wasmAbi);
return new ActionSerializer(this, this.api, this.accountName, authorization, wasmAbi) as ActionSerializerType;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/eosjs-jssig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
import { convertLegacyPublicKey } from './eosjs-numeric';

/** expensive to construct; so we do it once and reuse it */
const defaultEc = new ec('secp256k1') as any;
const defaultEc = new ec('secp256k1');

/** Construct the digest from transaction details */
const digestFromSerializedData = (
Expand Down
8 changes: 4 additions & 4 deletions src/eosjs-key-conversions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export { Signature } from './Signature';
/** Construct the elliptic curve object based on key type */
export const constructElliptic = (type: KeyType): EC => {
if (type === KeyType.k1) {
return new EC('secp256k1') as any;
return new EC('secp256k1');
}
return new EC('p256') as any;
return new EC('p256');
};

export const generateKeyPair = (
Expand All @@ -27,9 +27,9 @@ export const generateKeyPair = (
}
let ec;
if (type === KeyType.k1) {
ec = new EC('secp256k1') as any;
ec = new EC('secp256k1');
} else {
ec = new EC('p256') as any;
ec = new EC('p256');
}
const ellipticKeyPair = ec.genKeyPair(options.ecOptions);
const publicKey = PublicKey.fromElliptic(ellipticKeyPair, type, ec);
Expand Down
1 change: 1 addition & 0 deletions src/eosjs-serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export interface Action {
name: string;
authorization: Authorization[];
data: any;
hex_data?: string;
}

/** Action with data in serialized hex form */
Expand Down
2 changes: 1 addition & 1 deletion src/eosjs-webauthn-sig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class WebAuthnSignatureProvider implements SignatureProvider {
challenge: digest.buffer,
},
});
const e = new ec('p256') as any;
const e = new ec('p256') as any; // https://github.com/indutny/elliptic/pull/232
const pubKey = e.keyFromPublic(numeric.stringToPublicKey(key).data.subarray(0, 33)).getPublic();

const fixup = (x: Uint8Array) => {
Expand Down
3 changes: 2 additions & 1 deletion src/tests/eosjs-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { JsSignatureProvider } from '../eosjs-jssig';
import { WasmAbiProvider, WasmAbi } from '../eosjs-wasmabi';
import * as path from 'path';
import * as fs from 'fs';
import { Action } from '../eosjs-serialize';

const transaction = {
expiration: '2018-09-04T18:42:49',
Expand All @@ -13,7 +14,7 @@ const transaction = {
max_net_usage_words: 0,
max_cpu_usage_ms: 0,
delay_sec: 0,
context_free_actions: [] as any,
context_free_actions: [] as Action[],
actions: [
{
account: 'testeostoken',
Expand Down
Loading