@ethersphere/bee-js 8.3.1 → 9.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +192 -50
- package/dist/cjs/bee-dev.js +78 -0
- package/dist/cjs/bee.js +341 -417
- package/dist/cjs/chunk/bmt.js +15 -32
- package/dist/cjs/chunk/cac.js +26 -36
- package/dist/cjs/chunk/soc.js +56 -51
- package/dist/cjs/feed/identifier.js +3 -28
- package/dist/cjs/feed/index.js +95 -44
- package/dist/cjs/feed/retrievable.js +11 -22
- package/dist/cjs/index.js +17 -1
- package/dist/cjs/manifest/manifest.js +369 -0
- package/dist/cjs/modules/bytes.js +24 -12
- package/dist/cjs/modules/bzz.js +24 -34
- package/dist/cjs/modules/chunk.js +13 -7
- package/dist/cjs/modules/debug/balance.js +29 -4
- package/dist/cjs/modules/debug/chequebook.js +60 -16
- package/dist/cjs/modules/debug/connectivity.js +82 -4
- package/dist/cjs/modules/debug/settlements.js +22 -2
- package/dist/cjs/modules/debug/stake.js +29 -11
- package/dist/cjs/modules/debug/stamps.js +88 -7
- package/dist/cjs/modules/debug/states.js +25 -3
- package/dist/cjs/modules/debug/status.js +45 -47
- package/dist/cjs/modules/debug/transactions.js +27 -5
- package/dist/cjs/modules/envelope.js +8 -6
- package/dist/cjs/modules/feed.js +25 -10
- package/dist/cjs/modules/grantee.js +18 -12
- package/dist/cjs/modules/gsoc.js +24 -0
- package/dist/cjs/modules/pinning.js +13 -2
- package/dist/cjs/modules/pss.js +9 -3
- package/dist/cjs/modules/soc.js +9 -4
- package/dist/cjs/modules/stewardship.js +7 -3
- package/dist/cjs/modules/tag.js +35 -3
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/stamper/stamper.js +43 -0
- package/dist/cjs/types/debug.js +16 -1
- package/dist/cjs/types/index.js +2 -24
- package/dist/cjs/utils/bytes.js +67 -115
- package/dist/cjs/utils/chunk-size.js +17 -0
- package/dist/cjs/utils/chunk-stream.browser.js +85 -0
- package/dist/cjs/utils/chunk-stream.js +84 -0
- package/dist/cjs/utils/cid.js +5 -6
- package/dist/cjs/utils/collection.browser.js +2 -2
- package/dist/cjs/utils/collection.js +3 -2
- package/dist/cjs/utils/collection.node.js +0 -2
- package/dist/cjs/utils/constants.js +7 -3
- package/dist/cjs/utils/data.browser.js +6 -3
- package/dist/cjs/utils/data.js +8 -4
- package/dist/cjs/utils/duration.js +51 -0
- package/dist/cjs/utils/expose.js +11 -40
- package/dist/cjs/utils/headers.js +79 -52
- package/dist/cjs/utils/http.js +34 -10
- package/dist/cjs/utils/mime.js +78 -0
- package/dist/cjs/utils/pss.js +3 -4
- package/dist/cjs/utils/redundancy.js +18 -14
- package/dist/cjs/utils/resource-locator.js +17 -0
- package/dist/cjs/utils/size.js +35 -0
- package/dist/cjs/utils/stamps.js +67 -51
- package/dist/cjs/utils/tar-uploader.browser.js +2 -2
- package/dist/cjs/utils/tar-uploader.js +2 -2
- package/dist/cjs/utils/tokens.js +144 -0
- package/dist/cjs/utils/type.js +142 -350
- package/dist/cjs/utils/typed-bytes.js +179 -0
- package/dist/cjs/utils/upload-progress.js +2 -0
- package/dist/cjs/utils/url.js +0 -4
- package/dist/cjs/utils/workaround.js +27 -0
- package/dist/index.browser.min.js +1 -2
- package/dist/index.browser.min.js.map +1 -1
- package/dist/mjs/bee-dev.js +98 -0
- package/dist/mjs/bee.js +367 -411
- package/dist/mjs/chunk/bmt.js +13 -29
- package/dist/mjs/chunk/cac.js +26 -34
- package/dist/mjs/chunk/soc.js +57 -53
- package/dist/mjs/feed/identifier.js +3 -26
- package/dist/mjs/feed/index.js +98 -51
- package/dist/mjs/feed/retrievable.js +12 -23
- package/dist/mjs/index.js +10 -1
- package/dist/mjs/manifest/manifest.js +371 -0
- package/dist/mjs/modules/bytes.js +27 -15
- package/dist/mjs/modules/bzz.js +25 -32
- package/dist/mjs/modules/chunk.js +16 -8
- package/dist/mjs/modules/debug/balance.js +61 -4
- package/dist/mjs/modules/debug/chequebook.js +116 -16
- package/dist/mjs/modules/debug/connectivity.js +144 -3
- package/dist/mjs/modules/debug/settlements.js +46 -2
- package/dist/mjs/modules/debug/stake.js +69 -11
- package/dist/mjs/modules/debug/stamps.js +184 -7
- package/dist/mjs/modules/debug/states.js +55 -3
- package/dist/mjs/modules/debug/status.js +97 -45
- package/dist/mjs/modules/debug/transactions.js +61 -5
- package/dist/mjs/modules/envelope.js +11 -9
- package/dist/mjs/modules/feed.js +26 -10
- package/dist/mjs/modules/grantee.js +35 -13
- package/dist/mjs/modules/gsoc.js +16 -0
- package/dist/mjs/modules/pinning.js +23 -2
- package/dist/mjs/modules/pss.js +10 -4
- package/dist/mjs/modules/soc.js +10 -5
- package/dist/mjs/modules/stewardship.js +14 -4
- package/dist/mjs/modules/tag.js +93 -3
- package/dist/mjs/package.json +2 -1
- package/dist/mjs/stamper/stamper.js +39 -0
- package/dist/mjs/types/debug.js +15 -1
- package/dist/mjs/types/index.js +2 -24
- package/dist/mjs/utils/bytes.js +63 -104
- package/dist/mjs/utils/chunk-size.js +13 -0
- package/dist/mjs/utils/chunk-stream.browser.js +85 -0
- package/dist/mjs/utils/chunk-stream.js +87 -0
- package/dist/mjs/utils/cid.js +5 -6
- package/dist/mjs/utils/collection.browser.js +2 -2
- package/dist/mjs/utils/collection.js +2 -2
- package/dist/mjs/utils/collection.node.js +0 -2
- package/dist/mjs/utils/constants.js +6 -2
- package/dist/mjs/utils/data.browser.js +9 -3
- package/dist/mjs/utils/data.js +12 -4
- package/dist/mjs/utils/duration.js +47 -0
- package/dist/mjs/utils/expose.js +2 -7
- package/dist/mjs/utils/headers.js +73 -48
- package/dist/mjs/utils/http.js +33 -11
- package/dist/mjs/utils/mime.js +75 -0
- package/dist/mjs/utils/pss.js +3 -4
- package/dist/mjs/utils/redundancy.js +18 -8
- package/dist/mjs/utils/resource-locator.js +13 -0
- package/dist/mjs/utils/size.js +31 -0
- package/dist/mjs/utils/stamps.js +47 -44
- package/dist/mjs/utils/tar-uploader.browser.js +2 -2
- package/dist/mjs/utils/tar-uploader.js +2 -2
- package/dist/mjs/utils/tokens.js +139 -0
- package/dist/mjs/utils/type.js +216 -310
- package/dist/mjs/utils/typed-bytes.js +160 -0
- package/dist/mjs/utils/upload-progress.js +1 -0
- package/dist/mjs/utils/url.js +0 -4
- package/dist/mjs/utils/workaround.js +22 -0
- package/dist/types/bee-dev.d.ts +5 -0
- package/dist/types/bee.d.ts +88 -165
- package/dist/types/chunk/bmt.d.ts +2 -2
- package/dist/types/chunk/cac.d.ts +7 -24
- package/dist/types/chunk/soc.d.ts +15 -15
- package/dist/types/feed/identifier.d.ts +2 -4
- package/dist/types/feed/index.d.ts +14 -18
- package/dist/types/feed/retrievable.d.ts +3 -4
- package/dist/types/index.d.ts +28 -1
- package/dist/types/manifest/manifest.d.ts +106 -0
- package/dist/types/modules/bytes.d.ts +8 -5
- package/dist/types/modules/bzz.d.ts +8 -10
- package/dist/types/modules/chunk.d.ts +4 -3
- package/dist/types/modules/debug/balance.d.ts +3 -2
- package/dist/types/modules/debug/chequebook.d.ts +7 -6
- package/dist/types/modules/debug/connectivity.d.ts +5 -3
- package/dist/types/modules/debug/settlements.d.ts +2 -1
- package/dist/types/modules/debug/stake.d.ts +4 -2
- package/dist/types/modules/debug/stamps.d.ts +5 -4
- package/dist/types/modules/debug/status.d.ts +6 -25
- package/dist/types/modules/debug/transactions.d.ts +5 -4
- package/dist/types/modules/envelope.d.ts +3 -2
- package/dist/types/modules/feed.d.ts +20 -16
- package/dist/types/modules/grantee.d.ts +7 -6
- package/dist/types/modules/gsoc.d.ts +7 -0
- package/dist/types/modules/pinning.d.ts +2 -4
- package/dist/types/modules/pss.d.ts +4 -3
- package/dist/types/modules/soc.d.ts +3 -2
- package/dist/types/modules/stewardship.d.ts +4 -4
- package/dist/types/modules/tag.d.ts +2 -1
- package/dist/types/stamper/stamper.d.ts +15 -0
- package/dist/types/types/debug.d.ts +63 -115
- package/dist/types/types/index.d.ts +103 -204
- package/dist/types/utils/bytes.d.ts +16 -90
- package/dist/types/utils/chunk-size.d.ts +1 -0
- package/dist/types/utils/chunk-stream.browser.d.ts +6 -0
- package/dist/types/utils/chunk-stream.d.ts +6 -0
- package/dist/types/utils/cid.d.ts +3 -2
- package/dist/types/utils/collection.browser.d.ts +2 -2
- package/dist/types/utils/collection.d.ts +2 -1
- package/dist/types/utils/collection.node.d.ts +0 -1
- package/dist/types/utils/constants.d.ts +4 -1
- package/dist/types/utils/duration.d.ts +17 -0
- package/dist/types/utils/error.d.ts +2 -2
- package/dist/types/utils/expose.d.ts +2 -7
- package/dist/types/utils/headers.d.ts +3 -4
- package/dist/types/utils/mime.d.ts +1 -0
- package/dist/types/utils/pss.d.ts +2 -2
- package/dist/types/utils/resource-locator.d.ts +6 -0
- package/dist/types/utils/size.d.ts +16 -0
- package/dist/types/utils/stamps.d.ts +27 -33
- package/dist/types/utils/tar-uploader.browser.d.ts +3 -4
- package/dist/types/utils/tar-uploader.d.ts +3 -4
- package/dist/types/utils/tokens.d.ts +77 -0
- package/dist/types/utils/type.d.ts +20 -52
- package/dist/types/utils/typed-bytes.d.ts +68 -0
- package/dist/types/utils/upload-progress.d.ts +4 -0
- package/dist/types/utils/workaround.d.ts +2 -0
- package/package.json +11 -17
- package/dist/cjs/chunk/signer.js +0 -126
- package/dist/cjs/chunk/span.js +0 -25
- package/dist/cjs/feed/json.js +0 -28
- package/dist/cjs/feed/topic.js +0 -25
- package/dist/cjs/feed/type.js +0 -15
- package/dist/cjs/modules/debug/chunk.js +0 -21
- package/dist/cjs/modules/debug/tag.js +0 -19
- package/dist/cjs/utils/eth.js +0 -216
- package/dist/cjs/utils/hash.js +0 -21
- package/dist/cjs/utils/hex.js +0 -150
- package/dist/cjs/utils/reference.js +0 -36
- package/dist/index.browser.min.js.LICENSE.txt +0 -8
- package/dist/mjs/chunk/signer.js +0 -114
- package/dist/mjs/chunk/span.js +0 -21
- package/dist/mjs/feed/json.js +0 -26
- package/dist/mjs/feed/topic.js +0 -19
- package/dist/mjs/feed/type.js +0 -10
- package/dist/mjs/modules/debug/chunk.js +0 -17
- package/dist/mjs/modules/debug/tag.js +0 -15
- package/dist/mjs/utils/eth.js +0 -192
- package/dist/mjs/utils/hash.js +0 -16
- package/dist/mjs/utils/hex.js +0 -135
- package/dist/mjs/utils/reference.js +0 -29
- package/dist/types/chunk/signer.d.ts +0 -31
- package/dist/types/chunk/span.d.ts +0 -10
- package/dist/types/feed/json.d.ts +0 -4
- package/dist/types/feed/topic.d.ts +0 -3
- package/dist/types/feed/type.d.ts +0 -6
- package/dist/types/modules/debug/chunk.d.ts +0 -10
- package/dist/types/modules/debug/tag.d.ts +0 -8
- package/dist/types/utils/eth.d.ts +0 -67
- package/dist/types/utils/hash.d.ts +0 -9
- package/dist/types/utils/hex.d.ts +0 -86
- package/dist/types/utils/reference.d.ts +0 -2
package/dist/cjs/chunk/signer.js
DELETED
|
@@ -1,126 +0,0 @@
|
|
|
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.sign = exports.makeSigner = exports.assertSigner = exports.makePrivateKeySigner = exports.recoverAddress = exports.defaultSign = void 0;
|
|
7
|
-
// For ESM compatibility
|
|
8
|
-
const elliptic_1 = __importDefault(require("elliptic"));
|
|
9
|
-
const { ec } = elliptic_1.default;
|
|
10
|
-
const error_1 = require("../utils/error");
|
|
11
|
-
const bytes_1 = require("../utils/bytes");
|
|
12
|
-
const hash_1 = require("../utils/hash");
|
|
13
|
-
const hex_1 = require("../utils/hex");
|
|
14
|
-
const types_1 = require("../types");
|
|
15
|
-
const type_1 = require("../utils/type");
|
|
16
|
-
const UNCOMPRESSED_RECOVERY_ID = 27;
|
|
17
|
-
function hashWithEthereumPrefix(data) {
|
|
18
|
-
const ethereumSignedMessagePrefix = `\x19Ethereum Signed Message:\n${data.length}`;
|
|
19
|
-
const prefixBytes = new TextEncoder().encode(ethereumSignedMessagePrefix);
|
|
20
|
-
return (0, hash_1.keccak256Hash)(prefixBytes, data);
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* The default signer function that can be used for integrating with
|
|
24
|
-
* other applications (e.g. wallets).
|
|
25
|
-
*
|
|
26
|
-
* @param data The data to be signed
|
|
27
|
-
* @param privateKey The private key used for signing the data
|
|
28
|
-
*/
|
|
29
|
-
function defaultSign(data, privateKey) {
|
|
30
|
-
const curve = new ec('secp256k1');
|
|
31
|
-
const keyPair = curve.keyFromPrivate(privateKey);
|
|
32
|
-
const hashedDigest = hashWithEthereumPrefix(data);
|
|
33
|
-
const sigRaw = curve.sign(hashedDigest, keyPair, { canonical: true, pers: undefined });
|
|
34
|
-
if (sigRaw.recoveryParam === null) {
|
|
35
|
-
throw new error_1.BeeError('signDigest recovery param was null');
|
|
36
|
-
}
|
|
37
|
-
const signature = new Uint8Array([
|
|
38
|
-
...sigRaw.r.toArray('be', 32),
|
|
39
|
-
...sigRaw.s.toArray('be', 32),
|
|
40
|
-
sigRaw.recoveryParam + UNCOMPRESSED_RECOVERY_ID,
|
|
41
|
-
]);
|
|
42
|
-
return signature;
|
|
43
|
-
}
|
|
44
|
-
exports.defaultSign = defaultSign;
|
|
45
|
-
function publicKeyToAddress(pubKey) {
|
|
46
|
-
const pubBytes = pubKey.encode('array', false);
|
|
47
|
-
return (0, hash_1.keccak256Hash)(pubBytes.slice(1)).slice(12);
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Recovers the ethereum address from a given signature.
|
|
51
|
-
*
|
|
52
|
-
* Can be used for verifying a piece of data when the public key is
|
|
53
|
-
* known.
|
|
54
|
-
*
|
|
55
|
-
* @param signature The signature
|
|
56
|
-
* @param digest The digest of the data
|
|
57
|
-
*
|
|
58
|
-
* @returns the recovered address
|
|
59
|
-
*/
|
|
60
|
-
function recoverAddress(signature, digest) {
|
|
61
|
-
const curve = new ec('secp256k1');
|
|
62
|
-
const sig = {
|
|
63
|
-
r: signature.slice(0, 32),
|
|
64
|
-
s: signature.slice(32, 64),
|
|
65
|
-
};
|
|
66
|
-
const recoveryParam = signature[64] - UNCOMPRESSED_RECOVERY_ID;
|
|
67
|
-
const hash = hashWithEthereumPrefix(digest);
|
|
68
|
-
const recPubKey = curve.recoverPubKey(hash, sig, recoveryParam);
|
|
69
|
-
return publicKeyToAddress(recPubKey);
|
|
70
|
-
}
|
|
71
|
-
exports.recoverAddress = recoverAddress;
|
|
72
|
-
/**
|
|
73
|
-
* Creates a singer object that can be used when the private key is known.
|
|
74
|
-
*
|
|
75
|
-
* @param privateKey The private key
|
|
76
|
-
*/
|
|
77
|
-
function makePrivateKeySigner(privateKey) {
|
|
78
|
-
const curve = new ec('secp256k1');
|
|
79
|
-
const keyPair = curve.keyFromPrivate(privateKey);
|
|
80
|
-
const address = publicKeyToAddress(keyPair.getPublic());
|
|
81
|
-
return {
|
|
82
|
-
sign: (digest) => defaultSign(digest, privateKey),
|
|
83
|
-
address,
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
exports.makePrivateKeySigner = makePrivateKeySigner;
|
|
87
|
-
function assertSigner(signer) {
|
|
88
|
-
if (!(0, type_1.isStrictlyObject)(signer)) {
|
|
89
|
-
throw new TypeError('Signer must be an object!');
|
|
90
|
-
}
|
|
91
|
-
const typedSigner = signer;
|
|
92
|
-
if (!(0, bytes_1.isBytes)(typedSigner.address, 20)) {
|
|
93
|
-
throw new TypeError("Signer's address must be Uint8Array with 20 bytes!");
|
|
94
|
-
}
|
|
95
|
-
if (typeof typedSigner.sign !== 'function') {
|
|
96
|
-
throw new TypeError('Signer sign property needs to be function!');
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
exports.assertSigner = assertSigner;
|
|
100
|
-
function makeSigner(signer) {
|
|
101
|
-
if (typeof signer === 'string') {
|
|
102
|
-
const hexKey = (0, hex_1.makeHexString)(signer, 64);
|
|
103
|
-
const keyBytes = (0, hex_1.hexToBytes)(hexKey); // HexString is verified for 64 length => 32 is guaranteed
|
|
104
|
-
return makePrivateKeySigner(keyBytes);
|
|
105
|
-
}
|
|
106
|
-
else if (signer instanceof Uint8Array) {
|
|
107
|
-
(0, bytes_1.assertBytes)(signer, 32);
|
|
108
|
-
return makePrivateKeySigner(signer);
|
|
109
|
-
}
|
|
110
|
-
assertSigner(signer);
|
|
111
|
-
return signer;
|
|
112
|
-
}
|
|
113
|
-
exports.makeSigner = makeSigner;
|
|
114
|
-
async function sign(signer, data) {
|
|
115
|
-
const result = await signer.sign((0, bytes_1.wrapBytesWithHelpers)(data));
|
|
116
|
-
if (typeof result === 'string') {
|
|
117
|
-
const hexString = (0, hex_1.makeHexString)(result, types_1.SIGNATURE_HEX_LENGTH);
|
|
118
|
-
return (0, hex_1.hexToBytes)(hexString);
|
|
119
|
-
}
|
|
120
|
-
if (result instanceof Uint8Array) {
|
|
121
|
-
(0, bytes_1.assertBytes)(result, types_1.SIGNATURE_BYTES_LENGTH);
|
|
122
|
-
return result;
|
|
123
|
-
}
|
|
124
|
-
throw new TypeError('Invalid output of sign function!');
|
|
125
|
-
}
|
|
126
|
-
exports.sign = sign;
|
package/dist/cjs/chunk/span.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.makeSpan = exports.SPAN_SIZE = void 0;
|
|
4
|
-
const cafe_utility_1 = require("cafe-utility");
|
|
5
|
-
const error_1 = require("../utils/error");
|
|
6
|
-
exports.SPAN_SIZE = 8;
|
|
7
|
-
// we limit the maximum span size in 32 bits to avoid BigInt compatibility issues
|
|
8
|
-
const MAX_SPAN_LENGTH = 2 ** 32 - 1;
|
|
9
|
-
/**
|
|
10
|
-
* Create a span for storing the length of the chunk
|
|
11
|
-
*
|
|
12
|
-
* The length is encoded in 64-bit little endian.
|
|
13
|
-
*
|
|
14
|
-
* @param length The length of the span
|
|
15
|
-
*/
|
|
16
|
-
function makeSpan(length) {
|
|
17
|
-
if (length <= 0) {
|
|
18
|
-
throw new error_1.BeeArgumentError('invalid length for span', length);
|
|
19
|
-
}
|
|
20
|
-
if (length > MAX_SPAN_LENGTH) {
|
|
21
|
-
throw new error_1.BeeArgumentError('invalid length (> MAX_SPAN_LENGTH)', length);
|
|
22
|
-
}
|
|
23
|
-
return cafe_utility_1.Binary.numberToUint64LE(length);
|
|
24
|
-
}
|
|
25
|
-
exports.makeSpan = makeSpan;
|
package/dist/cjs/feed/json.js
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.setJsonData = exports.getJsonData = void 0;
|
|
4
|
-
const type_1 = require("../utils/type");
|
|
5
|
-
function serializeJson(data) {
|
|
6
|
-
try {
|
|
7
|
-
const jsonString = JSON.stringify(data);
|
|
8
|
-
return new TextEncoder().encode(jsonString);
|
|
9
|
-
}
|
|
10
|
-
catch (e) {
|
|
11
|
-
if ((0, type_1.isError)(e)) {
|
|
12
|
-
e.message = `JsonFeed: ${e.message}`;
|
|
13
|
-
}
|
|
14
|
-
throw e;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
async function getJsonData(bee, reader) {
|
|
18
|
-
const feedUpdate = await reader.download();
|
|
19
|
-
const retrievedData = await bee.downloadData(feedUpdate.reference);
|
|
20
|
-
return retrievedData.json();
|
|
21
|
-
}
|
|
22
|
-
exports.getJsonData = getJsonData;
|
|
23
|
-
async function setJsonData(bee, writer, postageBatchId, data, options, requestOptions) {
|
|
24
|
-
const serializedData = serializeJson(data);
|
|
25
|
-
const { reference } = await bee.uploadData(postageBatchId, serializedData, options, requestOptions);
|
|
26
|
-
return writer.upload(postageBatchId, reference, { pin: options?.pin });
|
|
27
|
-
}
|
|
28
|
-
exports.setJsonData = setJsonData;
|
package/dist/cjs/feed/topic.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.makeTopicFromString = exports.makeTopic = void 0;
|
|
4
|
-
const hash_1 = require("../utils/hash");
|
|
5
|
-
const bytes_1 = require("../utils/bytes");
|
|
6
|
-
const hex_1 = require("../utils/hex");
|
|
7
|
-
const types_1 = require("../types");
|
|
8
|
-
function makeTopic(topic) {
|
|
9
|
-
if (typeof topic === 'string') {
|
|
10
|
-
return (0, hex_1.makeHexString)(topic, types_1.TOPIC_HEX_LENGTH);
|
|
11
|
-
}
|
|
12
|
-
else if (topic instanceof Uint8Array) {
|
|
13
|
-
(0, bytes_1.assertBytes)(topic, types_1.TOPIC_BYTES_LENGTH);
|
|
14
|
-
return (0, hex_1.bytesToHex)(topic, types_1.TOPIC_HEX_LENGTH);
|
|
15
|
-
}
|
|
16
|
-
throw new TypeError('invalid topic');
|
|
17
|
-
}
|
|
18
|
-
exports.makeTopic = makeTopic;
|
|
19
|
-
function makeTopicFromString(s) {
|
|
20
|
-
if (typeof s !== 'string') {
|
|
21
|
-
throw new TypeError('topic has to be string!');
|
|
22
|
-
}
|
|
23
|
-
return (0, hex_1.bytesToHex)((0, hash_1.keccak256Hash)(s), types_1.TOPIC_HEX_LENGTH);
|
|
24
|
-
}
|
|
25
|
-
exports.makeTopicFromString = makeTopicFromString;
|
package/dist/cjs/feed/type.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.assertFeedType = exports.isFeedType = exports.DEFAULT_FEED_TYPE = void 0;
|
|
4
|
-
const feedTypes = ['sequence', 'epoch'];
|
|
5
|
-
exports.DEFAULT_FEED_TYPE = 'sequence';
|
|
6
|
-
function isFeedType(type) {
|
|
7
|
-
return typeof type === 'string' && feedTypes.includes(type);
|
|
8
|
-
}
|
|
9
|
-
exports.isFeedType = isFeedType;
|
|
10
|
-
function assertFeedType(type) {
|
|
11
|
-
if (!isFeedType(type)) {
|
|
12
|
-
throw new TypeError('invalid feed type');
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
exports.assertFeedType = assertFeedType;
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.checkIfChunkExistsLocally = void 0;
|
|
4
|
-
const http_1 = require("../../utils/http");
|
|
5
|
-
const endpoint = 'chunks';
|
|
6
|
-
/**
|
|
7
|
-
* Check if chunk at address exists locally
|
|
8
|
-
*
|
|
9
|
-
* @param requestOptions Options for making requests
|
|
10
|
-
* @param address Swarm address of chunk
|
|
11
|
-
*
|
|
12
|
-
* @returns BeeGenericResponse if chunk is found or throws an exception
|
|
13
|
-
*/
|
|
14
|
-
async function checkIfChunkExistsLocally(requestOptions, address) {
|
|
15
|
-
const response = await (0, http_1.http)(requestOptions, {
|
|
16
|
-
url: `${endpoint}/${address}`,
|
|
17
|
-
responseType: 'json',
|
|
18
|
-
});
|
|
19
|
-
return response.data;
|
|
20
|
-
}
|
|
21
|
-
exports.checkIfChunkExistsLocally = checkIfChunkExistsLocally;
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.retrieveExtendedTag = void 0;
|
|
4
|
-
const http_1 = require("../../utils/http");
|
|
5
|
-
const endpoint = 'tags';
|
|
6
|
-
/**
|
|
7
|
-
* Retrieve tag with extended information from Bee node
|
|
8
|
-
*
|
|
9
|
-
* @param requestOptions Options for making requests
|
|
10
|
-
* @param uid UID of tag to be retrieved
|
|
11
|
-
*/
|
|
12
|
-
async function retrieveExtendedTag(requestOptions, uid) {
|
|
13
|
-
const response = await (0, http_1.http)(requestOptions, {
|
|
14
|
-
url: `${endpoint}/${uid}`,
|
|
15
|
-
responseType: 'json',
|
|
16
|
-
});
|
|
17
|
-
return response.data;
|
|
18
|
-
}
|
|
19
|
-
exports.retrieveExtendedTag = retrieveExtendedTag;
|
package/dist/cjs/utils/eth.js
DELETED
|
@@ -1,216 +0,0 @@
|
|
|
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.makeEthereumWalletSigner = exports.ethToSwarmAddress = exports.fromLittleEndian = exports.toLittleEndian = exports.isHexEthAddress = exports.makeHexEthAddress = exports.makeEthAddress = exports.capitalizeAddressERC55 = void 0;
|
|
7
|
-
// For ESM compatibility
|
|
8
|
-
const js_sha3_1 = __importDefault(require("js-sha3"));
|
|
9
|
-
const { keccak256, sha3_256 } = js_sha3_1.default;
|
|
10
|
-
const bytes_1 = require("./bytes");
|
|
11
|
-
const hex_1 = require("./hex");
|
|
12
|
-
const ETH_ADDR_BYTES_LENGTH = 20;
|
|
13
|
-
const ETH_ADDR_HEX_LENGTH = 40;
|
|
14
|
-
function capitalizeAddressERC55(address) {
|
|
15
|
-
if (address.startsWith('0x')) {
|
|
16
|
-
address = address.slice(2);
|
|
17
|
-
}
|
|
18
|
-
const addressHash = keccak256(address.toLowerCase());
|
|
19
|
-
let result = '0x';
|
|
20
|
-
for (let i = 0; i < address.length; i++) {
|
|
21
|
-
if (parseInt(addressHash[i], 16) > 7) {
|
|
22
|
-
result += address[i].toUpperCase();
|
|
23
|
-
}
|
|
24
|
-
else {
|
|
25
|
-
result += address[i].toLowerCase();
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
return result;
|
|
29
|
-
}
|
|
30
|
-
exports.capitalizeAddressERC55 = capitalizeAddressERC55;
|
|
31
|
-
function makeEthAddress(address) {
|
|
32
|
-
if (typeof address === 'string') {
|
|
33
|
-
const hexAddr = (0, hex_1.makeHexString)(address, ETH_ADDR_HEX_LENGTH);
|
|
34
|
-
const ownerBytes = (0, hex_1.hexToBytes)(hexAddr);
|
|
35
|
-
(0, bytes_1.assertBytes)(ownerBytes, ETH_ADDR_BYTES_LENGTH);
|
|
36
|
-
return ownerBytes;
|
|
37
|
-
}
|
|
38
|
-
else if (address instanceof Uint8Array) {
|
|
39
|
-
(0, bytes_1.assertBytes)(address, ETH_ADDR_BYTES_LENGTH);
|
|
40
|
-
return address;
|
|
41
|
-
}
|
|
42
|
-
throw new TypeError('Invalid EthAddress');
|
|
43
|
-
}
|
|
44
|
-
exports.makeEthAddress = makeEthAddress;
|
|
45
|
-
function makeHexEthAddress(address) {
|
|
46
|
-
try {
|
|
47
|
-
return (0, hex_1.makeHexString)(address, ETH_ADDR_HEX_LENGTH);
|
|
48
|
-
}
|
|
49
|
-
catch (e) {
|
|
50
|
-
if (e instanceof TypeError) {
|
|
51
|
-
e.message = `Invalid HexEthAddress: ${e.message}`;
|
|
52
|
-
}
|
|
53
|
-
throw e;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
exports.makeHexEthAddress = makeHexEthAddress;
|
|
57
|
-
/**
|
|
58
|
-
* Check if this is all caps or small caps eth address (=address without checksum)
|
|
59
|
-
*
|
|
60
|
-
* @param address Ethereum address as hex string
|
|
61
|
-
*/
|
|
62
|
-
function isEthAddrCaseIns(address) {
|
|
63
|
-
// Check it's string, all small caps or all all caps hex and 40 chars long without the `0x` prefix
|
|
64
|
-
return (typeof address === 'string' && (/^(0x|0X)?[0-9a-f]{40}$/.test(address) || /^(0x|0X)?[0-9A-F]{40}$/.test(address)));
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Check if this is checksummed ethereum address
|
|
68
|
-
*
|
|
69
|
-
* @param address Ethereum address as hex string
|
|
70
|
-
*/
|
|
71
|
-
function isValidChecksummedEthAddress(address) {
|
|
72
|
-
try {
|
|
73
|
-
// Check for valid case insensitive hex type string, 40 chars
|
|
74
|
-
const addr = (0, hex_1.makeHexString)(address, ETH_ADDR_HEX_LENGTH);
|
|
75
|
-
// Check the checksum
|
|
76
|
-
const addressHash = keccak256(addr.toLowerCase());
|
|
77
|
-
for (let i = 0; i < 40; i += 1) {
|
|
78
|
-
// the nth letter should be uppercase if the nth digit of casemap is 1
|
|
79
|
-
if ((parseInt(addressHash[i], 16) > 7 && addr[i].toUpperCase() !== addr[i]) ||
|
|
80
|
-
(parseInt(addressHash[i], 16) <= 7 && addr[i].toLowerCase() !== addr[i])) {
|
|
81
|
-
return false;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
return true;
|
|
85
|
-
}
|
|
86
|
-
catch (e) {
|
|
87
|
-
if (e instanceof TypeError) {
|
|
88
|
-
return false;
|
|
89
|
-
}
|
|
90
|
-
throw e;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
/**
|
|
94
|
-
* Check if is valid ethereum address
|
|
95
|
-
*
|
|
96
|
-
* Pretty much typed version from web3js
|
|
97
|
-
* https://github.com/ChainSafe/web3.js/blob/1.x/packages/web3-utils/src/utils.js
|
|
98
|
-
*
|
|
99
|
-
* @param address Ethereum address as hex string
|
|
100
|
-
*
|
|
101
|
-
* @return True if is valid eth address
|
|
102
|
-
*/
|
|
103
|
-
function isHexEthAddress(address) {
|
|
104
|
-
return isEthAddrCaseIns(address) || isValidChecksummedEthAddress(address);
|
|
105
|
-
}
|
|
106
|
-
exports.isHexEthAddress = isHexEthAddress;
|
|
107
|
-
/**
|
|
108
|
-
* Convert big-endian hex or number to little-endian.
|
|
109
|
-
* Note: Before conversion it is automatically padded to even length hexstring
|
|
110
|
-
*
|
|
111
|
-
* @param bigEndian Big-endian hex string or number to convert
|
|
112
|
-
* @param pad Length to which the string should be padded before conversion (defaul: 2)
|
|
113
|
-
*
|
|
114
|
-
* @return little-endian encoded hexstring
|
|
115
|
-
*/
|
|
116
|
-
function toLittleEndian(bigEndian, pad = 2) {
|
|
117
|
-
if (!(Number.isInteger(pad) && pad >= 2 && pad % 2 === 0)) {
|
|
118
|
-
throw new TypeError('minimal padding for conversion needs to be positive even integer');
|
|
119
|
-
}
|
|
120
|
-
let hexRep;
|
|
121
|
-
if (typeof bigEndian === 'string')
|
|
122
|
-
hexRep = (0, hex_1.makeHexString)(bigEndian);
|
|
123
|
-
else if (typeof bigEndian === 'number')
|
|
124
|
-
hexRep = (0, hex_1.intToHex)(bigEndian);
|
|
125
|
-
else
|
|
126
|
-
throw new TypeError('incorrect input type');
|
|
127
|
-
hexRep = hexRep.padStart(pad, '0');
|
|
128
|
-
// Extend to an even length hexstring
|
|
129
|
-
if (hexRep.length % 2 !== 0)
|
|
130
|
-
hexRep = hexRep.padStart(hexRep.length + 1, '0');
|
|
131
|
-
// Match all two pairs in the hexstring, reverse the pairs and join it again
|
|
132
|
-
const littleEndian = hexRep.match(/../g)?.reverse().join('');
|
|
133
|
-
if (littleEndian)
|
|
134
|
-
return littleEndian;
|
|
135
|
-
throw new Error('failed to convert');
|
|
136
|
-
}
|
|
137
|
-
exports.toLittleEndian = toLittleEndian;
|
|
138
|
-
/**
|
|
139
|
-
* Convert little-endian hex or number to big-endian
|
|
140
|
-
* Note: Before conversion it is automatically padded to even length hexstring
|
|
141
|
-
*
|
|
142
|
-
* @param littleEndian Little-endian hex string or number to convert
|
|
143
|
-
* @param pad Length to which the string should be padded before conversion (defaul: 2)
|
|
144
|
-
*
|
|
145
|
-
* @return big-endian encoded hexstring
|
|
146
|
-
*/
|
|
147
|
-
function fromLittleEndian(littleEndian, pad = 2) {
|
|
148
|
-
// It's a reversible function
|
|
149
|
-
return toLittleEndian(littleEndian, pad);
|
|
150
|
-
}
|
|
151
|
-
exports.fromLittleEndian = fromLittleEndian;
|
|
152
|
-
function assertEthAddress(ethAddress) {
|
|
153
|
-
if (!isHexEthAddress(ethAddress))
|
|
154
|
-
throw new TypeError('invalid ETH address');
|
|
155
|
-
}
|
|
156
|
-
function assertSwarmNetworkId(networkId) {
|
|
157
|
-
if (Number.isInteger(networkId && networkId > 0 && networkId < Number.MAX_SAFE_INTEGER)) {
|
|
158
|
-
throw new TypeError('swarm network id must be positive integer');
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
/**
|
|
162
|
-
* Get swarm overlay address from public ethereum address and swarm network id
|
|
163
|
-
*
|
|
164
|
-
* @param ethAddress Public ethereum address
|
|
165
|
-
* @param networkId Swarm network id
|
|
166
|
-
*
|
|
167
|
-
* @return Swarm overlay address
|
|
168
|
-
*/
|
|
169
|
-
function ethToSwarmAddress(ethAddress, networkId = 1) {
|
|
170
|
-
assertEthAddress(ethAddress);
|
|
171
|
-
assertSwarmNetworkId(networkId);
|
|
172
|
-
const hex = `${(0, hex_1.makeHexString)(ethAddress)}${toLittleEndian(networkId, 16)}`;
|
|
173
|
-
(0, hex_1.assertHexString)(hex);
|
|
174
|
-
const overlayAddress = sha3_256((0, hex_1.hexToBytes)(hex));
|
|
175
|
-
return overlayAddress;
|
|
176
|
-
}
|
|
177
|
-
exports.ethToSwarmAddress = ethToSwarmAddress;
|
|
178
|
-
/**
|
|
179
|
-
* Function that takes Ethereum EIP-1193 compatible provider and create an Signer instance that
|
|
180
|
-
* uses `personal_sign` method to sign requested data.
|
|
181
|
-
*
|
|
182
|
-
* @param provider Injected web3 provider like window.ethereum or other compatible with EIP-1193
|
|
183
|
-
* @param ethAddress Optional address of the account which the data should be signed with. If not specified `eth_requestAccounts` request is used to get the account address.
|
|
184
|
-
*/
|
|
185
|
-
async function makeEthereumWalletSigner(provider, ethAddress) {
|
|
186
|
-
let executorFnc;
|
|
187
|
-
if (typeof provider !== 'object' || provider === null) {
|
|
188
|
-
throw new TypeError('We need JsonRPC provider object!');
|
|
189
|
-
}
|
|
190
|
-
if (provider.request) {
|
|
191
|
-
executorFnc = provider.request;
|
|
192
|
-
}
|
|
193
|
-
else if (provider.sendAsync) {
|
|
194
|
-
executorFnc = provider.sendAsync;
|
|
195
|
-
}
|
|
196
|
-
else {
|
|
197
|
-
throw new Error('Incompatible interface of given provider!');
|
|
198
|
-
}
|
|
199
|
-
if (!ethAddress) {
|
|
200
|
-
ethAddress = (await executorFnc({ method: 'eth_requestAccounts' }))[0];
|
|
201
|
-
}
|
|
202
|
-
const bytesEthAddress = makeEthAddress(ethAddress);
|
|
203
|
-
const hexEthAddress = makeHexEthAddress(ethAddress);
|
|
204
|
-
return {
|
|
205
|
-
address: bytesEthAddress,
|
|
206
|
-
sign: async (data) => {
|
|
207
|
-
const result = await executorFnc({
|
|
208
|
-
jsonrpc: '2.0',
|
|
209
|
-
method: 'personal_sign',
|
|
210
|
-
params: ['0x' + data.hex(), '0x' + hexEthAddress],
|
|
211
|
-
});
|
|
212
|
-
return result;
|
|
213
|
-
},
|
|
214
|
-
};
|
|
215
|
-
}
|
|
216
|
-
exports.makeEthereumWalletSigner = makeEthereumWalletSigner;
|
package/dist/cjs/utils/hash.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
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.keccak256Hash = void 0;
|
|
7
|
-
// For ESM compatibility
|
|
8
|
-
const js_sha3_1 = __importDefault(require("js-sha3"));
|
|
9
|
-
const { keccak256 } = js_sha3_1.default;
|
|
10
|
-
/**
|
|
11
|
-
* Helper function for calculating the keccak256 hash with
|
|
12
|
-
* correct types.
|
|
13
|
-
*
|
|
14
|
-
* @param messages Any number of messages (strings, byte arrays etc.)
|
|
15
|
-
*/
|
|
16
|
-
function keccak256Hash(...messages) {
|
|
17
|
-
const hasher = keccak256.create();
|
|
18
|
-
messages.forEach(bytes => hasher.update(bytes));
|
|
19
|
-
return Uint8Array.from(hasher.digest());
|
|
20
|
-
}
|
|
21
|
-
exports.keccak256Hash = keccak256Hash;
|
package/dist/cjs/utils/hex.js
DELETED
|
@@ -1,150 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.assertPrefixedHexString = exports.assertHexString = exports.isPrefixedHexString = exports.isHexString = exports.intToHex = exports.bytesToHex = exports.hexToBytes = exports.makeHexString = void 0;
|
|
4
|
-
const bytes_1 = require("./bytes");
|
|
5
|
-
/**
|
|
6
|
-
* Creates unprefixed hex string from wide range of data.
|
|
7
|
-
*
|
|
8
|
-
* TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
|
|
9
|
-
*
|
|
10
|
-
* @param input
|
|
11
|
-
* @param len of the resulting HexString WITHOUT prefix!
|
|
12
|
-
*/
|
|
13
|
-
function makeHexString(input, len) {
|
|
14
|
-
if (typeof input === 'number') {
|
|
15
|
-
return intToHex(input, len);
|
|
16
|
-
}
|
|
17
|
-
if (input instanceof Uint8Array) {
|
|
18
|
-
return bytesToHex(input, len);
|
|
19
|
-
}
|
|
20
|
-
if (typeof input === 'string') {
|
|
21
|
-
if (isPrefixedHexString(input)) {
|
|
22
|
-
const hex = input.slice(2);
|
|
23
|
-
if (len && hex.length !== len) {
|
|
24
|
-
throw new TypeError(`Length mismatch for valid hex string. Expecting length ${len}: ${hex}`);
|
|
25
|
-
}
|
|
26
|
-
return hex;
|
|
27
|
-
}
|
|
28
|
-
else {
|
|
29
|
-
// We use assertHexString() as there might be more reasons why a string is not valid hex string
|
|
30
|
-
// and usage of isHexString() would not give enough information to the user on what is going
|
|
31
|
-
// wrong.
|
|
32
|
-
assertHexString(input, len);
|
|
33
|
-
return input;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
throw new TypeError('Not HexString compatible type!');
|
|
37
|
-
}
|
|
38
|
-
exports.makeHexString = makeHexString;
|
|
39
|
-
/**
|
|
40
|
-
* Converts a hex string to Uint8Array
|
|
41
|
-
*
|
|
42
|
-
* @param hex string input without 0x prefix!
|
|
43
|
-
*/
|
|
44
|
-
function hexToBytes(hex) {
|
|
45
|
-
assertHexString(hex);
|
|
46
|
-
const bytes = (0, bytes_1.makeBytes)(hex.length / 2);
|
|
47
|
-
for (let i = 0; i < bytes.length; i++) {
|
|
48
|
-
const hexByte = hex.substr(i * 2, 2);
|
|
49
|
-
bytes[i] = parseInt(hexByte, 16);
|
|
50
|
-
}
|
|
51
|
-
return bytes;
|
|
52
|
-
}
|
|
53
|
-
exports.hexToBytes = hexToBytes;
|
|
54
|
-
/**
|
|
55
|
-
* Converts array of number or Uint8Array to HexString without prefix.
|
|
56
|
-
*
|
|
57
|
-
* @param bytes The input array
|
|
58
|
-
* @param len The length of the non prefixed HexString
|
|
59
|
-
*/
|
|
60
|
-
function bytesToHex(bytes, len) {
|
|
61
|
-
const hexByte = (n) => n.toString(16).padStart(2, '0');
|
|
62
|
-
const hex = Array.from(bytes, hexByte).join('');
|
|
63
|
-
// TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
|
|
64
|
-
if (len && hex.length !== len) {
|
|
65
|
-
throw new TypeError(`Resulting HexString does not have expected length ${len}: ${hex}`);
|
|
66
|
-
}
|
|
67
|
-
return hex;
|
|
68
|
-
}
|
|
69
|
-
exports.bytesToHex = bytesToHex;
|
|
70
|
-
/**
|
|
71
|
-
* Converts integer number to hex string.
|
|
72
|
-
*
|
|
73
|
-
* Optionally provides '0x' prefix or padding
|
|
74
|
-
*
|
|
75
|
-
* @param int The positive integer to be converted
|
|
76
|
-
* @param len The length of the non prefixed HexString
|
|
77
|
-
*/
|
|
78
|
-
function intToHex(int, len) {
|
|
79
|
-
if (!Number.isInteger(int))
|
|
80
|
-
throw new TypeError('the value provided is not integer');
|
|
81
|
-
if (int > Number.MAX_SAFE_INTEGER)
|
|
82
|
-
throw new TypeError('the value provided exceeds safe integer');
|
|
83
|
-
if (int < 0)
|
|
84
|
-
throw new TypeError('the value provided is a negative integer');
|
|
85
|
-
const hex = int.toString(16);
|
|
86
|
-
// TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
|
|
87
|
-
if (len && hex.length !== len) {
|
|
88
|
-
throw new TypeError(`Resulting HexString does not have expected length ${len}: ${hex}`);
|
|
89
|
-
}
|
|
90
|
-
return hex;
|
|
91
|
-
}
|
|
92
|
-
exports.intToHex = intToHex;
|
|
93
|
-
/**
|
|
94
|
-
* Type guard for HexStrings.
|
|
95
|
-
* Requires no 0x prefix!
|
|
96
|
-
*
|
|
97
|
-
* TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
|
|
98
|
-
*
|
|
99
|
-
* @param s string input
|
|
100
|
-
* @param len expected length of the HexString
|
|
101
|
-
*/
|
|
102
|
-
function isHexString(s, len) {
|
|
103
|
-
return typeof s === 'string' && /^[0-9a-f]+$/i.test(s) && (!len || s.length === len);
|
|
104
|
-
}
|
|
105
|
-
exports.isHexString = isHexString;
|
|
106
|
-
/**
|
|
107
|
-
* Type guard for PrefixedHexStrings.
|
|
108
|
-
* Does enforce presence of 0x prefix!
|
|
109
|
-
*
|
|
110
|
-
* @param s string input
|
|
111
|
-
*/
|
|
112
|
-
function isPrefixedHexString(s) {
|
|
113
|
-
return typeof s === 'string' && /^0x[0-9a-f]+$/i.test(s);
|
|
114
|
-
}
|
|
115
|
-
exports.isPrefixedHexString = isPrefixedHexString;
|
|
116
|
-
/**
|
|
117
|
-
* Verifies if the provided input is a HexString.
|
|
118
|
-
*
|
|
119
|
-
* TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
|
|
120
|
-
*
|
|
121
|
-
* @param s string input
|
|
122
|
-
* @param len expected length of the HexString
|
|
123
|
-
* @param name optional name for the asserted value
|
|
124
|
-
* @returns HexString or throws error
|
|
125
|
-
*/
|
|
126
|
-
function assertHexString(s, len, name = 'value') {
|
|
127
|
-
if (!isHexString(s, len)) {
|
|
128
|
-
if (isPrefixedHexString(s)) {
|
|
129
|
-
throw new TypeError(`${name} not valid non prefixed hex string (has 0x prefix): ${s}`);
|
|
130
|
-
}
|
|
131
|
-
// Don't display length error if no length specified in order not to confuse user
|
|
132
|
-
const lengthMsg = len ? ` of length ${len}` : '';
|
|
133
|
-
throw new TypeError(`${name} not valid hex string${lengthMsg}: ${s}`);
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
exports.assertHexString = assertHexString;
|
|
137
|
-
/**
|
|
138
|
-
* Verifies if the provided input is a PrefixedHexString.
|
|
139
|
-
*
|
|
140
|
-
* @param s string input
|
|
141
|
-
* @param len expected length of the HexString
|
|
142
|
-
* @param name optional name for the asserted value
|
|
143
|
-
* @returns HexString or throws error
|
|
144
|
-
*/
|
|
145
|
-
function assertPrefixedHexString(s, name = 'value') {
|
|
146
|
-
if (!isPrefixedHexString(s)) {
|
|
147
|
-
throw new TypeError(`${name} not valid prefixed hex string: ${s}`);
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
exports.assertPrefixedHexString = assertPrefixedHexString;
|