UNPKG

11.7 kBTypeScriptView Raw
1/**
2 * @module Serialize
3 */
4import { TransactionHeader } from './eosjs-api-interfaces';
5import { Abi, BlockTaposInfo } from './eosjs-rpc-interfaces';
6import { Query } from './eosjs-api-interfaces';
7/** A field in an abi */
8export interface Field {
9 /** Field name */
10 name: string;
11 /** Type name in string form */
12 typeName: string;
13 /** Type of the field */
14 type: Type;
15}
16/** Options for serialize() and deserialize() */
17export interface SerializerOptions {
18 bytesAsUint8Array?: boolean;
19}
20/** State for serialize() and deserialize() */
21export declare class SerializerState {
22 options: SerializerOptions;
23 /** Have any binary extensions been skipped? */
24 skippedBinaryExtension: boolean;
25 constructor(options?: SerializerOptions);
26}
27/**
28 * An Anyvar (non-short form) may be any of the following:
29 * * null
30 * * string
31 * * number
32 * * Caution: assumes number is int32. Use {type, value} form for other numeric types
33 * * an array of anyvar
34 * * {type, value}
35 * * type is a string matching one of the predefined types in anyvarDefs
36 * * value:
37 * * If type === 'any_object', then value is an object. The values within the object are anyvar.
38 * * If type === 'any_array', then value is an array of anyvar.
39 * * Else, value must be eosjs-compatible with the specified type (e.g. uint64 should be a string
40 * containing the value in decimal).
41 * * Other object. The values within the object are anyvar.
42 *
43 * The short form is more convenient, but it can't be converted back to binary (serialized).
44 * Wherever the anyvar would have {type, value}, it has just the value instead.
45 */
46export declare type Anyvar = null | string | number | Anyvar[] | {
47 type: string;
48 value: any;
49} | Record<string, unknown>;
50/** A type in an abi */
51export interface Type {
52 /** Type name */
53 name: string;
54 /** Type name this is an alias of, if any */
55 aliasOfName: string;
56 /** Type this is an array of, if any */
57 arrayOf: Type;
58 /** Type this is an optional of, if any */
59 optionalOf: Type;
60 /** Marks binary extension fields */
61 extensionOf?: Type;
62 /** Base name of this type, if this is a struct */
63 baseName: string;
64 /** Base of this type, if this is a struct */
65 base: Type;
66 /** Contained fields, if this is a struct */
67 fields: Field[];
68 /** Convert `data` to binary form and store in `buffer` */
69 serialize: (buffer: SerialBuffer, data: any, state?: SerializerState, allowExtensions?: boolean) => void;
70 /** Convert data in `buffer` from binary form */
71 deserialize: (buffer: SerialBuffer, state?: SerializerState, allowExtensions?: boolean) => any;
72}
73/** Structural representation of a symbol */
74export interface Symbol {
75 /** Name of the symbol, not including precision */
76 name: string;
77 /** Number of digits after the decimal point */
78 precision: number;
79}
80export interface Contract {
81 actions: Map<string, Type>;
82 types: Map<string, Type>;
83}
84export interface Authorization {
85 actor: string;
86 permission: string;
87}
88/** Action with data in structured form */
89export interface Action {
90 account: string;
91 name: string;
92 authorization: Authorization[];
93 data: any;
94 hex_data?: string;
95}
96/** Action with data in serialized hex form */
97export interface SerializedAction {
98 account: string;
99 name: string;
100 authorization: Authorization[];
101 data: string;
102}
103/** Serialize and deserialize data */
104export declare class SerialBuffer {
105 /** Amount of valid data in `array` */
106 length: number;
107 /** Data in serialized (binary) form */
108 array: Uint8Array;
109 /** Current position while reading (deserializing) */
110 readPos: number;
111 textEncoder: TextEncoder;
112 textDecoder: TextDecoder;
113 /**
114 * @param __namedParameters
115 * `array`: `null` if serializing, or binary data to deserialize
116 * `textEncoder`: `TextEncoder` instance to use. Pass in `null` if running in a browser
117 * `textDecoder`: `TextDecider` instance to use. Pass in `null` if running in a browser
118 */
119 constructor({ textEncoder, textDecoder, array }?: {
120 textEncoder?: TextEncoder;
121 textDecoder?: TextDecoder;
122 array?: Uint8Array;
123 });
124 /** Resize `array` if needed to have at least `size` bytes free */
125 reserve(size: number): void;
126 /** Is there data available to read? */
127 haveReadData(): boolean;
128 /** Restart reading from the beginning */
129 restartRead(): void;
130 /** Return data with excess storage trimmed away */
131 asUint8Array(): Uint8Array;
132 /** Append bytes */
133 pushArray(v: number[] | Uint8Array): void;
134 /** Append bytes */
135 push(...v: number[]): void;
136 /** Get a single byte */
137 get(): number;
138 /** Append bytes in `v`. Throws if `len` doesn't match `v.length` */
139 pushUint8ArrayChecked(v: Uint8Array, len: number): void;
140 /** Get `len` bytes */
141 getUint8Array(len: number): Uint8Array;
142 /** Skip `len` bytes */
143 skip(len: number): void;
144 /** Append a `uint16` */
145 pushUint16(v: number): void;
146 /** Get a `uint16` */
147 getUint16(): number;
148 /** Append a `uint32` */
149 pushUint32(v: number): void;
150 /** Get a `uint32` */
151 getUint32(): number;
152 /** Append a `uint64`. *Caution*: `number` only has 53 bits of precision */
153 pushNumberAsUint64(v: number): void;
154 /**
155 * Get a `uint64` as a `number`. *Caution*: `number` only has 53 bits of precision; some values will change.
156 * `numeric.binaryToDecimal(serialBuffer.getUint8Array(8))` recommended instead
157 */
158 getUint64AsNumber(): number;
159 /** Append a `varuint32` */
160 pushVaruint32(v: number): void;
161 /** Get a `varuint32` */
162 getVaruint32(): number;
163 /** Append a `varint32` */
164 pushVarint32(v: number): void;
165 /** Get a `varint32` */
166 getVarint32(): number;
167 /** Append a `float32` */
168 pushFloat32(v: number): void;
169 /** Get a `float32` */
170 getFloat32(): number;
171 /** Append a `float64` */
172 pushFloat64(v: number): void;
173 /** Get a `float64` */
174 getFloat64(): number;
175 /** Append a `name` */
176 pushName(s: string): void;
177 /** Get a `name` */
178 getName(): string;
179 /** Append length-prefixed binary data */
180 pushBytes(v: number[] | Uint8Array): void;
181 /** Get length-prefixed binary data */
182 getBytes(): Uint8Array;
183 /** Append a string */
184 pushString(v: string): void;
185 /** Get a string */
186 getString(): string;
187 /** Append a `symbol_code`. Unlike `symbol`, `symbol_code` doesn't include a precision. */
188 pushSymbolCode(name: string): void;
189 /** Get a `symbol_code`. Unlike `symbol`, `symbol_code` doesn't include a precision. */
190 getSymbolCode(): string;
191 /** Append a `symbol` */
192 pushSymbol({ name, precision }: {
193 name: string;
194 precision: number;
195 }): void;
196 /** Get a `symbol` */
197 getSymbol(): {
198 name: string;
199 precision: number;
200 };
201 /** Append an asset */
202 pushAsset(s: string): void;
203 /** Get an asset */
204 getAsset(): string;
205 /** Append a public key */
206 pushPublicKey(s: string): void;
207 /** Get a public key */
208 getPublicKey(): string;
209 /** Append a private key */
210 pushPrivateKey(s: string): void;
211 /** Get a private key */
212 getPrivateKey(): string;
213 /** Append a signature */
214 pushSignature(s: string): void;
215 /** Get a signature */
216 getSignature(): string;
217}
218/** Is this a supported ABI version? */
219export declare const supportedAbiVersion: (version: string) => boolean;
220/** Convert date in ISO format to `time_point` (miliseconds since epoch) */
221export declare const dateToTimePoint: (date: string) => number;
222/** Convert `time_point` (miliseconds since epoch) to date in ISO format */
223export declare const timePointToDate: (us: number) => string;
224/** Convert date in ISO format to `time_point_sec` (seconds since epoch) */
225export declare const dateToTimePointSec: (date: string) => number;
226/** Convert `time_point_sec` (seconds since epoch) to to date in ISO format */
227export declare const timePointSecToDate: (sec: number) => string;
228/** Convert date in ISO format to `block_timestamp_type` (half-seconds since a different epoch) */
229export declare const dateToBlockTimestamp: (date: string) => number;
230/** Convert `block_timestamp_type` (half-seconds since a different epoch) to to date in ISO format */
231export declare const blockTimestampToDate: (slot: number) => string;
232/** Convert `string` to `Symbol`. format: `precision,NAME`. */
233export declare const stringToSymbol: (s: string) => {
234 name: string;
235 precision: number;
236};
237/** Convert `Symbol` to `string`. format: `precision,NAME`. */
238export declare const symbolToString: ({ name, precision }: {
239 name: string;
240 precision: number;
241}) => string;
242/** Convert binary data to hex */
243export declare const arrayToHex: (data: Uint8Array) => string;
244/** Convert hex to binary data */
245export declare const hexToUint8Array: (hex: string) => Uint8Array;
246/** Create the set of types built-in to the abi format */
247export declare const createInitialTypes: () => Map<string, Type>;
248export declare const createAbiTypes: () => Map<string, Type>;
249export declare const createTransactionExtensionTypes: () => Map<string, Type>;
250export declare const createTransactionTypes: () => Map<string, Type>;
251/** Get type from `types` */
252export declare const getType: (types: Map<string, Type>, name: string) => Type;
253/**
254 * Get types from abi
255 *
256 * @param initialTypes Set of types to build on.
257 * In most cases, it's best to fill this from a fresh call to `getTypesFromAbi()`.
258 */
259export declare const getTypesFromAbi: (initialTypes: Map<string, Type>, abi?: Abi) => Map<string, Type>;
260/** TAPoS: Return transaction fields which reference `refBlock` and expire `expireSeconds` after `timestamp` */
261export declare const transactionHeader: (refBlock: BlockTaposInfo, expireSeconds: number) => TransactionHeader;
262/** Convert action data to serialized form (hex) */
263export declare const serializeActionData: (contract: Contract, account: string, name: string, data: any, textEncoder: TextEncoder, textDecoder: TextDecoder) => string;
264/** Return action in serialized form */
265export declare const serializeAction: (contract: Contract, account: string, name: string, authorization: Authorization[], data: any, textEncoder: TextEncoder, textDecoder: TextDecoder) => SerializedAction;
266/** Deserialize action data. If `data` is a `string`, then it's assumed to be in hex. */
267export declare const deserializeActionData: (contract: Contract, account: string, name: string, data: string | Uint8Array | number[], textEncoder: TextEncoder, textDecoder: TextDecoder) => any;
268/** Deserialize action. If `data` is a `string`, then it's assumed to be in hex. */
269export declare const deserializeAction: (contract: Contract, account: string, name: string, authorization: Authorization[], data: string | Uint8Array | number[], textEncoder: TextEncoder, textDecoder: TextDecoder) => Action;
270export declare const serializeAnyvar: (buffer: SerialBuffer, anyvar: Anyvar) => void;
271export declare const deserializeAnyvar: (buffer: SerialBuffer, state?: SerializerState) => any;
272export declare const deserializeAnyvarShort: (buffer: SerialBuffer) => any;
273export declare const serializeAnyObject: (buffer: SerialBuffer, obj: any) => void;
274export declare const deserializeAnyObject: (buffer: SerialBuffer, state?: SerializerState) => any;
275export declare const serializeAnyArray: (buffer: SerialBuffer, arr: Anyvar[]) => void;
276export declare const deserializeAnyArray: (buffer: SerialBuffer, state?: SerializerState) => any[];
277export declare const serializeQuery: (buffer: SerialBuffer, query: Query) => void;