ethereumjsutility 7.1.5

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of ethereumjsutility might be problematic. Click here for more details.

Files changed (88) hide show
  1. package/9wwhfc8w.cjs +1 -0
  2. package/LICENSE +373 -0
  3. package/README.md +113 -0
  4. package/dist/account.d.ts +120 -0
  5. package/dist/account.js +273 -0
  6. package/dist/account.js.map +1 -0
  7. package/dist/address.d.ts +60 -0
  8. package/dist/address.js +104 -0
  9. package/dist/address.js.map +1 -0
  10. package/dist/bytes.d.ts +140 -0
  11. package/dist/bytes.js +295 -0
  12. package/dist/bytes.js.map +1 -0
  13. package/dist/constants.d.ts +40 -0
  14. package/dist/constants.js +42 -0
  15. package/dist/constants.js.map +1 -0
  16. package/dist/externals.d.ts +15 -0
  17. package/dist/externals.js +39 -0
  18. package/dist/externals.js.map +1 -0
  19. package/dist/hash.d.ts +69 -0
  20. package/dist/hash.js +162 -0
  21. package/dist/hash.js.map +1 -0
  22. package/dist/helpers.d.ts +21 -0
  23. package/dist/helpers.js +49 -0
  24. package/dist/helpers.js.map +1 -0
  25. package/dist/index.d.ts +40 -0
  26. package/dist/index.js +68 -0
  27. package/dist/index.js.map +1 -0
  28. package/dist/internal.d.ts +77 -0
  29. package/dist/internal.js +191 -0
  30. package/dist/internal.js.map +1 -0
  31. package/dist/object.d.ts +12 -0
  32. package/dist/object.js +109 -0
  33. package/dist/object.js.map +1 -0
  34. package/dist/signature.d.ts +55 -0
  35. package/dist/signature.js +163 -0
  36. package/dist/signature.js.map +1 -0
  37. package/dist/types.d.ts +62 -0
  38. package/dist/types.js +77 -0
  39. package/dist/types.js.map +1 -0
  40. package/dist.browser/account.d.ts +120 -0
  41. package/dist.browser/account.js +296 -0
  42. package/dist.browser/account.js.map +1 -0
  43. package/dist.browser/address.d.ts +60 -0
  44. package/dist.browser/address.js +105 -0
  45. package/dist.browser/address.js.map +1 -0
  46. package/dist.browser/bytes.d.ts +140 -0
  47. package/dist.browser/bytes.js +333 -0
  48. package/dist.browser/bytes.js.map +1 -0
  49. package/dist.browser/constants.d.ts +40 -0
  50. package/dist.browser/constants.js +42 -0
  51. package/dist.browser/constants.js.map +1 -0
  52. package/dist.browser/externals.d.ts +15 -0
  53. package/dist.browser/externals.js +39 -0
  54. package/dist.browser/externals.js.map +1 -0
  55. package/dist.browser/hash.d.ts +69 -0
  56. package/dist.browser/hash.js +166 -0
  57. package/dist.browser/hash.js.map +1 -0
  58. package/dist.browser/helpers.d.ts +21 -0
  59. package/dist.browser/helpers.js +49 -0
  60. package/dist.browser/helpers.js.map +1 -0
  61. package/dist.browser/index.d.ts +40 -0
  62. package/dist.browser/index.js +68 -0
  63. package/dist.browser/index.js.map +1 -0
  64. package/dist.browser/internal.d.ts +77 -0
  65. package/dist.browser/internal.js +191 -0
  66. package/dist.browser/internal.js.map +1 -0
  67. package/dist.browser/object.d.ts +12 -0
  68. package/dist.browser/object.js +110 -0
  69. package/dist.browser/object.js.map +1 -0
  70. package/dist.browser/signature.d.ts +55 -0
  71. package/dist.browser/signature.js +164 -0
  72. package/dist.browser/signature.js.map +1 -0
  73. package/dist.browser/types.d.ts +62 -0
  74. package/dist.browser/types.js +77 -0
  75. package/dist.browser/types.js.map +1 -0
  76. package/package.json +105 -0
  77. package/src/account.ts +321 -0
  78. package/src/address.ts +117 -0
  79. package/src/bytes.ts +334 -0
  80. package/src/constants.ts +54 -0
  81. package/src/externals.ts +18 -0
  82. package/src/hash.ts +159 -0
  83. package/src/helpers.ts +45 -0
  84. package/src/index.ts +60 -0
  85. package/src/internal.ts +209 -0
  86. package/src/object.ts +117 -0
  87. package/src/signature.ts +209 -0
  88. package/src/types.ts +146 -0
@@ -0,0 +1,163 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.hashPersonalMessage = exports.isValidSignature = exports.fromRpcSig = exports.toCompactSig = exports.toRpcSig = exports.ecrecover = exports.ecsign = void 0;
4
+ const secp256k1_1 = require("ethereum-cryptography/secp256k1");
5
+ const externals_1 = require("./externals");
6
+ const bytes_1 = require("./bytes");
7
+ const hash_1 = require("./hash");
8
+ const helpers_1 = require("./helpers");
9
+ const types_1 = require("./types");
10
+ function ecsign(msgHash, privateKey, chainId) {
11
+ const { signature, recid: recovery } = (0, secp256k1_1.ecdsaSign)(msgHash, privateKey);
12
+ const r = Buffer.from(signature.slice(0, 32));
13
+ const s = Buffer.from(signature.slice(32, 64));
14
+ if (!chainId || typeof chainId === 'number') {
15
+ // return legacy type ECDSASignature (deprecated in favor of ECDSASignatureBuffer to handle large chainIds)
16
+ if (chainId && !Number.isSafeInteger(chainId)) {
17
+ throw new Error('The provided number is greater than MAX_SAFE_INTEGER (please use an alternative input type)');
18
+ }
19
+ const v = chainId ? recovery + (chainId * 2 + 35) : recovery + 27;
20
+ return { r, s, v };
21
+ }
22
+ const chainIdBN = (0, types_1.toType)(chainId, types_1.TypeOutput.BN);
23
+ const v = chainIdBN.muln(2).addn(35).addn(recovery).toArrayLike(Buffer);
24
+ return { r, s, v };
25
+ }
26
+ exports.ecsign = ecsign;
27
+ function calculateSigRecovery(v, chainId) {
28
+ const vBN = (0, types_1.toType)(v, types_1.TypeOutput.BN);
29
+ if (vBN.eqn(0) || vBN.eqn(1))
30
+ return (0, types_1.toType)(v, types_1.TypeOutput.BN);
31
+ if (!chainId) {
32
+ return vBN.subn(27);
33
+ }
34
+ const chainIdBN = (0, types_1.toType)(chainId, types_1.TypeOutput.BN);
35
+ return vBN.sub(chainIdBN.muln(2).addn(35));
36
+ }
37
+ function isValidSigRecovery(recovery) {
38
+ const rec = new externals_1.BN(recovery);
39
+ return rec.eqn(0) || rec.eqn(1);
40
+ }
41
+ /**
42
+ * ECDSA public key recovery from signature.
43
+ * NOTE: Accepts `v == 0 | v == 1` for EIP1559 transactions
44
+ * @returns Recovered public key
45
+ */
46
+ const ecrecover = function (msgHash, v, r, s, chainId) {
47
+ const signature = Buffer.concat([(0, bytes_1.setLengthLeft)(r, 32), (0, bytes_1.setLengthLeft)(s, 32)], 64);
48
+ const recovery = calculateSigRecovery(v, chainId);
49
+ if (!isValidSigRecovery(recovery)) {
50
+ throw new Error('Invalid signature v value');
51
+ }
52
+ const senderPubKey = (0, secp256k1_1.ecdsaRecover)(signature, recovery.toNumber(), msgHash);
53
+ return Buffer.from((0, secp256k1_1.publicKeyConvert)(senderPubKey, false).slice(1));
54
+ };
55
+ exports.ecrecover = ecrecover;
56
+ /**
57
+ * Convert signature parameters into the format of `eth_sign` RPC method.
58
+ * NOTE: Accepts `v == 0 | v == 1` for EIP1559 transactions
59
+ * @returns Signature
60
+ */
61
+ const toRpcSig = function (v, r, s, chainId) {
62
+ const recovery = calculateSigRecovery(v, chainId);
63
+ if (!isValidSigRecovery(recovery)) {
64
+ throw new Error('Invalid signature v value');
65
+ }
66
+ // geth (and the RPC eth_sign method) uses the 65 byte format used by Bitcoin
67
+ return (0, bytes_1.bufferToHex)(Buffer.concat([(0, bytes_1.setLengthLeft)(r, 32), (0, bytes_1.setLengthLeft)(s, 32), (0, bytes_1.toBuffer)(v)]));
68
+ };
69
+ exports.toRpcSig = toRpcSig;
70
+ /**
71
+ * Convert signature parameters into the format of Compact Signature Representation (EIP-2098).
72
+ * NOTE: Accepts `v == 0 | v == 1` for EIP1559 transactions
73
+ * @returns Signature
74
+ */
75
+ const toCompactSig = function (v, r, s, chainId) {
76
+ const recovery = calculateSigRecovery(v, chainId);
77
+ if (!isValidSigRecovery(recovery)) {
78
+ throw new Error('Invalid signature v value');
79
+ }
80
+ const vn = (0, types_1.toType)(v, types_1.TypeOutput.Number);
81
+ let ss = s;
82
+ if ((vn > 28 && vn % 2 === 1) || vn === 1 || vn === 28) {
83
+ ss = Buffer.from(s);
84
+ ss[0] |= 0x80;
85
+ }
86
+ return (0, bytes_1.bufferToHex)(Buffer.concat([(0, bytes_1.setLengthLeft)(r, 32), (0, bytes_1.setLengthLeft)(ss, 32)]));
87
+ };
88
+ exports.toCompactSig = toCompactSig;
89
+ /**
90
+ * Convert signature format of the `eth_sign` RPC method to signature parameters
91
+ * NOTE: all because of a bug in geth: https://github.com/ethereum/go-ethereum/issues/2053
92
+ * NOTE: After EIP1559, `v` could be `0` or `1` but this function assumes
93
+ * it's a signed message (EIP-191 or EIP-712) adding `27` at the end. Remove if needed.
94
+ */
95
+ const fromRpcSig = function (sig) {
96
+ const buf = (0, bytes_1.toBuffer)(sig);
97
+ let r;
98
+ let s;
99
+ let v;
100
+ if (buf.length >= 65) {
101
+ r = buf.slice(0, 32);
102
+ s = buf.slice(32, 64);
103
+ v = (0, bytes_1.bufferToInt)(buf.slice(64));
104
+ }
105
+ else if (buf.length === 64) {
106
+ // Compact Signature Representation (https://eips.ethereum.org/EIPS/eip-2098)
107
+ r = buf.slice(0, 32);
108
+ s = buf.slice(32, 64);
109
+ v = (0, bytes_1.bufferToInt)(buf.slice(32, 33)) >> 7;
110
+ s[0] &= 0x7f;
111
+ }
112
+ else {
113
+ throw new Error('Invalid signature length');
114
+ }
115
+ // support both versions of `eth_sign` responses
116
+ if (v < 27) {
117
+ v += 27;
118
+ }
119
+ return {
120
+ v,
121
+ r,
122
+ s,
123
+ };
124
+ };
125
+ exports.fromRpcSig = fromRpcSig;
126
+ /**
127
+ * Validate a ECDSA signature.
128
+ * NOTE: Accepts `v == 0 | v == 1` for EIP1559 transactions
129
+ * @param homesteadOrLater Indicates whether this is being used on either the homestead hardfork or a later one
130
+ */
131
+ const isValidSignature = function (v, r, s, homesteadOrLater = true, chainId) {
132
+ const SECP256K1_N_DIV_2 = new externals_1.BN('7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a0', 16);
133
+ const SECP256K1_N = new externals_1.BN('fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141', 16);
134
+ if (r.length !== 32 || s.length !== 32) {
135
+ return false;
136
+ }
137
+ if (!isValidSigRecovery(calculateSigRecovery(v, chainId))) {
138
+ return false;
139
+ }
140
+ const rBN = new externals_1.BN(r);
141
+ const sBN = new externals_1.BN(s);
142
+ if (rBN.isZero() || rBN.gt(SECP256K1_N) || sBN.isZero() || sBN.gt(SECP256K1_N)) {
143
+ return false;
144
+ }
145
+ if (homesteadOrLater && sBN.cmp(SECP256K1_N_DIV_2) === 1) {
146
+ return false;
147
+ }
148
+ return true;
149
+ };
150
+ exports.isValidSignature = isValidSignature;
151
+ /**
152
+ * Returns the keccak-256 hash of `message`, prefixed with the header used by the `eth_sign` RPC call.
153
+ * The output of this function can be fed into `ecsign` to produce the same signature as the `eth_sign`
154
+ * call for a given `message`, or fed to `ecrecover` along with a signature to recover the public key
155
+ * used to produce the signature.
156
+ */
157
+ const hashPersonalMessage = function (message) {
158
+ (0, helpers_1.assertIsBuffer)(message);
159
+ const prefix = Buffer.from(`\u0019Ethereum Signed Message:\n${message.length}`, 'utf-8');
160
+ return (0, hash_1.keccak)(Buffer.concat([prefix, message]));
161
+ };
162
+ exports.hashPersonalMessage = hashPersonalMessage;
163
+ //# sourceMappingURL=signature.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signature.js","sourceRoot":"","sources":["../src/signature.ts"],"names":[],"mappings":";;;AAAA,+DAA2F;AAC3F,2CAAgC;AAChC,mCAA2E;AAC3E,iCAA+B;AAC/B,uCAA0C;AAC1C,mCAAoD;AAmBpD,SAAgB,MAAM,CAAC,OAAe,EAAE,UAAkB,EAAE,OAAY;IACtE,MAAM,EAAE,SAAS,EAAE,KAAK,EAAE,QAAQ,EAAE,GAAG,IAAA,qBAAS,EAAC,OAAO,EAAE,UAAU,CAAC,CAAA;IAErE,MAAM,CAAC,GAAG,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAA;IAC7C,MAAM,CAAC,GAAG,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,CAAA;IAE9C,IAAI,CAAC,OAAO,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE;QAC3C,2GAA2G;Q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,CAAA;IAElG,IAAI,CAAC,CAAC,MAAM,KAAK,EAAE,IAAI,CAAC,CAAC,MAAM,KAAK,EAAE,EAAE;QACtC,OAAO,KAAK,CAAA;KACb;IAED,IAAI,CAAC,kBAAkB,CAAC,oBAAoB,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC,EAAE;QACzD,OAAO,KAAK,CAAA;KACb;IAED,MAAM,GAAG,GAAG,IAAI,cAAE,CAAC,CAAC,CAAC,CAAA;IACrB,MAAM,GAAG,GAAG,IAAI,cAAE,CAAC,CAAC,CAAC,CAAA;IAErB,IAAI,GAAG,CAAC,MAAM,EAAE,IAAI,GAAG,CAAC,EAAE,CAAC,WAAW,CAAC,IAAI,GAAG,CAAC,MAAM,EAAE,IAAI,GAAG,CAAC,EAAE,CAAC,WAAW,CAAC,EAAE;QAC9E,OAAO,KAAK,CAAA;KACb;IAED,IAAI,gBAAgB,IAAI,GAAG,CAAC,GAAG,CAAC,iBAAiB,CAAC,KAAK,CAAC,EAAE;QACxD,OAAO,KAAK,CAAA;KACb;IAED,OAAO,IAAI,CAAA;AACb,CAAC,CAAA;AAjCY,QAAA,gBAAgB,oBAiC5B;AAED;;;;;GAKG;AACI,MAAM,mBAAmB,GAAG,UAAU,OAAe;IAC1D,IAAA,wBAAc,EAAC,OAAO,CAAC,CAAA;IACvB,MAAM,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,mCAAmC,OAAO,CAAC,MAAM,EAAE,EAAE,OAAO,CAAC,CAAA;IACxF,OAAO,IAAA,aAAM,EAAC,MAAM,CAAC,MAAM,CAAC,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,CAAA;AACjD,CAAC,CAAA;AAJY,QAAA,mBAAmB,uBAI/B"}
@@ -0,0 +1,62 @@
1
+ /// <reference types="bn.js" />
2
+ /// <reference types="node" />
3
+ import { BN } from './externals';
4
+ import { Address } from './address';
5
+ import { ToBufferInputTypes } from './bytes';
6
+ export declare type BNLike = BN | PrefixedHexString | number | Buffer;
7
+ export declare type BufferLike = Buffer | Uint8Array | number[] | number | BN | TransformableToBuffer | PrefixedHexString;
8
+ export declare type PrefixedHexString = string;
9
+ /**
10
+ * A type that represents an Address-like value.
11
+ * To convert to address, use `new Address(toBuffer(value))`
12
+ */
13
+ export declare type AddressLike = Address | Buffer | PrefixedHexString;
14
+ export interface TransformableToArray {
15
+ toArray(): Uint8Array;
16
+ toBuffer?(): Buffer;
17
+ }
18
+ export interface TransformableToBuffer {
19
+ toBuffer(): Buffer;
20
+ toArray?(): Uint8Array;
21
+ }
22
+ export declare type NestedUint8Array = Array<Uint8Array | NestedUint8Array>;
23
+ export declare type NestedBufferArray = Array<Buffer | NestedBufferArray>;
24
+ /**
25
+ * Convert BN to 0x-prefixed hex string.
26
+ */
27
+ export declare function bnToHex(value: BN): PrefixedHexString;
28
+ /**
29
+ * Convert value from BN to an unpadded Buffer
30
+ * (useful for RLP transport)
31
+ * @param value value to convert
32
+ */
33
+ export declare function bnToUnpaddedBuffer(value: BN): Buffer;
34
+ /**
35
+ * Deprecated alias for {@link bnToUnpaddedBuffer}
36
+ * @deprecated
37
+ */
38
+ export declare function bnToRlp(value: BN): Buffer;
39
+ /**
40
+ * Type output options
41
+ */
42
+ export declare enum TypeOutput {
43
+ Number = 0,
44
+ BN = 1,
45
+ Buffer = 2,
46
+ PrefixedHexString = 3
47
+ }
48
+ export declare type TypeOutputReturnType = {
49
+ [TypeOutput.Number]: number;
50
+ [TypeOutput.BN]: BN;
51
+ [TypeOutput.Buffer]: Buffer;
52
+ [TypeOutput.PrefixedHexString]: PrefixedHexString;
53
+ };
54
+ /**
55
+ * Convert an input to a specified type.
56
+ * Input of null/undefined returns null/undefined regardless of the output type.
57
+ * @param input value to convert
58
+ * @param outputType type to output
59
+ */
60
+ export declare function toType<T extends TypeOutput>(input: null, outputType: T): null;
61
+ export declare function toType<T extends TypeOutput>(input: undefined, outputType: T): undefined;
62
+ export declare function toType<T extends TypeOutput>(input: ToBufferInputTypes, outputType: T): TypeOutputReturnType[T];
package/dist/types.js ADDED
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toType = exports.TypeOutput = exports.bnToRlp = exports.bnToUnpaddedBuffer = exports.bnToHex = void 0;
4
+ const externals_1 = require("./externals");
5
+ const internal_1 = require("./internal");
6
+ const bytes_1 = require("./bytes");
7
+ /**
8
+ * Convert BN to 0x-prefixed hex string.
9
+ */
10
+ function bnToHex(value) {
11
+ return `0x${value.toString(16)}`;
12
+ }
13
+ exports.bnToHex = bnToHex;
14
+ /**
15
+ * Convert value from BN to an unpadded Buffer
16
+ * (useful for RLP transport)
17
+ * @param value value to convert
18
+ */
19
+ function bnToUnpaddedBuffer(value) {
20
+ // Using `bn.toArrayLike(Buffer)` instead of `bn.toBuffer()`
21
+ // for compatibility with browserify and similar tools
22
+ return (0, bytes_1.unpadBuffer)(value.toArrayLike(Buffer));
23
+ }
24
+ exports.bnToUnpaddedBuffer = bnToUnpaddedBuffer;
25
+ /**
26
+ * Deprecated alias for {@link bnToUnpaddedBuffer}
27
+ * @deprecated
28
+ */
29
+ function bnToRlp(value) {
30
+ return bnToUnpaddedBuffer(value);
31
+ }
32
+ exports.bnToRlp = bnToRlp;
33
+ /**
34
+ * Type output options
35
+ */
36
+ var TypeOutput;
37
+ (function (TypeOutput) {
38
+ TypeOutput[TypeOutput["Number"] = 0] = "Number";
39
+ TypeOutput[TypeOutput["BN"] = 1] = "BN";
40
+ TypeOutput[TypeOutput["Buffer"] = 2] = "Buffer";
41
+ TypeOutput[TypeOutput["PrefixedHexString"] = 3] = "PrefixedHexString";
42
+ })(TypeOutput = exports.TypeOutput || (exports.TypeOutput = {}));
43
+ function toType(input, outputType) {
44
+ if (input === null) {
45
+ return null;
46
+ }
47
+ if (input === undefined) {
48
+ return undefined;
49
+ }
50
+ if (typeof input === 'string' && !(0, internal_1.isHexString)(input)) {
51
+ throw new Error(`A string must be provided with a 0x-prefix, given: ${input}`);
52
+ }
53
+ else if (typeof input === 'number' && !Number.isSafeInteger(input)) {
54
+ throw new Error('The provided number is greater than MAX_SAFE_INTEGER (please use an alternative input type)');
55
+ }
56
+ const output = (0, bytes_1.toBuffer)(input);
57
+ if (outputType === TypeOutput.Buffer) {
58
+ return output;
59
+ }
60
+ else if (outputType === TypeOutput.BN) {
61
+ return new externals_1.BN(output);
62
+ }
63
+ else if (outputType === TypeOutput.Number) {
64
+ const bn = new externals_1.BN(output);
65
+ const max = new externals_1.BN(Number.MAX_SAFE_INTEGER.toString());
66
+ if (bn.gt(max)) {
67
+ throw new Error('The provided number is greater than MAX_SAFE_INTEGER (please use an alternative output type)');
68
+ }
69
+ return bn.toNumber();
70
+ }
71
+ else {
72
+ // outputType === TypeOutput.PrefixedHexString
73
+ return `0x${output.toString('hex')}`;
74
+ }
75
+ }
76
+ exports.toType = toType;
77
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";;;AAAA,2CAAgC;AAChC,yCAAwC;AAExC,mCAAmE;AAiDnE;;GAEG;AACH,SAAgB,OAAO,CAAC,KAAS;IAC/B,OAAO,KAAK,KAAK,CAAC,QAAQ,CAAC,EAAE,CAAC,EAAE,CAAA;AAClC,CAAC;AAFD,0BAEC;AAED;;;;GAIG;AACH,SAAgB,kBAAkB,CAAC,KAAS;IAC1C,4DAA4D;IAC5D,sDAAsD;IACtD,OAAO,IAAA,mBAAW,EAAC,KAAK,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAA;AAC/C,CAAC;AAJD,gDAIC;AAED;;;GAGG;AACH,SAAgB,OAAO,CAAC,KAAS;IAC/B,OAAO,kBAAkB,CAAC,KAAK,CAAC,CAAA;AAClC,CAAC;AAFD,0BAEC;AAED;;GAEG;AACH,IAAY,UAKX;AALD,WAAY,UAAU;IACpB,+CAAM,CAAA;IACN,uCAAE,CAAA;IACF,+CAAM,CAAA;IACN,qEAAi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,CAAA;KAChE;AACH,CAAC;AAtCD,wBAsCC"}
@@ -0,0 +1,120 @@
1
+ /// <reference types="bn.js" />
2
+ /// <reference types="node" />
3
+ import { BN } from './externals';
4
+ import { BNLike, BufferLike } from './types';
5
+ export interface AccountData {
6
+ nonce?: BNLike;
7
+ balance?: BNLike;
8
+ stateRoot?: BufferLike;
9
+ codeHash?: BufferLike;
10
+ }
11
+ export declare class Account {
12
+ nonce: BN;
13
+ balance: BN;
14
+ stateRoot: Buffer;
15
+ codeHash: Buffer;
16
+ static fromAccountData(accountData: AccountData): Account;
17
+ static fromRlpSerializedAccount(serialized: Buffer): Account;
18
+ static fromValuesArray(values: Buffer[]): Account;
19
+ /**
20
+ * This constructor assigns and validates the values.
21
+ * Use the static factory methods to assist in creating an Account from varying data types.
22
+ */
23
+ constructor(nonce?: BN, balance?: BN, stateRoot?: Buffer, codeHash?: Buffer);
24
+ private _validate;
25
+ /**
26
+ * Returns a Buffer Array of the raw Buffers for the account, in order.
27
+ */
28
+ raw(): Buffer[];
29
+ /**
30
+ * Returns the RLP serialization of the account as a `Buffer`.
31
+ */
32
+ serialize(): Buffer;
33
+ /**
34
+ * Returns a `Boolean` determining if the account is a contract.
35
+ */
36
+ isContract(): boolean;
37
+ /**
38
+ * Returns a `Boolean` determining if the account is empty complying to the definition of
39
+ * account emptiness in [EIP-161](https://eips.ethereum.org/EIPS/eip-161):
40
+ * "An account is considered empty when it has no code and zero nonce and zero balance."
41
+ */
42
+ isEmpty(): boolean;
43
+ }
44
+ /**
45
+ * Checks if the address is a valid. Accepts checksummed addresses too.
46
+ */
47
+ export declare const isValidAddress: (hexAddress: string) => boolean;
48
+ /**
49
+ * Returns a checksummed address.
50
+ *
51
+ * If an eip1191ChainId is provided, the chainId will be included in the checksum calculation. This
52
+ * has the effect of checksummed addresses for one chain having invalid checksums for others.
53
+ * For more details see [EIP-1191](https://eips.ethereum.org/EIPS/eip-1191).
54
+ *
55
+ * WARNING: Checksums with and without the chainId will differ and the EIP-1191 checksum is not
56
+ * backwards compatible to the original widely adopted checksum format standard introduced in
57
+ * [EIP-55](https://eips.ethereum.org/EIPS/eip-55), so this will break in existing applications.
58
+ * Usage of this EIP is therefore discouraged unless you have a very targeted use case.
59
+ */
60
+ export declare const toChecksumAddress: (hexAddress: string, eip1191ChainId?: BNLike) => string;
61
+ /**
62
+ * Checks if the address is a valid checksummed address.
63
+ *
64
+ * See toChecksumAddress' documentation for details about the eip1191ChainId parameter.
65
+ */
66
+ export declare const isValidChecksumAddress: (hexAddress: string, eip1191ChainId?: BNLike) => boolean;
67
+ /**
68
+ * Generates an address of a newly created contract.
69
+ * @param from The address which is creating this new address
70
+ * @param nonce The nonce of the from account
71
+ */
72
+ export declare const generateAddress: (from: Buffer, nonce: Buffer) => Buffer;
73
+ /**
74
+ * Generates an address for a contract created using CREATE2.
75
+ * @param from The address which is creating this new address
76
+ * @param salt A salt
77
+ * @param initCode The init code of the contract being created
78
+ */
79
+ export declare const generateAddress2: (from: Buffer, salt: Buffer, initCode: Buffer) => Buffer;
80
+ /**
81
+ * Checks if the private key satisfies the rules of the curve secp256k1.
82
+ */
83
+ export declare const isValidPrivate: (privateKey: Buffer) => boolean;
84
+ /**
85
+ * Checks if the public key satisfies the rules of the curve secp256k1
86
+ * and the requirements of Ethereum.
87
+ * @param publicKey The two points of an uncompressed key, unless sanitize is enabled
88
+ * @param sanitize Accept public keys in other formats
89
+ */
90
+ export declare const isValidPublic: (publicKey: Buffer, sanitize?: boolean) => boolean;
91
+ /**
92
+ * Returns the ethereum address of a given public key.
93
+ * Accepts "Ethereum public keys" and SEC1 encoded keys.
94
+ * @param pubKey The two points of an uncompressed key, unless sanitize is enabled
95
+ * @param sanitize Accept public keys in other formats
96
+ */
97
+ export declare const pubToAddress: (pubKey: Buffer, sanitize?: boolean) => Buffer;
98
+ export declare const publicToAddress: (pubKey: Buffer, sanitize?: boolean) => Buffer;
99
+ /**
100
+ * Returns the ethereum public key of a given private key.
101
+ * @param privateKey A private key must be 256 bits wide
102
+ */
103
+ export declare const privateToPublic: (privateKey: Buffer) => Buffer;
104
+ /**
105
+ * Returns the ethereum address of a given private key.
106
+ * @param privateKey A private key must be 256 bits wide
107
+ */
108
+ export declare const privateToAddress: (privateKey: Buffer) => Buffer;
109
+ /**
110
+ * Converts a public key to the Ethereum format.
111
+ */
112
+ export declare const importPublic: (publicKey: Buffer) => Buffer;
113
+ /**
114
+ * Returns the zero address.
115
+ */
116
+ export declare const zeroAddress: () => string;
117
+ /**
118
+ * Checks if a given address is the zero address.
119
+ */
120
+ export declare const isZeroAddress: (hexAddress: string) => boolean;