@ethersphere/bee-js 6.0.0-pre.0 → 6.0.0-pre.10
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 +18 -17
- package/dist/cjs/bee-debug.js +572 -0
- package/dist/cjs/bee.js +876 -0
- package/dist/cjs/chunk/bmt.js +55 -0
- package/dist/cjs/chunk/cac.js +56 -0
- package/dist/cjs/chunk/serialize.js +19 -0
- package/dist/cjs/chunk/signer.js +126 -0
- package/dist/cjs/chunk/soc.js +159 -0
- package/dist/cjs/chunk/span.js +29 -0
- package/dist/cjs/feed/identifier.js +35 -0
- package/dist/cjs/feed/index.js +116 -0
- package/dist/cjs/feed/json.js +28 -0
- package/dist/cjs/feed/retrievable.js +58 -0
- package/dist/cjs/feed/topic.js +25 -0
- package/dist/cjs/feed/type.js +15 -0
- package/dist/cjs/index.js +39 -0
- package/dist/cjs/modules/bytes.js +61 -0
- package/dist/cjs/modules/bzz.js +125 -0
- package/dist/cjs/modules/chunk.js +48 -0
- package/dist/cjs/modules/debug/balance.js +60 -0
- package/dist/cjs/modules/debug/chequebook.js +142 -0
- package/dist/cjs/modules/debug/chunk.js +38 -0
- package/dist/cjs/modules/debug/connectivity.js +54 -0
- package/dist/cjs/modules/debug/settlements.js +32 -0
- package/dist/cjs/modules/debug/stake.js +57 -0
- package/dist/cjs/modules/debug/stamps.js +68 -0
- package/dist/cjs/modules/debug/states.js +64 -0
- package/dist/cjs/modules/debug/status.js +148 -0
- package/dist/cjs/modules/debug/tag.js +19 -0
- package/dist/cjs/modules/debug/transactions.js +64 -0
- package/dist/cjs/modules/feed.js +66 -0
- package/dist/cjs/modules/pinning.js +63 -0
- package/dist/cjs/modules/pss.js +43 -0
- package/dist/cjs/modules/soc.js +32 -0
- package/dist/cjs/modules/status.js +15 -0
- package/dist/cjs/modules/stewardship.js +28 -0
- package/dist/cjs/modules/tag.js +77 -0
- package/dist/cjs/package.json +8 -0
- package/dist/cjs/types/debug.js +10 -0
- package/dist/cjs/types/index.js +51 -0
- package/dist/cjs/utils/bytes.js +121 -0
- package/dist/cjs/utils/collection.browser.js +23 -0
- package/dist/cjs/utils/collection.js +59 -0
- package/dist/cjs/utils/collection.node.js +71 -0
- package/dist/cjs/utils/data.browser.js +61 -0
- package/dist/cjs/utils/data.js +45 -0
- package/dist/cjs/utils/error.js +16 -0
- package/dist/cjs/utils/eth.js +199 -0
- package/dist/cjs/utils/expose.js +44 -0
- package/dist/cjs/utils/file.js +38 -0
- package/dist/cjs/utils/hash.js +21 -0
- package/dist/cjs/utils/headers.js +59 -0
- package/dist/cjs/utils/hex.js +150 -0
- package/dist/cjs/utils/http.js +40 -0
- package/dist/cjs/utils/pss.js +18 -0
- package/dist/cjs/utils/reference.js +36 -0
- package/dist/cjs/utils/stamps.js +17 -0
- package/dist/cjs/utils/stream.js +146 -0
- package/dist/cjs/utils/tar.js +25 -0
- package/dist/cjs/utils/type.js +434 -0
- package/dist/cjs/utils/uint64.js +29 -0
- package/dist/cjs/utils/url.js +56 -0
- package/dist/index.browser.min.js +3 -0
- package/dist/index.browser.min.js.LICENSE.txt +50 -0
- package/dist/index.browser.min.js.map +1 -0
- package/dist/{src → mjs}/bee-debug.js +102 -183
- package/dist/{src → mjs}/bee.js +82 -232
- package/dist/{src → mjs}/chunk/bmt.js +4 -10
- package/dist/{src → mjs}/chunk/cac.js +0 -3
- package/dist/{src → mjs}/chunk/signer.js +1 -18
- package/dist/{src → mjs}/chunk/soc.js +17 -29
- package/dist/{src → mjs}/chunk/span.js +2 -5
- package/dist/{src → mjs}/feed/identifier.js +0 -6
- package/dist/{src → mjs}/feed/index.js +24 -27
- package/dist/{src → mjs}/feed/json.js +2 -5
- package/dist/{src → mjs}/feed/retrievable.js +7 -21
- package/dist/{src → mjs}/feed/topic.js +0 -2
- package/dist/{src → mjs}/index.js +2 -2
- package/dist/{src → mjs}/modules/bytes.js +16 -20
- package/dist/{src → mjs}/modules/bzz.js +33 -40
- package/dist/{src → mjs}/modules/chunk.js +11 -13
- package/dist/mjs/modules/debug/balance.js +53 -0
- package/dist/mjs/modules/debug/chequebook.js +135 -0
- package/dist/{src → mjs}/modules/debug/chunk.js +10 -12
- package/dist/mjs/modules/debug/connectivity.js +45 -0
- package/dist/mjs/modules/debug/settlements.js +27 -0
- package/dist/mjs/modules/debug/stake.js +51 -0
- package/dist/mjs/modules/debug/stamps.js +61 -0
- package/dist/mjs/modules/debug/states.js +58 -0
- package/dist/{src → mjs}/modules/debug/status.js +46 -37
- package/dist/mjs/modules/debug/tag.js +15 -0
- package/dist/mjs/modules/debug/transactions.js +59 -0
- package/dist/{src → mjs}/modules/feed.js +15 -21
- package/dist/mjs/modules/pinning.js +56 -0
- package/dist/{src → mjs}/modules/pss.js +7 -10
- package/dist/{src → mjs}/modules/soc.js +6 -7
- package/dist/mjs/modules/status.js +11 -0
- package/dist/mjs/modules/stewardship.js +23 -0
- package/dist/mjs/modules/tag.js +72 -0
- package/dist/mjs/package.json +8 -0
- package/dist/{src → mjs}/types/debug.js +0 -1
- package/dist/{src → mjs}/types/index.js +0 -4
- package/dist/{src → mjs}/utils/bytes.js +8 -17
- package/dist/{src/utils/collection-browser.js → mjs/utils/collection.browser.js} +0 -1
- package/dist/{src → mjs}/utils/collection.js +0 -12
- package/dist/{src/utils/collection-node.js → mjs/utils/collection.node.js} +0 -13
- package/dist/{src → mjs}/utils/data.browser.js +2 -11
- package/dist/{src → mjs}/utils/data.js +0 -7
- package/dist/mjs/utils/error.js +11 -0
- package/dist/{src → mjs}/utils/eth.js +6 -28
- package/dist/{src → mjs}/utils/expose.js +1 -1
- package/dist/{src → mjs}/utils/file.js +4 -9
- package/dist/{src → mjs}/utils/hash.js +0 -1
- package/dist/{src → mjs}/utils/headers.js +5 -13
- package/dist/{src → mjs}/utils/hex.js +6 -25
- package/dist/mjs/utils/http.js +34 -0
- package/dist/{src → mjs}/utils/pss.js +0 -2
- package/dist/{src → mjs}/utils/reference.js +2 -5
- package/dist/{src → mjs}/utils/stream.js +3 -26
- package/dist/{src → mjs}/utils/tar.js +3 -6
- package/dist/{src → mjs}/utils/type.js +23 -80
- package/dist/{src → mjs}/utils/uint64.js +0 -1
- package/dist/{src → mjs}/utils/url.js +2 -10
- package/dist/types/bee-debug.d.ts +71 -46
- package/dist/types/bee.d.ts +34 -58
- package/dist/types/chunk/bmt.d.ts +1 -1
- package/dist/types/chunk/cac.d.ts +1 -1
- package/dist/types/chunk/soc.d.ts +9 -9
- package/dist/types/chunk/span.d.ts +1 -1
- package/dist/types/feed/index.d.ts +10 -10
- package/dist/types/feed/json.d.ts +2 -2
- package/dist/types/feed/retrievable.d.ts +2 -2
- package/dist/types/feed/type.d.ts +1 -1
- package/dist/types/index.d.ts +13 -2
- package/dist/types/modules/bytes.d.ts +5 -5
- package/dist/types/modules/bzz.d.ts +8 -8
- package/dist/types/modules/chunk.d.ts +4 -4
- package/dist/types/modules/debug/balance.d.ts +9 -9
- package/dist/types/modules/debug/chequebook.d.ts +17 -17
- package/dist/types/modules/debug/chunk.d.ts +5 -5
- package/dist/types/modules/debug/connectivity.d.ts +7 -7
- package/dist/types/modules/debug/settlements.d.ts +5 -5
- package/dist/types/modules/debug/stake.d.ts +21 -0
- package/dist/types/modules/debug/stamps.d.ts +7 -7
- package/dist/types/modules/debug/states.d.ts +7 -7
- package/dist/types/modules/debug/status.d.ts +21 -15
- package/dist/types/modules/debug/tag.d.ts +3 -3
- package/dist/types/modules/debug/transactions.d.ts +5 -5
- package/dist/types/modules/feed.d.ts +3 -3
- package/dist/types/modules/pinning.d.ts +7 -7
- package/dist/types/modules/pss.d.ts +3 -3
- package/dist/types/modules/soc.d.ts +2 -2
- package/dist/types/modules/status.d.ts +3 -3
- package/dist/types/modules/stewardship.d.ts +3 -3
- package/dist/types/modules/tag.d.ts +6 -6
- package/dist/types/types/debug.d.ts +35 -2
- package/dist/types/types/index.d.ts +48 -89
- package/dist/types/utils/error.d.ts +0 -26
- package/dist/types/utils/eth.d.ts +6 -6
- package/dist/types/utils/expose.d.ts +4 -7
- package/dist/types/utils/hash.d.ts +1 -1
- package/dist/types/utils/headers.d.ts +1 -1
- package/dist/types/utils/hex.d.ts +4 -4
- package/dist/types/utils/http.d.ts +11 -21
- package/dist/types/utils/stream.d.ts +1 -1
- package/dist/types/utils/type.d.ts +3 -2
- package/dist/types/utils/uint64.d.ts +1 -1
- package/package.json +59 -52
- package/dist/index.browser.js +0 -33
- package/dist/index.browser.js.map +0 -7
- package/dist/src/modules/debug/balance.js +0 -57
- package/dist/src/modules/debug/chequebook.js +0 -150
- package/dist/src/modules/debug/connectivity.js +0 -45
- package/dist/src/modules/debug/settlements.js +0 -29
- package/dist/src/modules/debug/stamps.js +0 -64
- package/dist/src/modules/debug/states.js +0 -46
- package/dist/src/modules/debug/tag.js +0 -16
- package/dist/src/modules/debug/transactions.js +0 -69
- package/dist/src/modules/pinning.js +0 -60
- package/dist/src/modules/status.js +0 -12
- package/dist/src/modules/stewardship.js +0 -24
- package/dist/src/modules/tag.js +0 -77
- package/dist/src/utils/error.js +0 -47
- package/dist/src/utils/http.js +0 -162
- package/dist/src/utils/merge.js +0 -36
- package/dist/src/utils/sleep.js +0 -8
- package/dist/types/utils/merge.d.ts +0 -8
- package/dist/types/utils/sleep.d.ts +0 -6
- /package/dist/{src → mjs}/chunk/serialize.js +0 -0
- /package/dist/{src → mjs}/feed/type.js +0 -0
- /package/dist/{src → mjs}/utils/stamps.js +0 -0
- /package/dist/types/utils/{collection-browser.d.ts → collection.browser.d.ts} +0 -0
- /package/dist/types/utils/{collection-node.d.ts → collection.node.d.ts} +0 -0
|
@@ -0,0 +1,199 @@
|
|
|
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 = 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 hex_1 = require("./hex");
|
|
11
|
+
const bytes_1 = require("./bytes");
|
|
12
|
+
const ETH_ADDR_BYTES_LENGTH = 20;
|
|
13
|
+
const ETH_ADDR_HEX_LENGTH = 40;
|
|
14
|
+
function makeEthAddress(address) {
|
|
15
|
+
if (typeof address === 'string') {
|
|
16
|
+
const hexAddr = (0, hex_1.makeHexString)(address, ETH_ADDR_HEX_LENGTH);
|
|
17
|
+
const ownerBytes = (0, hex_1.hexToBytes)(hexAddr);
|
|
18
|
+
(0, bytes_1.assertBytes)(ownerBytes, ETH_ADDR_BYTES_LENGTH);
|
|
19
|
+
return ownerBytes;
|
|
20
|
+
}
|
|
21
|
+
else if (address instanceof Uint8Array) {
|
|
22
|
+
(0, bytes_1.assertBytes)(address, ETH_ADDR_BYTES_LENGTH);
|
|
23
|
+
return address;
|
|
24
|
+
}
|
|
25
|
+
throw new TypeError('Invalid EthAddress');
|
|
26
|
+
}
|
|
27
|
+
exports.makeEthAddress = makeEthAddress;
|
|
28
|
+
function makeHexEthAddress(address) {
|
|
29
|
+
try {
|
|
30
|
+
return (0, hex_1.makeHexString)(address, ETH_ADDR_HEX_LENGTH);
|
|
31
|
+
}
|
|
32
|
+
catch (e) {
|
|
33
|
+
if (e instanceof TypeError) {
|
|
34
|
+
e.message = `Invalid HexEthAddress: ${e.message}`;
|
|
35
|
+
}
|
|
36
|
+
throw e;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
exports.makeHexEthAddress = makeHexEthAddress;
|
|
40
|
+
/**
|
|
41
|
+
* Check if this is all caps or small caps eth address (=address without checksum)
|
|
42
|
+
*
|
|
43
|
+
* @param address Ethereum address as hex string
|
|
44
|
+
*/
|
|
45
|
+
function isEthAddrCaseIns(address) {
|
|
46
|
+
// Check it's string, all small caps or all all caps hex and 40 chars long without the `0x` prefix
|
|
47
|
+
return (typeof address === 'string' && (/^(0x|0X)?[0-9a-f]{40}$/.test(address) || /^(0x|0X)?[0-9A-F]{40}$/.test(address)));
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Check if this is checksummed ethereum address
|
|
51
|
+
*
|
|
52
|
+
* @param address Ethereum address as hex string
|
|
53
|
+
*/
|
|
54
|
+
function isValidChecksummedEthAddress(address) {
|
|
55
|
+
try {
|
|
56
|
+
// Check for valid case insensitive hex type string, 40 chars
|
|
57
|
+
const addr = (0, hex_1.makeHexString)(address, ETH_ADDR_HEX_LENGTH);
|
|
58
|
+
// Check the checksum
|
|
59
|
+
const addressHash = keccak256(addr.toLowerCase());
|
|
60
|
+
for (let i = 0; i < 40; i += 1) {
|
|
61
|
+
// the nth letter should be uppercase if the nth digit of casemap is 1
|
|
62
|
+
if ((parseInt(addressHash[i], 16) > 7 && addr[i].toUpperCase() !== addr[i]) ||
|
|
63
|
+
(parseInt(addressHash[i], 16) <= 7 && addr[i].toLowerCase() !== addr[i])) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
catch (e) {
|
|
70
|
+
if (e instanceof TypeError) {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
throw e;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Check if is valid ethereum address
|
|
78
|
+
*
|
|
79
|
+
* Pretty much typed version from web3js
|
|
80
|
+
* https://github.com/ChainSafe/web3.js/blob/1.x/packages/web3-utils/src/utils.js
|
|
81
|
+
*
|
|
82
|
+
* @param address Ethereum address as hex string
|
|
83
|
+
*
|
|
84
|
+
* @return True if is valid eth address
|
|
85
|
+
*/
|
|
86
|
+
function isHexEthAddress(address) {
|
|
87
|
+
return isEthAddrCaseIns(address) || isValidChecksummedEthAddress(address);
|
|
88
|
+
}
|
|
89
|
+
exports.isHexEthAddress = isHexEthAddress;
|
|
90
|
+
/**
|
|
91
|
+
* Convert big-endian hex or number to little-endian.
|
|
92
|
+
* Note: Before conversion it is automatically padded to even length hexstring
|
|
93
|
+
*
|
|
94
|
+
* @param bigEndian Big-endian hex string or number to convert
|
|
95
|
+
* @param pad Length to which the string should be padded before conversion (defaul: 2)
|
|
96
|
+
*
|
|
97
|
+
* @return little-endian encoded hexstring
|
|
98
|
+
*/
|
|
99
|
+
function toLittleEndian(bigEndian, pad = 2) {
|
|
100
|
+
if (!(Number.isInteger(pad) && pad >= 2 && pad % 2 === 0)) {
|
|
101
|
+
throw new TypeError('minimal padding for conversion needs to be positive even integer');
|
|
102
|
+
}
|
|
103
|
+
let hexRep;
|
|
104
|
+
if (typeof bigEndian === 'string')
|
|
105
|
+
hexRep = (0, hex_1.makeHexString)(bigEndian);
|
|
106
|
+
else if (typeof bigEndian === 'number')
|
|
107
|
+
hexRep = (0, hex_1.intToHex)(bigEndian);
|
|
108
|
+
else
|
|
109
|
+
throw new TypeError('incorrect input type');
|
|
110
|
+
hexRep = hexRep.padStart(pad, '0');
|
|
111
|
+
// Extend to an even length hexstring
|
|
112
|
+
if (hexRep.length % 2 !== 0)
|
|
113
|
+
hexRep = hexRep.padStart(hexRep.length + 1, '0');
|
|
114
|
+
// Match all two pairs in the hexstring, reverse the pairs and join it again
|
|
115
|
+
const littleEndian = hexRep.match(/../g)?.reverse().join('');
|
|
116
|
+
if (littleEndian)
|
|
117
|
+
return littleEndian;
|
|
118
|
+
throw new Error('failed to convert');
|
|
119
|
+
}
|
|
120
|
+
exports.toLittleEndian = toLittleEndian;
|
|
121
|
+
/**
|
|
122
|
+
* Convert little-endian hex or number to big-endian
|
|
123
|
+
* Note: Before conversion it is automatically padded to even length hexstring
|
|
124
|
+
*
|
|
125
|
+
* @param littleEndian Little-endian hex string or number to convert
|
|
126
|
+
* @param pad Length to which the string should be padded before conversion (defaul: 2)
|
|
127
|
+
*
|
|
128
|
+
* @return big-endian encoded hexstring
|
|
129
|
+
*/
|
|
130
|
+
function fromLittleEndian(littleEndian, pad = 2) {
|
|
131
|
+
// It's a reversible function
|
|
132
|
+
return toLittleEndian(littleEndian, pad);
|
|
133
|
+
}
|
|
134
|
+
exports.fromLittleEndian = fromLittleEndian;
|
|
135
|
+
function assertEthAddress(ethAddress) {
|
|
136
|
+
if (!isHexEthAddress(ethAddress))
|
|
137
|
+
throw new TypeError('invalid ETH address');
|
|
138
|
+
}
|
|
139
|
+
function assertSwarmNetworkId(networkId) {
|
|
140
|
+
if (Number.isInteger(networkId && networkId > 0 && networkId < Number.MAX_SAFE_INTEGER)) {
|
|
141
|
+
throw new TypeError('swarm network id must be positive integer');
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Get swarm overlay address from public ethereum address and swarm network id
|
|
146
|
+
*
|
|
147
|
+
* @param ethAddress Public ethereum address
|
|
148
|
+
* @param networkId Swarm network id
|
|
149
|
+
*
|
|
150
|
+
* @return Swarm overlay address
|
|
151
|
+
*/
|
|
152
|
+
function ethToSwarmAddress(ethAddress, networkId = 1) {
|
|
153
|
+
assertEthAddress(ethAddress);
|
|
154
|
+
assertSwarmNetworkId(networkId);
|
|
155
|
+
const hex = `${(0, hex_1.makeHexString)(ethAddress)}${toLittleEndian(networkId, 16)}`;
|
|
156
|
+
(0, hex_1.assertHexString)(hex);
|
|
157
|
+
const overlayAddress = sha3_256((0, hex_1.hexToBytes)(hex));
|
|
158
|
+
return overlayAddress;
|
|
159
|
+
}
|
|
160
|
+
exports.ethToSwarmAddress = ethToSwarmAddress;
|
|
161
|
+
/**
|
|
162
|
+
* Function that takes Ethereum EIP-1193 compatible provider and create an Signer instance that
|
|
163
|
+
* uses `personal_sign` method to sign requested data.
|
|
164
|
+
*
|
|
165
|
+
* @param provider Injected web3 provider like window.ethereum or other compatible with EIP-1193
|
|
166
|
+
* @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.
|
|
167
|
+
*/
|
|
168
|
+
async function makeEthereumWalletSigner(provider, ethAddress) {
|
|
169
|
+
let executorFnc;
|
|
170
|
+
if (typeof provider !== 'object' || provider === null) {
|
|
171
|
+
throw new TypeError('We need JsonRPC provider object!');
|
|
172
|
+
}
|
|
173
|
+
if (provider.request) {
|
|
174
|
+
executorFnc = provider.request;
|
|
175
|
+
}
|
|
176
|
+
else if (provider.sendAsync) {
|
|
177
|
+
executorFnc = provider.sendAsync;
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
throw new Error('Incompatible interface of given provider!');
|
|
181
|
+
}
|
|
182
|
+
if (!ethAddress) {
|
|
183
|
+
ethAddress = (await executorFnc({ method: 'eth_requestAccounts' }))[0];
|
|
184
|
+
}
|
|
185
|
+
const bytesEthAddress = makeEthAddress(ethAddress);
|
|
186
|
+
const hexEthAddress = makeHexEthAddress(ethAddress);
|
|
187
|
+
return {
|
|
188
|
+
address: bytesEthAddress,
|
|
189
|
+
sign: async (data) => {
|
|
190
|
+
const result = await executorFnc({
|
|
191
|
+
jsonrpc: '2.0',
|
|
192
|
+
method: 'personal_sign',
|
|
193
|
+
params: ['0x' + data.hex(), '0x' + hexEthAddress],
|
|
194
|
+
});
|
|
195
|
+
return result;
|
|
196
|
+
},
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
exports.makeEthereumWalletSigner = makeEthereumWalletSigner;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getStampUsage = exports.makeMaxTarget = exports.keccak256Hash = exports.isReadable = exports.normalizeToReadableStream = exports.isNodeReadable = exports.isReadableStream = exports.readableNodeToWeb = exports.readableWebToNode = exports.makeEthereumWalletSigner = exports.fromLittleEndian = exports.toLittleEndian = exports.ethToSwarmAddress = exports.isHexEthAddress = exports.makeHexEthAddress = exports.makeEthAddress = exports.assertPrefixedHexString = exports.assertHexString = exports.isHexString = exports.intToHex = exports.bytesToHex = exports.hexToBytes = exports.makeHexString = exports.bytesEqual = exports.flexBytesAtOffset = exports.bytesAtOffset = exports.assertFlexBytes = exports.isFlexBytes = exports.assertBytes = exports.isBytes = exports.getFolderSize = exports.getCollectionSize = void 0;
|
|
4
|
+
var collection_1 = require("./collection");
|
|
5
|
+
Object.defineProperty(exports, "getCollectionSize", { enumerable: true, get: function () { return collection_1.getCollectionSize; } });
|
|
6
|
+
var collection_node_1 = require("./collection.node");
|
|
7
|
+
Object.defineProperty(exports, "getFolderSize", { enumerable: true, get: function () { return collection_node_1.getFolderSize; } });
|
|
8
|
+
var bytes_1 = require("./bytes");
|
|
9
|
+
Object.defineProperty(exports, "isBytes", { enumerable: true, get: function () { return bytes_1.isBytes; } });
|
|
10
|
+
Object.defineProperty(exports, "assertBytes", { enumerable: true, get: function () { return bytes_1.assertBytes; } });
|
|
11
|
+
Object.defineProperty(exports, "isFlexBytes", { enumerable: true, get: function () { return bytes_1.isFlexBytes; } });
|
|
12
|
+
Object.defineProperty(exports, "assertFlexBytes", { enumerable: true, get: function () { return bytes_1.assertFlexBytes; } });
|
|
13
|
+
Object.defineProperty(exports, "bytesAtOffset", { enumerable: true, get: function () { return bytes_1.bytesAtOffset; } });
|
|
14
|
+
Object.defineProperty(exports, "flexBytesAtOffset", { enumerable: true, get: function () { return bytes_1.flexBytesAtOffset; } });
|
|
15
|
+
Object.defineProperty(exports, "bytesEqual", { enumerable: true, get: function () { return bytes_1.bytesEqual; } });
|
|
16
|
+
var hex_1 = require("./hex");
|
|
17
|
+
Object.defineProperty(exports, "makeHexString", { enumerable: true, get: function () { return hex_1.makeHexString; } });
|
|
18
|
+
Object.defineProperty(exports, "hexToBytes", { enumerable: true, get: function () { return hex_1.hexToBytes; } });
|
|
19
|
+
Object.defineProperty(exports, "bytesToHex", { enumerable: true, get: function () { return hex_1.bytesToHex; } });
|
|
20
|
+
Object.defineProperty(exports, "intToHex", { enumerable: true, get: function () { return hex_1.intToHex; } });
|
|
21
|
+
Object.defineProperty(exports, "isHexString", { enumerable: true, get: function () { return hex_1.isHexString; } });
|
|
22
|
+
Object.defineProperty(exports, "assertHexString", { enumerable: true, get: function () { return hex_1.assertHexString; } });
|
|
23
|
+
Object.defineProperty(exports, "assertPrefixedHexString", { enumerable: true, get: function () { return hex_1.assertPrefixedHexString; } });
|
|
24
|
+
var eth_1 = require("./eth");
|
|
25
|
+
Object.defineProperty(exports, "makeEthAddress", { enumerable: true, get: function () { return eth_1.makeEthAddress; } });
|
|
26
|
+
Object.defineProperty(exports, "makeHexEthAddress", { enumerable: true, get: function () { return eth_1.makeHexEthAddress; } });
|
|
27
|
+
Object.defineProperty(exports, "isHexEthAddress", { enumerable: true, get: function () { return eth_1.isHexEthAddress; } });
|
|
28
|
+
Object.defineProperty(exports, "ethToSwarmAddress", { enumerable: true, get: function () { return eth_1.ethToSwarmAddress; } });
|
|
29
|
+
Object.defineProperty(exports, "toLittleEndian", { enumerable: true, get: function () { return eth_1.toLittleEndian; } });
|
|
30
|
+
Object.defineProperty(exports, "fromLittleEndian", { enumerable: true, get: function () { return eth_1.fromLittleEndian; } });
|
|
31
|
+
Object.defineProperty(exports, "makeEthereumWalletSigner", { enumerable: true, get: function () { return eth_1.makeEthereumWalletSigner; } });
|
|
32
|
+
var stream_1 = require("./stream");
|
|
33
|
+
Object.defineProperty(exports, "readableWebToNode", { enumerable: true, get: function () { return stream_1.readableWebToNode; } });
|
|
34
|
+
Object.defineProperty(exports, "readableNodeToWeb", { enumerable: true, get: function () { return stream_1.readableNodeToWeb; } });
|
|
35
|
+
Object.defineProperty(exports, "isReadableStream", { enumerable: true, get: function () { return stream_1.isReadableStream; } });
|
|
36
|
+
Object.defineProperty(exports, "isNodeReadable", { enumerable: true, get: function () { return stream_1.isNodeReadable; } });
|
|
37
|
+
Object.defineProperty(exports, "normalizeToReadableStream", { enumerable: true, get: function () { return stream_1.normalizeToReadableStream; } });
|
|
38
|
+
Object.defineProperty(exports, "isReadable", { enumerable: true, get: function () { return stream_1.isReadable; } });
|
|
39
|
+
var hash_1 = require("./hash");
|
|
40
|
+
Object.defineProperty(exports, "keccak256Hash", { enumerable: true, get: function () { return hash_1.keccak256Hash; } });
|
|
41
|
+
var pss_1 = require("./pss");
|
|
42
|
+
Object.defineProperty(exports, "makeMaxTarget", { enumerable: true, get: function () { return pss_1.makeMaxTarget; } });
|
|
43
|
+
var stamps_1 = require("./stamps");
|
|
44
|
+
Object.defineProperty(exports, "getStampUsage", { enumerable: true, get: function () { return stamps_1.getStampUsage; } });
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Compatibility functions for working with File API objects
|
|
4
|
+
*
|
|
5
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/File
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.fileArrayBuffer = exports.isFile = void 0;
|
|
9
|
+
function isFile(file) {
|
|
10
|
+
// browser
|
|
11
|
+
if (typeof File === 'function') {
|
|
12
|
+
return file instanceof File;
|
|
13
|
+
}
|
|
14
|
+
// node.js
|
|
15
|
+
const f = file;
|
|
16
|
+
return (typeof f === 'object' &&
|
|
17
|
+
typeof f.name === 'string' &&
|
|
18
|
+
(typeof f.stream === 'function' || typeof f.arrayBuffer === 'function'));
|
|
19
|
+
}
|
|
20
|
+
exports.isFile = isFile;
|
|
21
|
+
/**
|
|
22
|
+
* Compatibility helper for browsers where the `arrayBuffer function is
|
|
23
|
+
* missing from `File` objects.
|
|
24
|
+
*
|
|
25
|
+
* @param file A File object
|
|
26
|
+
*/
|
|
27
|
+
async function fileArrayBuffer(file) {
|
|
28
|
+
if (file.arrayBuffer) {
|
|
29
|
+
return file.arrayBuffer();
|
|
30
|
+
}
|
|
31
|
+
// workaround for Safari where arrayBuffer is not supported on Files
|
|
32
|
+
return new Promise(resolve => {
|
|
33
|
+
const fr = new FileReader();
|
|
34
|
+
fr.onload = () => resolve(fr.result);
|
|
35
|
+
fr.readAsArrayBuffer(file);
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
exports.fileArrayBuffer = fileArrayBuffer;
|
|
@@ -0,0 +1,21 @@
|
|
|
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;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.extractUploadHeaders = exports.readFileHeaders = void 0;
|
|
4
|
+
const error_1 = require("./error");
|
|
5
|
+
/**
|
|
6
|
+
* Read the filename from the content-disposition header
|
|
7
|
+
* See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition
|
|
8
|
+
*
|
|
9
|
+
* @param header the content-disposition header value
|
|
10
|
+
*
|
|
11
|
+
* @returns the filename
|
|
12
|
+
*/
|
|
13
|
+
function readContentDispositionFilename(header) {
|
|
14
|
+
if (!header) {
|
|
15
|
+
throw new error_1.BeeError('missing content-disposition header');
|
|
16
|
+
}
|
|
17
|
+
// Regex was found here
|
|
18
|
+
// https://stackoverflow.com/questions/23054475/javascript-regex-for-extracting-filename-from-content-disposition-header
|
|
19
|
+
const dispositionMatch = header.match(/filename\*?=['"]?(?:UTF-\d['"]*)?([^;\r\n"']*)['"]?;?/i);
|
|
20
|
+
if (dispositionMatch && dispositionMatch.length > 0) {
|
|
21
|
+
return dispositionMatch[1];
|
|
22
|
+
}
|
|
23
|
+
throw new error_1.BeeError('invalid content-disposition header');
|
|
24
|
+
}
|
|
25
|
+
function readTagUid(header) {
|
|
26
|
+
if (!header) {
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
return parseInt(header, 10);
|
|
30
|
+
}
|
|
31
|
+
function readFileHeaders(headers) {
|
|
32
|
+
const name = readContentDispositionFilename(headers['content-disposition']);
|
|
33
|
+
const tagUid = readTagUid(headers['swarm-tag-uid']);
|
|
34
|
+
const contentType = headers['content-type'] || undefined;
|
|
35
|
+
return {
|
|
36
|
+
name,
|
|
37
|
+
tagUid,
|
|
38
|
+
contentType,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
exports.readFileHeaders = readFileHeaders;
|
|
42
|
+
function extractUploadHeaders(postageBatchId, options) {
|
|
43
|
+
if (!postageBatchId) {
|
|
44
|
+
throw new error_1.BeeError('Postage BatchID has to be specified!');
|
|
45
|
+
}
|
|
46
|
+
const headers = {
|
|
47
|
+
'swarm-postage-batch-id': postageBatchId,
|
|
48
|
+
};
|
|
49
|
+
if (options?.pin)
|
|
50
|
+
headers['swarm-pin'] = String(options.pin);
|
|
51
|
+
if (options?.encrypt)
|
|
52
|
+
headers['swarm-encrypt'] = String(options.encrypt);
|
|
53
|
+
if (options?.tag)
|
|
54
|
+
headers['swarm-tag'] = String(options.tag);
|
|
55
|
+
if (typeof options?.deferred === 'boolean')
|
|
56
|
+
headers['swarm-deferred-upload'] = options.deferred.toString();
|
|
57
|
+
return headers;
|
|
58
|
+
}
|
|
59
|
+
exports.extractUploadHeaders = extractUploadHeaders;
|
|
@@ -0,0 +1,150 @@
|
|
|
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;
|
|
@@ -0,0 +1,40 @@
|
|
|
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.http = exports.DEFAULT_HTTP_CONFIG = void 0;
|
|
7
|
+
const axios_1 = __importDefault(require("axios"));
|
|
8
|
+
const cafe_utility_1 = require("cafe-utility");
|
|
9
|
+
exports.DEFAULT_HTTP_CONFIG = {
|
|
10
|
+
headers: {
|
|
11
|
+
accept: 'application/json, text/plain, */*',
|
|
12
|
+
},
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Main function to make HTTP requests.
|
|
16
|
+
* @param options User defined settings
|
|
17
|
+
* @param config Internal settings and/or Bee settings
|
|
18
|
+
*/
|
|
19
|
+
async function http(options, config) {
|
|
20
|
+
try {
|
|
21
|
+
const requestConfig = cafe_utility_1.Objects.deepMerge3(exports.DEFAULT_HTTP_CONFIG, config, options);
|
|
22
|
+
maybeRunOnRequestHook(options, requestConfig);
|
|
23
|
+
const response = await (0, axios_1.default)(requestConfig);
|
|
24
|
+
return response;
|
|
25
|
+
}
|
|
26
|
+
catch (e) {
|
|
27
|
+
throw e;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
exports.http = http;
|
|
31
|
+
function maybeRunOnRequestHook(options, requestConfig) {
|
|
32
|
+
if (options.onRequest) {
|
|
33
|
+
options.onRequest({
|
|
34
|
+
method: requestConfig.method || 'GET',
|
|
35
|
+
url: cafe_utility_1.Strings.joinUrl(requestConfig.baseURL, requestConfig.url),
|
|
36
|
+
headers: { ...requestConfig.headers },
|
|
37
|
+
params: requestConfig.params,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.makeMaxTarget = void 0;
|
|
4
|
+
const types_1 = require("../types");
|
|
5
|
+
/**
|
|
6
|
+
* Utility function that for given strings/reference takes the most specific
|
|
7
|
+
* target that Bee node will except.
|
|
8
|
+
*
|
|
9
|
+
* @param target is a non-prefixed hex string Bee address
|
|
10
|
+
* @see [Bee docs - PSS](https://docs.ethswarm.org/docs/dapps-on-swarm/pss)
|
|
11
|
+
*/
|
|
12
|
+
function makeMaxTarget(target) {
|
|
13
|
+
if (typeof target !== 'string') {
|
|
14
|
+
throw new TypeError('target has to be an string!');
|
|
15
|
+
}
|
|
16
|
+
return target.slice(0, types_1.PSS_TARGET_HEX_LENGTH_MAX);
|
|
17
|
+
}
|
|
18
|
+
exports.makeMaxTarget = makeMaxTarget;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.makeBytesReference = void 0;
|
|
4
|
+
const types_1 = require("../types");
|
|
5
|
+
const bytes_1 = require("./bytes");
|
|
6
|
+
const hex_1 = require("./hex");
|
|
7
|
+
function makeBytesReference(reference, offset = 0) {
|
|
8
|
+
if (typeof reference === 'string') {
|
|
9
|
+
if (offset) {
|
|
10
|
+
throw new Error('Offset property can be set only for UintArray reference!');
|
|
11
|
+
}
|
|
12
|
+
try {
|
|
13
|
+
// Non-encrypted chunk hex string reference
|
|
14
|
+
const hexReference = (0, hex_1.makeHexString)(reference, types_1.REFERENCE_HEX_LENGTH);
|
|
15
|
+
return (0, hex_1.hexToBytes)(hexReference);
|
|
16
|
+
}
|
|
17
|
+
catch (e) {
|
|
18
|
+
if (!(e instanceof TypeError)) {
|
|
19
|
+
throw e;
|
|
20
|
+
}
|
|
21
|
+
// Encrypted chunk hex string reference
|
|
22
|
+
const hexReference = (0, hex_1.makeHexString)(reference, types_1.ENCRYPTED_REFERENCE_HEX_LENGTH);
|
|
23
|
+
return (0, hex_1.hexToBytes)(hexReference);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
else if (reference instanceof Uint8Array) {
|
|
27
|
+
if ((0, bytes_1.hasBytesAtOffset)(reference, offset, types_1.ENCRYPTED_REFERENCE_BYTES_LENGTH)) {
|
|
28
|
+
return (0, bytes_1.bytesAtOffset)(reference, offset, types_1.ENCRYPTED_REFERENCE_BYTES_LENGTH);
|
|
29
|
+
}
|
|
30
|
+
else if ((0, bytes_1.hasBytesAtOffset)(reference, offset, types_1.REFERENCE_BYTES_LENGTH)) {
|
|
31
|
+
return (0, bytes_1.bytesAtOffset)(reference, offset, types_1.REFERENCE_BYTES_LENGTH);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
throw new TypeError('invalid chunk reference');
|
|
35
|
+
}
|
|
36
|
+
exports.makeBytesReference = makeBytesReference;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getStampUsage = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Utility function that calculates usage of postage batch based on its utilization, depth and bucket depth.
|
|
6
|
+
*
|
|
7
|
+
* Be aware for small depths (17, 18) this does not provide that much information as the provided set of distinct values
|
|
8
|
+
* is small.
|
|
9
|
+
*
|
|
10
|
+
* @param utilization
|
|
11
|
+
* @param depth
|
|
12
|
+
* @param bucketDepth
|
|
13
|
+
*/
|
|
14
|
+
function getStampUsage({ utilization, depth, bucketDepth }) {
|
|
15
|
+
return utilization / Math.pow(2, depth - bucketDepth);
|
|
16
|
+
}
|
|
17
|
+
exports.getStampUsage = getStampUsage;
|