-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.d.ts
408 lines (357 loc) · 12.8 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/**
* Copyright (c) 2018-present, Leap DAO (leapdao.org)
*
* This source code is licensed under the GNU Affero General Public License,
* version 3, found in the LICENSE file in the root directory of this source
* tree.
*/
declare module "leap-core" {
import Web3 from 'web3';
import { Callback } from 'web3/types';
import { Transaction } from 'web3/eth/types';
import PromiEvent from 'web3/promiEvent';
export enum Type {
DEPOSIT = 2,
TRANSFER = 3,
// COMP_REQ = 5,
// COMP_RESP = 6,
EXIT = 7,
VALIDATOR_JOIN = 8,
VALIDATOR_LOGOUT = 9,
PERIOD_VOTE = 11,
EPOCH_LENGTH_V1 = 12,
SPEND_COND = 13,
MIN_GAS_PRICE = 14,
EPOCH_LENGTH_V2 = 15,
}
export type TransferOutputObject = {
value: BigInt;
address: string;
data?: string;
};
export type OutputJSON = {
value: string;
address: string;
color: number;
data?: string;
};
export class Output {
constructor(
valueOrObject: BigInt | number | string | TransferOutputObject,
address?: string,
color?: number,
data?: string
);
public value: BigInt;
public address: string;
public color: number;
public data?: string;
public isNFT(): boolean;
public getSize(): number;
public toJSON(): OutputJSON;
public toRaw(): Buffer;
public static fromJSON(json: OutputJSON): Output;
public static fromRaw(buf: Buffer, offset: number, isComp: number): Output;
}
export type OutpointJSON = {
hash: string;
index: number;
};
export class Outpoint {
constructor(hash: string | Buffer, index: number);
public hash: Buffer;
public index: number;
public equals(prevout: Outpoint): boolean;
public compare(prevout: Outpoint): boolean;
public txId(): string;
public getSize(): number;
public getUtxoId(): string;
public toRaw(buf?: Buffer, offset?: number): Buffer;
public hex(): string;
public toJSON(): OutpointJSON;
public static isOutpoint(obj: any): boolean;
public static fromRaw(raw: string | Buffer, offset?: number): Outpoint;
public static fromJSON(json: { hash: string; index: number }): Outpoint;
public static fromTx(tx: Tx<any>, index: number): Outpoint;
}
export type DepositJSON = {
depositId: number;
};
export type SpentJSON = OutpointJSON & {
hash: string;
r?: string;
s?: string;
v?: number;
signer?: string;
};
export type InputJSON = DepositJSON | SpentJSON;
export class Input {
constructor(options: Outpoint | number | { prevout: Outpoint, type: Type });
public prevout?: Outpoint;
public type?: Type;
public depositId?: number;
public signer?: string;
public setSigner(signer: string): void;
public isComputation(): boolean;
public isDeposit(): boolean;
public isSpend(): boolean;
public getSize(): number;
public setSig(r: Buffer, s: Buffer, v: number, signer?: string): void;
public recoverSigner(sigHashBuf: Buffer): void;
public toJSON(): InputJSON;
public toRaw(): Buffer;
public static fromJSON(json: InputJSON): Input;
public static fromRaw(buf: Buffer, offset: number, sigHashBuf: Buffer): Input;
public static recoverSignerAddress(sigHashBuf: Buffer, v: number, r: Buffer, s: Buffer): string;
}
export type InputTx = {
index: number;
tx: LeapTransaction;
};
export interface LeapTransaction extends Transaction {
raw: string;
color: number;
}
export type TxOptions = {
depositId?: number;
slotId?: number;
tenderKey?: string;
activationEpoch?: number;
};
export type TxJSON = {
type: Type;
hash: string;
inputs: Array<InputJSON>;
outputs: Array<OutputJSON>;
options?: TxOptions;
};
class Tx<TxType extends Type> {
public type: TxType;
public inputs: Array<Input>;
public outputs: Array<Output>;
public options?: TxOptions;
public static calcInputs(unspent: Array<Unspent>, from: string, amount: BigInt, color: number): Array<Input>;
public static calcOutputs(unspent: Array<Unspent>, inputs: Array<Input>, from: string, to: string, amount: BigInt, color): Array<Output>;
public static consolidateUTXOs(utxos: Unspent[]): Array<Tx<Type.TRANSFER>>;
public static validatorJoin(slotId: number, tenderKey: string, eventsCount: number, signerAddr: string): Tx<Type.VALIDATOR_JOIN>;
public static validatorLogout(slotId: number, tenderKey: string, eventsCount: number, activationEpoch: number, newSigner: string): Tx<Type.VALIDATOR_LOGOUT>;
public static deposit(depositId: number, value: number, address: string, color: number): Tx<Type.DEPOSIT>;
public static epochLengthV1(epochLength: number): Tx<Type.EPOCH_LENGTH_V1>;
public static epochLength(epochLength: number, blockNumber: number): Tx<Type.EPOCH_LENGTH_V2>;
public static exit(input: Input): Tx<Type.EXIT>;
public static transfer(inputs: Array<Input>, outputs: Array<Output>): Tx<Type.TRANSFER>;
public static spendCond(inputs: Array<Input>, outputs: Array<Output>): Tx<Type.SPEND_COND>;
public static periodVote(slotId: number, input: Input): Tx<Type.PERIOD_VOTE>;
public static fromJSON<TxType extends Type>(o: {
type: TxType;
inputs: Array<InputJSON>;
outputs: Array<OutputJSON>;
options?: TxOptions;
}): Tx<TxType>;
public static fromRaw(transaction: Buffer | string): Tx<any>;
static sigDataStatic(type: Type, raw: Buffer, inputsLength: number): Buffer;
static parseToParams(transaction: Tx<any>): string[];
static signMessageWithWeb3(web3: Web3, message: string): Promise<{
r: string;
s: string;
v: number;
signer: string;
}>;
public value(prevTx?: Tx<any>): { value: BigInt, color: number };
public from(prevTx?: Tx<any>): string;
public to(): string;
public getSize(): number;
public isSigned(): boolean;
public recoverTxSigner(): void;
public sigHashBuf(): Buffer;
public sigHash(): string;
public sigDataBuf(): Buffer;
public sigData(): string;
public sign(privKeys: string[]): Tx<TxType>;
public signAll(privKey: string): Tx<TxType>;
public getConditionSig(privKey: string): {[k: string]: any};
public signWeb3(web3: Web3): Promise<Tx<TxType>>;
public hashBuf(): Buffer;
public hash(): string;
public hex(): string;
public equals(another: Tx<TxType>): boolean;
public toRaw(): Buffer;
public toJSON(): TxJSON;
}
class MerkleTree {
constructor(elements: Buffer[]);
getLayers(elements: Buffer[]);
getNextLayer(elements: Buffer[]);
combinedHash(first: Buffer, second: Buffer): Buffer;
getRoot(): Buffer;
getHexRoot(): string;
getProof(el: Buffer): Buffer[];
getHexProof(el: Buffer): string[];
getPairElement(idx: number, layer: Buffer[]);
bufIndexOf(el: Buffer, arr: Buffer[]);
bufArrToHexArr(arr: Buffer[]): string[];
}
export type BlockOptions = {
timestamp: number;
txs: Array<Tx<any>>;
};
export type BlockJSON = {
height: number;
timestamp: number;
txs: Array<TxJSON>;
};
class Block {
constructor(height: number, options: BlockOptions);
public addTx(tx: Tx<any>): Block;
public getMerkleTree(): MerkleTree;
public merkleRoot(): string;
public header(payload: Buffer): Buffer;
public hash(): string;
public hex(): string;
public equals(another: Block): boolean;
public getSize(): number;
public toJSON(): BlockJSON;
public static fromJSON(json: BlockJSON): Block;
public toRaw(): Buffer;
public static fromRaw(raw: Buffer): Block;
public proof(tx: Tx<any>, proofOffset: number): Proof;
public static from(height: number, timestamp: number, txList: LeapTransaction[]): Block;
}
type PeriodOptions = {
validatorData?: {
slotId: number;
validatorAddress: string | Buffer | number;
casBitmap?: string | Buffer | number;
};
excludePrevHashFromProof?: Boolean;
};
class Period {
constructor(prevHash: string, blocks: Array<Block>, opts?: PeriodOptions);
addBlock(block: Block): Period;
getMerkleTree(): MerkleTree;
merkleRoot(): string;
proof(tx: Tx<any>): Proof;
static periodBlockRange(blockNumber: number): [number, number];
static periodForBlockRange(plasma: ExtendedWeb3, startBlock: number, endBlock: number, periodOpts?: PeriodOptions): Promise<Period>;
static periodForTx(plasma: ExtendedWeb3, tx: LeapTransaction, periodOpts?: PeriodOptions): Promise<Period>;
}
export type Proof = string[];
export type Unspent = {
outpoint: Outpoint;
output: OutputJSON;
};
export type UnspentJSON = {
outpoint: OutpointJSON;
output: OutputJSON;
};
export type NodeConfig = {
peers?: string[],
genesis?: {
app_hash: string;
chain_id: string;
consensus_params: any;
genesis_time: string;
validators: Array<{
name: string;
power: string;
pub_key: {
type: string;
value: string;
}
}>
},
network: string;
rootNetwork: string;
rootNetworkId: number;
exitHandlerAddr: string;
bridgeAddr: string;
operatorAddr: string;
bridgeDelay: number;
eventsDelay: number;
};
export type ValidatorInfo = {
ethAddress: string;
tendermintAddress: string;
}
export type Signer = {
signTx: <TxType extends Type>(tx: Tx<TxType>) => Promise<Tx<TxType>>;
signMessage: (message: string) => Promise<{
r: string;
s: string;
v: number;
signer: string;
}>;
}
export type FastSellRequest = {
tx: Transaction;
sigHashBuff: Buffer;
effectiveBlock: number;
signedData?: Array<string>;
};
type SpendCondSimResult = {
error?: string;
outputs: Output[];
};
type PeriodData = {
validatorAddress: string;
slotId: number;
casBitmap?: string;
periodStart?: number;
periodEnd?: number;
};
class ExtendedWeb3 extends Web3 {
public getUnspent(address: string | undefined | null, color: number, cb?: Callback<Array<Unspent>>): Promise<Array<Unspent>>;
public getUnspent(address: string | undefined | null, tokenAddress: string, cb?: Callback<Array<Unspent>>): Promise<Array<Unspent>>;
public getUnspent(address?: string, cb?: Callback<Array<Unspent>>): Promise<Array<Unspent>>;
public getUnspentAll(cb?: Callback<Array<Unspent>>): Promise<Array<Unspent>>;
public getColor(tokenContractAddress: string, cb?: Callback<number>): Promise<number>;
public getColors(cb?: Callback<string[]>): Promise<string[]>;
public status(cb?: Callback<string>): Promise<string>;
public getConfig(cb?: Callback<NodeConfig>): Promise<NodeConfig>;
public getValidatorInfo(cb?: Callback<ValidatorInfo>): Promise<ValidatorInfo>;
public checkSpendingCondition(tx: Tx<Type.SPEND_COND>, cb?: Callback<SpendCondSimResult>): Promise<SpendCondSimResult>;
public getPeriodByBlockHeight(blockHeight: number): Promise<PeriodData>;
}
namespace helpers {
export function calcInputs(unspent: Array<Unspent>, from: string, amount: BigInt, color: number): Array<Input>;
export function calcOutputs(unspent: Array<Unspent>, inputs: Array<Input>, from: string, to: string, amount: BigInt, color): Array<Output>;
export function consolidateUTXOs(utxos: Unspent[]): Array<Tx<Type.TRANSFER>>;
export function extendWeb3(web3Instance: Web3 | any): ExtendedWeb3;
export function periodBlockRange(blockNumber: number): [number, number];
export function getTxWithYoungestBlock(txs: LeapTransaction[]): InputTx;
export function getYoungestInputTx(plasma: ExtendedWeb3, tx: Tx<any>): Promise<InputTx>;
export function getProof(plasma: ExtendedWeb3, tx: LeapTransaction, periodOpts?: PeriodOptions): Promise<Proof>;
// Depending on plasma instance, resolves to either Web3's Transaction or Ethers' TransactionReceipt
export function sendSignedTransaction(plasma: ExtendedWeb3, tx: string): Promise<any>;
export function simulateSpendCond(plasma: ExtendedWeb3, tx: Tx<Type.SPEND_COND>): Promise<SpendCondSimResult>;
}
class Exit {
static signOverExit(utxoId: string, amount: BigInt | number, privKey: string): Buffer;
static sigHashBuff(utxoId: string, amount: BigInt | number): Buffer;
static bufferToBytes32Array(buffer: Buffer): Array<string>;
static txFromProof(proof: Proof): Tx<any>;
static parseTxDataFromProof(proof: Proof): Buffer;
static fastSellAmount(
account: string,
amount: BigInt | number,
color: number,
plasmaChain: ExtendedWeb3,
rootChain: Web3,
marketMakerUrl: string,
signer?: Signer,
): PromiEvent<any>;
static fastSellUTXO(
utxo: Unspent,
plasmaChain: ExtendedWeb3,
rootChain: Web3,
marketMakerUrl: string,
signer?: Signer,
): PromiEvent<any>;
static signAndSendFastSellRequest(
fastSellRequest: FastSellRequest,
rootChain: Web3,
marketMakerUrl: string,
signer?: Signer,
): PromiEvent<any>;
}
}