This repository has been archived by the owner on Jan 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
W3.ts
1182 lines (1049 loc) · 46.3 KB
/
W3.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// typings originally from https://github.com/ethereum/web3.js/pull/897
// and https://github.com/0xProject/web3-typescript-typings
import { JsonRPCRequest, JsonRPCResponse } from './jsonrpc';
import { BigNumber } from 'bignumber.js'; // TODO change to BN
import Web3JS = require('web3');
// W3 is the only class wrapper over JS object, others are interfaces
// cannot just cast from JS, but ctor does some standard logic to resolve web3
// so we do not need cast but could just use new W3()
/** Convert number or hex string to BigNumber */
export function toBN(value: number): BigNumber {
// TODO BN.js
return new BigNumber(value);
}
// tslint:disable:member-ordering
// tslint:disable:max-line-length
/**
* Strongly-types wrapper over web3.js with additional helper methods.
*/
export class W3 {
private static _counter: number = 0;
private static _default: W3;
private _globalWeb3;
private _netId: string;
private _netNode: Promise<string>;
private _defaultAccount: string;
/**
* Default W3 instance that is used as a fallback when such an instance is not provided to a constructor.
* You must set it explicitly via W3.default setter. Use an empty `new W3()` constructor to get an instance that
* automatically resolves to a global web3 instance `window['web3']` provided by e.g. MIST/Metamask or connects
* to the default 8545 port if no global instance is present.
*/
public static get default(): W3 {
if (W3._default) {
return W3._default;
}
throw 'Default W3 instance is not set. Use W3.default setter.';
}
/**
* Set default W3 instance.
*/
public static set default(w3: W3) {
W3._default = w3;
}
/** Get Global W3 incrementing counter. Starts with zero. */
public static getNextCounter(): number {
// node is single-threaded
// same provider in two W3 instances is OK, counter will be unique accross them
let value = W3._counter;
W3._counter++;
return value;
}
/** Web3 providers. */
public static providers: W3.Providers = Web3JS.providers;
/** Current Web3 provider. */
public get currentProvider(): W3.Provider { return this.web3 ? this.web3.currentProvider : undefined; }
/** Convert number or hex string to BigNumber */
public toBigNumber(value: number | string): BigNumber {
return this.web3.toBigNumber(value);
}
/** Converts a number or number string to its HEX representation. */
public fromDecimal(value: number | string): string {
return this.web3.fromDecimal(value);
}
/**
* web3.js untyped instance created with a resolved or given in ctor provider, if any.
*/
public web3;
/** Default account for sending transactions without explicit txParams. */
public get defaultAccount(): string {
return this._defaultAccount;
}
public set defaultAccount(defaultAccount: string) {
this._defaultAccount = defaultAccount;
this.web3.defaultAccount = defaultAccount;
}
/**
* Create a default Web3 instance - resolves to a global window['web3'] injected my MIST, MetaMask, etc
* or to `localhost:8545` if not running on https.
*/
constructor()
/**
* Create a W3 instance with a given provider.
* @param provider web3.js provider.
*/
constructor(provider?: W3.Provider)
constructor(provider?: W3.Provider, defaultAccount?: string) {
let tmpWeb3;
if (typeof provider === 'undefined') {
// tslint:disable-next-line:no-string-literal
if ((typeof window !== 'undefined' && typeof window['web3'] !== 'undefined' && typeof window['web3'].currentProvider !== 'undefined')
|| this._globalWeb3) {
// tslint:disable-next-line:no-string-literal
this._globalWeb3 = window['web3'];
// tslint:disable-next-line:no-string-literal
tmpWeb3 = new Web3JS(this._globalWeb3.currentProvider);
console.log('Using an injected web3 provider.');
} else {
// set the provider you want from Web3.providers
if (typeof window === 'undefined' || window.location.protocol !== 'https:') {
tmpWeb3 = new Web3JS(new Web3JS.providers.HttpProvider('http://localhost:8545'));
console.log('Cannot find an injected web3 provider. Using HttpProvider(http://localhost:8545).');
} else {
// tslint:disable-next-line:max-line-length
console.log('Cannot find an injected web3 provider. Running on https, will not try to access localhost at 8545');
}
}
} else {
// regardless if a web3 exists, we create a new one for a specific provider
tmpWeb3 = new Web3JS(provider);
console.log('Using a provider from constructor.');
}
if (!tmpWeb3.version.api.startsWith('0.20')) {
throw 'Only web3 0.20.xx package is currently supported';
}
this.web3 = tmpWeb3;
// set and override eth's default account with a given value or use eth's one if it is set
if (defaultAccount) {
// set property, that will also set this.web3.defaultAccount
this.defaultAccount = defaultAccount;
} else if (this.web3.defaultAccount) {
// set private field directly
this._defaultAccount = this.web3.defaultAccount;
}
this.updateNetworkInfo();
}
/** Request netid string from ctor or after provider change */
private updateNetworkInfo(netid?: string): void {
if (!netid) {
this.networkId.then(nid => this._netId = nid);
} else {
this._netId = netid;
}
this._netNode = new Promise((resolve, reject) =>
this.web3.version.getNode((error, result) => error ? reject(error) : resolve(result))
);
}
/*
Below are frequently used functions that should not depend on web3 API (0.20 or 1.0).
Will update them for web3 1.0 when truffle-contract support it without sendAsync error.
*/
/** Returns a list of accounts the node controlst */
public get accounts(): Promise<string[]> {
return new Promise((resolve, reject) =>
this.web3.eth.getAccounts((error, result) => error ? reject(error) : resolve(result)));
}
/** Web3.js version API */
public get web3API(): string {
return this.web3.version.api;
}
/** True if current web3.js version is 0.20.x. */
public get isPre1API(): boolean {
return this.web3API.startsWith('0.20.');
}
/** True if current node name contains TestRPC string */
public get isTestRPC(): Promise<boolean> {
return this._netNode.then(node => { return node.includes('TestRPC'); });
}
/** Get network ID. */
public get networkId(): Promise<string> {
return new Promise((resolve, reject) =>
this.web3.version.getNetwork((error, result) => {
if (error) {
return reject(error);
}
if (result + '' !== this._netId) {
this.updateNetworkInfo(result + '');
}
return resolve(result);
})
);
}
/** Set web3 provider and update network info asynchronously. */
public setProvider(provider: W3.Provider) {
this.web3.setProvider(provider);
this.updateNetworkInfo();
}
/** Send raw JSON RPC request to the current provider. */
public sendRPC(payload: JsonRPCRequest): Promise<JsonRPCResponse> {
return new Promise((resolve, reject) => {
if (this.isPre1API) {
this.web3.currentProvider.sendAsync(payload, (e, r) => {
if (e) {
reject(e);
} else {
resolve(r);
}
});
} else {
this.web3.currentProvider.send(payload, (e, r) => {
if (e) {
reject(e);
} else {
resolve(r);
}
});
}
});
}
/** Returns the time of the last mined block in seconds. */
public get latestTime(): Promise<number> {
return new Promise((resolve, reject) => {
if (this.isPre1API) {
this.web3.eth.getBlock('latest', (err, res) => {
if (err) {
reject(err);
} else {
resolve(res.timestamp as number);
}
});
} else {
reject('web3 1.0 support is not implemented');
}
});
}
/** Returns the current block number */
public get blockNumber(): Promise<number> {
// getBlockNumber(callback: (err: Error, blockNumber: number) => void): void;
return new Promise((resolve, reject) => {
if (this.isPre1API) {
this.web3.eth.getBlockNumber((err, res) => {
if (err) {
reject(err);
} else {
resolve(res as number);
}
});
} else {
reject('web3 1.0 support is not implemented');
}
});
}
/** Async unlock while web3.js only has sync version. This function uses `personal_unlockAccount` RPC message that is not available in some web3 providers. */
public async unlockAccount(address: string, password: string, duration?: number): Promise<boolean> {
const id = 'W3:' + W3.getNextCounter();
return this.sendRPC({
jsonrpc: '2.0',
method: 'personal_unlockAccount',
params: [address, password, duration || 10],
id: id,
}).then(async r => {
if (r.error) {
return false;
}
return <boolean>r.result;
});
}
/** Get account balance */
public async getBalance(address: string): Promise<BigNumber> {
return new Promise<BigNumber>((resolve, reject) => {
this.web3.eth.getBalance(address, (error, result) => {
if (error) {
reject(error);
} else {
resolve(result as BigNumber);
}
});
});
}
/** Sign using eth.sign but with prefix as personal_sign */
public async ethSignRaw(hex: string, account: string): Promise<string> {
// NOTE geth already adds the prefix to eth.sign, no need to do so manually
// hex = hex.replace('0x', '');
// let prefix = '\x19Ethereum Signed Message:\n' + (hex.length / 2);
// let message = this.web3.toHex(prefix) + hex;
return new Promise<string>((resolve, reject) => {
this.web3.eth.sign(account, hex, (err, res) => {
if (err) {
reject(err);
}
resolve(res);
});
});
}
/** Sign a message */
public async sign(message: string, account: string, password?: string): Promise<string> {
message = W3.toHex(message);
return this.signRaw(message, account, password);
}
/** Message already as hex */
public async signRaw(message: any, account: string, password?: string): Promise<string> {
const id = 'W3:' + W3.getNextCounter();
console.log('signRaw MESSAGE', message);
return this.sendRPC({
jsonrpc: '2.0',
method: 'personal_sign',
params: password ? [message, account, password] : [message, account],
id: id,
}).then(async r => {
if (r.error) {
console.log('ERROR:', r.error);
throw new Error(r.error.message);
}
return <string>r.result;
});
}
/** True if current web3 provider is from MetaMask. */
public get isMetaMask() {
try {
return this.web3.currentProvider.isMetaMask ? true : false;
} catch {
return false;
}
}
/**
* Get the numbers of transactions sent from this address.
* @param account The address to get the numbers of transactions from.
* @param defaultBlock (optional) If you pass this parameter it will not use the default block set with.
*/
public async getTransactionCount(account?: string, defaultBlock?: number | string): Promise<number> {
account = account || this.defaultAccount;
if (!account) {
throw new Error('No default account set to call getTransactionCount without a given account address');
}
return new Promise<number>((resolve, reject) => {
this.web3.eth.getTransactionCount(account, defaultBlock, (e, r) => {
if (e) {
reject(e);
} else {
resolve(r);
}
});
});
}
/**
* Sends a raw signed transaction and returns tx hash. Use waitTransactionReceipt method on w3 or a contract to get a tx receipt.
* @param to Target contract address or zero address (W3.zeroAddress) to deploy a new contract.
* @param privateKey Private key hex string prefixed with 0x.
* @param data Payload data.
* @param txParams Tx parameters.
* @param nonce Nonce override if needed to replace a pending transaction.
*/
public async sendSignedTransaction(to: string, privateKey: string, data?: string, txParams?: W3.TX.TxParams, nonce?: number): Promise<string> {
if (!this.isPre1API) {
throw new Error('Web3.js v.1.0 is not supported yet');
}
if (!to || !W3.isValidAddress(to)) {
throw new Error('To address is not set in sendSignedTransaction');
}
if (!privateKey) {
throw new Error('PrivateKey is not set in sendSignedTransaction');
}
txParams = txParams || W3.TX.txParamsDefaultSend(this.defaultAccount);
nonce = nonce || await this.getTransactionCount(txParams.from);
let EthereumTx = W3.TX.getEthereumjsTx();
let pb = W3.EthUtils.toBuffer(privateKey);
let nid = +(this._netId || await this.networkId);
const signedTxParams = {
nonce: W3.toHex(nonce),
gasPrice: W3.toHex(txParams.gasPrice),
gasLimit: W3.toHex(txParams.gas),
to: to,
value: W3.toHex(txParams.value),
data: data,
chainId: nid
};
if (W3.zeroAddress === to) {
delete signedTxParams.to;
}
const tx = new EthereumTx(signedTxParams);
tx.sign(pb);
const serializedTx = tx.serialize();
const raw = `0x${serializedTx.toString('hex')}`;
return new Promise<string>((resolve, reject) => {
this.web3.eth.sendRawTransaction(raw, (e, r) => {
if (e) {
reject(e);
} else {
resolve(r);
}
});
});
}
/**
* Returns the receipt of a transaction by transaction hash. Retries for up to 240 seconds.
* @param hashString The transaction hash.
* @param timeoutSeconds Timeout in seconds, must be above 240 or ignored.
*/
public async waitTransactionReceipt(hashString: string, timeoutSeconds?: number): Promise<W3.TransactionReceipt> {
return new Promise<W3.TransactionReceipt>((accept, reject) => {
var timeout = (timeoutSeconds && timeoutSeconds > 240) ? timeoutSeconds * 1000 : 240000;
var start = new Date().getTime();
let makeAttempt = () => {
this.web3.eth.getTransactionReceipt(hashString, (err, receipt) => {
if (err) { return reject(err); }
if (receipt != null) {
return accept(receipt);
}
if (timeout > 0 && new Date().getTime() - start > timeout) {
return reject(new Error('Transaction ' + hashString + ' wasn\'t processed in ' + (timeout / 1000) + ' seconds!'));
}
setTimeout(makeAttempt, 1000);
});
};
makeAttempt();
});
}
}
export namespace W3 {
/** Type alias for Ethereum address. */
export type address = string;
/** Type alias for bytes string. */
export type bytes = string;
/** Hex zero address. */
export const zeroAddress: string = '0x0000000000000000000000000000000000000000';
/** Check if Ethereum address is in valid format. */
export function isValidAddress(addr: address): boolean {
return W3.EthUtils.isValidAddress(addr);
}
/** Utf8 package. */
export let Utf8: any = require('utf8');
/**
* Convert value to hex with optional left padding. If a string is already a hex it will be converted to lower case.
* @param value Value to convert to hex
* @param size Size of number in bits (8 for int8, 16 for uint16, etc)
*/
export function toHex(value: number | string | BigNumber, stripPrefix?: boolean, size?: number): string {
const HEX_CHAR_SIZE = 4;
if (typeof value === 'string') {
if (!value.startsWith('0x')) {
// non-hex string
return W3.utf8ToHex(value, stripPrefix);
} else {
let lowerCase = value.toLowerCase();
return stripPrefix ? lowerCase.slice(2) : lowerCase;
}
}
// numbers, big numbers, and hex strings
if (size) {
// tslint:disable-next-line:no-bitwise
let hexNoPrefix = EthUtils.bufferToHex(EthUtils.toBuffer((value as any) >>> 0)).slice(2);
// tslint:disable-next-line:no-bitwise
let leftPadded = W3.leftPad(hexNoPrefix, size / HEX_CHAR_SIZE, value < 0 ? 'F' : '0');
return (stripPrefix ? leftPadded : '0x' + leftPadded);
} else {
// tslint:disable-next-line:no-bitwise
let hexWithPrefix = EthUtils.bufferToHex(EthUtils.toBuffer((value as any) >>> 0));
// tslint:disable-next-line:no-bitwise
return (stripPrefix ? hexWithPrefix.slice(2) : hexWithPrefix);
}
}
/** String left pad. Same as the notorious left-pad package as a single function. */
export function leftPad(str: string, len: number, ch: any) {
// the notorious 12-lines npm package
str = String(str);
var i = -1;
if (!ch && ch !== 0) { ch = ' '; }
len = len - str.length;
while (++i < len) {
str = ch + str;
}
return str;
}
/** Utf8 to hex convertor. */
export function utf8ToHex(str: string, stripPrefix?: boolean): string {
// this is from web3 1.0
str = W3.Utf8.encode(str);
let hex = '';
// remove \u0000 padding from either side
str = str.replace(/^(?:\u0000)*/, '');
str = str.split('').reverse().join('');
str = str.replace(/^(?:\u0000)*/, '');
str = str.split('').reverse().join('');
for (let i = 0; i < str.length; i++) {
let code = str.charCodeAt(i);
// if (code !== 0) {
let n = code.toString(16);
hex += n.length < 2 ? '0' + n : n;
// }
}
return (stripPrefix ? hex : '0x' + hex);
}
/** Ethereumjs-util package. */
export let EthUtils: W3.EthUtils = require('ethereumjs-util');
/** Creates SHA-3 hash of the input. */
export function sha3(a: Buffer | Array<any> | string | number, bits?: number): string {
return EthUtils.bufferToHex(EthUtils.sha3(a, bits));
}
/** Creates SHA256 hash of the input. */
export function sha256(a: Buffer | Array<any> | string | number): string {
return EthUtils.bufferToHex(EthUtils.sha256(a));
}
/** ECDSA sign. */
export function sign(message: any, privateKey: string): bytes {
let mb = W3.EthUtils.toBuffer(message);
let pb = W3.EthUtils.toBuffer(privateKey);
let personalHash = W3.EthUtils.hashPersonalMessage(mb);
let signature = W3.EthUtils.ecsign(personalHash, pb);
let rpcSignature = W3.EthUtils.toRpcSig(signature.v, signature.r, signature.s);
return rpcSignature;
}
/** ECDSA public key recovery from signature. */
export function ecrecover(message: string, signature: string): address {
let mb = W3.EthUtils.toBuffer(message);
let sigObject = W3.EthUtils.fromRpcSig(signature);
let personalHash = W3.EthUtils.hashPersonalMessage(mb);
let recoveredPubKeyBuffer = W3.EthUtils.ecrecover(personalHash, sigObject.v, sigObject.r, sigObject.s);
let recoveredAddress = W3.EthUtils.pubToAddress(recoveredPubKeyBuffer);
let addr = W3.EthUtils.bufferToHex(recoveredAddress);
return addr;
}
let _keythereum: any;
/**
* Get Keythereum instance. https://github.com/ethereumjs/keythereum
*/
export function getKeythereum() {
if (_keythereum) {
return _keythereum;
}
// tslint:disable-next-line:no-string-literal
if (typeof window !== 'undefined' && typeof window['keythereum'] !== 'undefined') {
// tslint:disable-next-line:no-string-literal
_keythereum = window['keythereum'];
} else {
_keythereum = require('keythereum');
}
return _keythereum;
}
/** Truffle Contract */
export namespace TX {
/** Standard transaction parameters. */
export interface TxParams {
from: address;
gas: number | BigNumber;
gasPrice: number | BigNumber;
value: number | BigNumber;
}
export type ContractDataType = BigNumber | number | string | boolean | BigNumber[] | number[] | string[];
export interface TransactionResult {
/** Transaction hash. */
tx: string;
receipt: TransactionReceipt;
/** This array has decoded events, while reseipt.logs has raw logs when returned from TC transaction */
logs: Log[];
}
/** 4500000 gas @ 2 Gwei */
export function txParamsDefaultDeploy(from: address, gas?: number, gasPrice?: number): TxParams {
if (gasPrice && gasPrice > 40000000000) {
throw new Error(`Given gasPrice ${gasPrice} is above 40Gwei (our default is 2 Gwei, common value for fast transactions is 20Gwei), this could be costly. Construct txParams manually if you know what you are doing.`);
}
if (gas && gas > 4500000) {
throw new Error(`Given gas ${gas} is too large and could exceed block size.`);
}
return {
from: from,
gas: gas || 4500000,
gasPrice: gasPrice || 2000000000, // 2 Gwei, not 20
value: 0
};
}
/** 50000 gas @ 2 Gwei */
export function txParamsDefaultSend(from: address, gas?: number, gasPrice?: number): TxParams {
if (gasPrice && gasPrice > 40000000000) {
throw new Error(`Given gasPrice ${gasPrice} is above 40Gwei (our default is 2 Gwei, common value for fast transactions is 20Gwei), this could be costly. Construct txParams manually if you know what you are doing.`);
}
if (gas && gas > 500000) {
throw new Error(`Given gas ${gas} is too large for a normal transaction (>500k). Use txParamsDefaultDeploy or construct params manually.`);
}
return {
from: from,
gas: gas || 50000,
gasPrice: gasPrice || 2000000000, // 2 Gwei, not 20
value: 0
};
}
let _ethereumjsTx: any;
export function getEthereumjsTx() {
if (_ethereumjsTx) {
return _ethereumjsTx;
}
// tslint:disable-next-line:no-string-literal
if (typeof window !== 'undefined' && typeof window['ethereumjs-tx'] !== 'undefined') {
// tslint:disable-next-line:no-string-literal
_ethereumjsTx = window['ethereumjs-tx'];
} else {
_ethereumjsTx = require('ethereumjs-tx');
}
return _ethereumjsTx;
}
}
export interface Provider {
sendAsync(
payload: JsonRPCRequest,
callback: (err: Error, result: JsonRPCResponse) => void,
): void;
}
// export interface Provider {
// send(payload: JsonRPCRequest, callback: (e: Error, val: JsonRPCResponse) => void);
// }
export interface WebsocketProvider extends Provider { }
export interface HttpProvider extends Provider { }
export interface IpcProvider extends Provider { }
export interface Providers {
WebsocketProvider: new (host: string, timeout?: number) => WebsocketProvider;
HttpProvider: new (host: string, timeout?: number) => HttpProvider;
IpcProvider: new (path: string, net: any) => IpcProvider;
}
// tslint:disable-next-line:max-line-length
export type Unit = 'kwei' | 'femtoether' | 'babbage' | 'mwei' | 'picoether' | 'lovelace' | 'qwei' | 'nanoether' | 'shannon' | 'microether' | 'szabo' | 'nano' | 'micro' | 'milliether' | 'finney' | 'milli' | 'ether' | 'kether' | 'grand' | 'mether' | 'gether' | 'tether';
export type BlockType = 'latest' | 'pending' | 'genesis' | number;
export interface BatchRequest {
add(request: Request): void;
execute(): void;
}
export interface Iban { }
export interface Utils {
BN: BigNumber; // TODO only static-definition
isBN(obj: any): boolean;
isBigNumber(obj: any): boolean;
isAddress(obj: any): boolean;
isHex(obj: any): boolean;
asciiToHex(val: string): string;
hexToAscii(val: string): string;
bytesToHex(val: number[]): string;
numberToHex(val: number | BigNumber): string;
checkAddressChecksum(address: string): boolean;
fromAscii(val: string): string;
fromDecimal(val: string | number | BigNumber): string;
fromUtf8(val: string): string;
fromWei(val: string | number | BigNumber, unit: Unit): string | BigNumber;
hexToBytes(val: string): number[];
hexToNumber(val: string | number | BigNumber): number;
hexToNumberString(val: string | number | BigNumber): string;
hexToString(val: string): string;
hexToUtf8(val: string): string;
keccak256(val: string): string;
leftPad(str: string, chars: number, sign: string): string;
padLeft(str: string, chars: number, sign: string): string;
rightPad(str: string, chars: number, sign: string): string;
padRight(str: string, chars: number, sign: string): string;
sha3(val: string, val2?: string, val3?: string, val4?: string, val5?: string): string;
soliditySha3(val: string): string;
randomHex(bytes: number): string;
stringToHex(val: string): string;
toAscii(hex: string): string;
toBN(obj: any): BigNumber;
toChecksumAddress(val: string): string;
toDecimal(val: any): number;
toHex(val: any): string;
toUtf8(val: any): string;
toWei(val: string | number | BigNumber, unit: Unit): string | BigNumber;
// tslint:disable-next-line:member-ordering
unitMap: any;
}
/**
* https://github.com/ethereumjs/ethereumjs-util/blob/master/docs/index.md
*/
export interface EthUtils {
/** BN.js */
BN: BigNumber;
/** Adds "0x" to a given String if it does not already start with "0x". */
addHexPrefix(str: string): string;
/** Converts a Buffer or Array to JSON. */
baToJSON(ba: Buffer | Array<any>): any;
bufferToHex(buf: Buffer): string;
bufferToInt(buf: Buffer): number;
ecrecover(msgHash: Buffer, v: number, r: Buffer, s: Buffer): Buffer;
ecsign(msgHash: Buffer, privateKey: Buffer): any;
fromRpcSig(sig: string): any;
fromSigned(num: Buffer): any; // TODO BN
generateAddress(from: Buffer, nonce: Buffer): Buffer;
hashPersonalMessage(message: Buffer): Buffer;
importPublic(publicKey: Buffer): Buffer;
isValidAddress(address: string): boolean;
isValidChecksumAddress(address: Buffer): boolean;
isValidPrivate(privateKey: Buffer): boolean;
isValidPublic(privateKey: Buffer, sanitize: boolean): any;
isValidSignature(v: number, r: Buffer, s: Buffer, homestead?: boolean): any;
privateToAddress(privateKey: Buffer): Buffer;
pubToAddress(privateKey: Buffer, sanitize?: boolean): Buffer;
sha256(a: Buffer | Array<any> | string | number): Buffer;
/** Keccak[bits] */
sha3(a: Buffer | Array<any> | string | number, bits?: number): Buffer;
SHA3_NULL: Buffer;
SHA3_NULL_S: string;
toBuffer(v: any): Buffer;
toChecksumAddress(address: string): string;
toRpcSig(v: number, r: Buffer, s: Buffer): string;
privateToPublic(privateKey: Buffer): Buffer;
zeros(bytes: number): Buffer;
}
export type Callback<T> = (error: Error, result: T) => void;
export type ABIDataTypes = 'uint256' | 'boolean' | 'string' | 'bytes' | string; // TODO complete list
export interface ABIDefinition {
constant?: boolean;
payable?: boolean;
anonymous?: boolean;
inputs?: Array<{ name: string, type: ABIDataTypes, indexed?: boolean }>;
name?: string;
outputs?: Array<{ name: string, type: ABIDataTypes }>;
type: 'function' | 'constructor' | 'event' | 'fallback';
}
export interface CompileResult {
code: string;
info: {
source: string;
language: string;
languageVersion: string;
compilerVersion: string;
abiDefinition: Array<ABIDefinition>;
};
userDoc: { methods: object };
developerDoc: { methods: object };
}
export interface Transaction {
hash: string;
nonce: number;
blockHash: string;
blockNumber: number;
transactionIndex: number;
from: string;
to: string;
value: string;
gasPrice: string;
gas: number;
input: string;
v?: string;
r?: string;
s?: string;
}
export interface EventLog {
event: string;
address: string;
returnValues: object;
logIndex: number;
transactionIndex: number;
transactionHash: string;
blockHash: string;
blockNumber: number;
raw?: { data: string, topics: any[] };
}
export interface TransactionReceipt {
transactionHash: string;
transactionIndex: number;
blockHash: string;
blockNumber: number;
from: string;
to: string;
contractAddress: string;
cumulativeGasUsed: number;
gasUsed: number;
logs?: Log[];
events?: {
[eventName: string]: EventLog
};
}
export interface BlockHeader {
number: number;
hash: string;
parentHash: string;
nonce: string;
sha3Uncles: string;
logsBloom: string;
transactionRoot: string;
stateRoot: string;
receiptRoot: string;
miner: string;
extraData: string;
gasLimit: number;
gasUsed: number;
timestamp: number;
}
export interface Block extends BlockHeader {
transactions: Array<Transaction>;
size: number;
difficulty: number;
totalDifficulty: number;
uncles: Array<string>;
}
export interface Logs {
fromBlock?: number;
address?: string;
topics?: Array<string | string[]>;
}
/** Transaction log entry. */
export interface Log {
/** true when the log was removed, due to a chain reorganization. false if its a valid log. */
removed?: boolean;
logIndex: number;
transactionIndex: number;
transactionHash: string;
blockHash: string;
blockNumber: number;
address: string;
data?: string;
topics?: Array<string>;
/** Truffle-contract returns this as 'mined' */
type?: string;
/** Event name decoded by Truffle-contract */
event?: string;
/** Args passed to a Truffle-contract method */
args?: any;
}
export interface Subscribe<T> {
subscription: {
id: string;
subscribe(callback?: Callback<Subscribe<T>>): Subscribe<T>;
unsubscribe(callback?: Callback<boolean>): void | boolean;
// tslint:disable-next-line:member-ordering
arguments: object;
};
/* on(type: "data" , handler:(data:Transaction)=>void): void
on(type: "changed" , handler:(data:Logs)=>void): void
on(type: "error" , handler:(data:Error)=>void): void
on(type: "block" , handler:(data:BlockHeader)=>void): void
*/
on(type: 'data', handler: (data: T) => void): void;
on(type: 'changed', handler: (data: T) => void): void;
on(type: 'error', handler: (data: Error) => void): void;
}
export interface Account {
address: string;
privateKey: string;
publicKey: string;
}
export interface PrivateKey {
address: string;
Crypto: {
cipher: string,
ciphertext: string,
cipherparams: {
iv: string
},
kdf: string,
kdfparams: {
dklen: number,
n: number,
p: number,
r: number,
salt: string
},
mac: string
};
id: string;
version: number;
}
export interface Signature {
message: string;
hash: string;
r: string;
s: string;
v: string;
}
export interface Tx {
nonce?: string | number;
chainId?: string | number;
from?: string;
to?: string;
data?: string;
value?: string | number;
gas?: string | number;
gasPrice?: string | number;
}
export interface ContractOptions {
address: string;
jsonInterface: ABIDefinition[];
from?: string;
gas?: string | number | BigNumber;
gasPrice?: number;
data?: string;
}
export type PromiEventType = 'transactionHash' | 'receipt' | 'confirmation' | 'error';
export interface PromiEvent<T> extends Promise<T> {
once(type: 'transactionHash', handler: (receipt: string) => void): PromiEvent<T>;
once(type: 'receipt', handler: (receipt: TransactionReceipt) => void): PromiEvent<T>;
once(type: 'confirmation', handler: (confNumber: number, receipt: TransactionReceipt) => void): PromiEvent<T>;
once(type: 'error', handler: (error: Error) => void): PromiEvent<T>;
// tslint:disable-next-line:max-line-length
once(type: 'error' | 'confirmation' | 'receipt' | 'transactionHash', handler: (error: Error | TransactionReceipt | string) => void): PromiEvent<T>;
on(type: 'transactionHash', handler: (receipt: string) => void): PromiEvent<T>;
on(type: 'receipt', handler: (receipt: TransactionReceipt) => void): PromiEvent<T>;
on(type: 'confirmation', handler: (confNumber: number, receipt: TransactionReceipt) => void): PromiEvent<T>;
on(type: 'error', handler: (error: Error) => void): PromiEvent<T>;
// tslint:disable-next-line:max-line-length
on(type: 'error' | 'confirmation' | 'receipt' | 'transactionHash', handler: (error: Error | TransactionReceipt | string) => void): PromiEvent<T>;
}
export interface EventEmitter {
on(type: 'data', handler: (event: EventLog) => void): EventEmitter;
on(type: 'changed', handler: (receipt: EventLog) => void): EventEmitter;
on(type: 'error', handler: (error: Error) => void): EventEmitter;
// tslint:disable-next-line:max-line-length