@ethersphere/bee-js 5.1.1 → 5.1.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 +14 -6
- package/dist/223.index.browser.min.js +3 -0
- package/dist/223.index.browser.min.js.LICENSE.txt +1 -0
- package/dist/223.index.browser.min.js.map +1 -0
- package/dist/cjs/bee-debug.js +208 -312
- package/dist/cjs/bee.js +258 -353
- package/dist/cjs/chunk/signer.js +11 -22
- package/dist/cjs/chunk/soc.js +33 -50
- package/dist/cjs/chunk/span.js +1 -1
- package/dist/cjs/feed/index.js +50 -65
- package/dist/cjs/feed/json.js +8 -21
- package/dist/cjs/feed/retrievable.js +14 -27
- package/dist/cjs/modules/bytes.js +25 -37
- package/dist/cjs/modules/bzz.js +63 -68
- package/dist/cjs/modules/chunk.js +17 -27
- package/dist/cjs/modules/debug/balance.js +24 -41
- package/dist/cjs/modules/debug/chequebook.js +71 -96
- package/dist/cjs/modules/debug/chunk.js +13 -26
- package/dist/cjs/modules/debug/connectivity.js +32 -53
- package/dist/cjs/modules/debug/settlements.js +12 -25
- package/dist/cjs/modules/debug/stake.js +20 -33
- package/dist/cjs/modules/debug/stamps.js +45 -66
- package/dist/cjs/modules/debug/states.js +27 -40
- package/dist/cjs/modules/debug/status.js +53 -80
- package/dist/cjs/modules/debug/tag.js +6 -17
- package/dist/cjs/modules/debug/transactions.js +23 -40
- package/dist/cjs/modules/feed.js +17 -27
- package/dist/cjs/modules/pinning.js +24 -41
- package/dist/cjs/modules/pss.js +9 -20
- package/dist/cjs/modules/soc.js +12 -20
- package/dist/cjs/modules/status.js +4 -15
- package/dist/cjs/modules/stewardship.js +10 -23
- package/dist/cjs/modules/tag.js +28 -47
- package/dist/cjs/utils/collection.browser.js +4 -17
- package/dist/cjs/utils/collection.js +11 -22
- package/dist/cjs/utils/collection.node.js +42 -86
- package/dist/cjs/utils/data.browser.js +46 -59
- package/dist/cjs/utils/data.js +22 -35
- package/dist/cjs/utils/eth.js +31 -43
- package/dist/cjs/utils/file.js +9 -20
- package/dist/cjs/utils/headers.js +4 -4
- package/dist/cjs/utils/http.js +78 -93
- package/dist/cjs/utils/merge.js +1 -1
- package/dist/cjs/utils/sleep.js +2 -13
- package/dist/cjs/utils/type.js +14 -12
- package/dist/index.browser.min.js +1 -1
- package/dist/index.browser.min.js.LICENSE.txt +1 -3
- package/dist/index.browser.min.js.map +1 -1
- package/dist/mjs/bee-debug.js +209 -450
- package/dist/mjs/bee.js +256 -491
- package/dist/mjs/chunk/bmt.js +4 -10
- package/dist/mjs/chunk/cac.js +0 -3
- package/dist/mjs/chunk/signer.js +12 -63
- package/dist/mjs/chunk/soc.js +33 -85
- package/dist/mjs/chunk/span.js +3 -6
- package/dist/mjs/feed/identifier.js +0 -6
- package/dist/mjs/feed/index.js +53 -97
- package/dist/mjs/feed/json.js +10 -49
- package/dist/mjs/feed/retrievable.js +13 -64
- package/dist/mjs/feed/topic.js +0 -2
- package/dist/mjs/index.js +1 -2
- package/dist/mjs/modules/bytes.js +25 -65
- package/dist/mjs/modules/bzz.js +64 -108
- package/dist/mjs/modules/chunk.js +17 -54
- package/dist/mjs/modules/debug/balance.js +24 -68
- package/dist/mjs/modules/debug/chequebook.js +75 -138
- package/dist/mjs/modules/debug/chunk.js +13 -51
- package/dist/mjs/modules/debug/connectivity.js +32 -76
- package/dist/mjs/modules/debug/settlements.js +12 -50
- package/dist/mjs/modules/debug/stake.js +20 -61
- package/dist/mjs/modules/debug/stamps.js +47 -94
- package/dist/mjs/modules/debug/states.js +25 -66
- package/dist/mjs/modules/debug/status.js +66 -125
- package/dist/mjs/modules/debug/tag.js +6 -41
- package/dist/mjs/modules/debug/transactions.js +25 -69
- package/dist/mjs/modules/feed.js +17 -57
- package/dist/mjs/modules/pinning.js +24 -68
- package/dist/mjs/modules/pss.js +11 -47
- package/dist/mjs/modules/soc.js +14 -48
- package/dist/mjs/modules/status.js +4 -39
- package/dist/mjs/modules/stewardship.js +10 -47
- package/dist/mjs/modules/tag.js +31 -78
- package/dist/mjs/types/debug.js +0 -1
- package/dist/mjs/types/index.js +0 -4
- package/dist/mjs/utils/bytes.js +6 -14
- package/dist/mjs/utils/collection.browser.js +4 -42
- package/dist/mjs/utils/collection.js +11 -57
- package/dist/mjs/utils/collection.node.js +42 -150
- package/dist/mjs/utils/data.browser.js +49 -94
- package/dist/mjs/utils/data.js +19 -62
- package/dist/mjs/utils/error.js +0 -5
- package/dist/mjs/utils/eth.js +37 -95
- package/dist/mjs/utils/file.js +11 -50
- package/dist/mjs/utils/hash.js +0 -1
- package/dist/mjs/utils/headers.js +6 -14
- package/dist/mjs/utils/hex.js +6 -25
- package/dist/mjs/utils/http.js +80 -138
- package/dist/mjs/utils/merge.js +3 -7
- package/dist/mjs/utils/pss.js +0 -2
- package/dist/mjs/utils/reference.js +2 -5
- package/dist/mjs/utils/sleep.js +2 -37
- package/dist/mjs/utils/stream.js +0 -23
- package/dist/mjs/utils/tar.js +2 -5
- package/dist/mjs/utils/type.js +18 -82
- package/dist/mjs/utils/uint64.js +0 -1
- package/dist/mjs/utils/url.js +2 -10
- package/dist/types/bee-debug.d.ts +2 -2
- package/dist/types/bee.d.ts +2 -26
- package/dist/types/chunk/cac.d.ts +1 -1
- package/dist/types/chunk/soc.d.ts +8 -7
- package/dist/types/feed/index.d.ts +9 -8
- package/dist/types/feed/type.d.ts +1 -1
- package/dist/types/modules/bytes.d.ts +6 -5
- package/dist/types/modules/bzz.d.ts +9 -8
- package/dist/types/modules/chunk.d.ts +5 -4
- package/dist/types/modules/debug/balance.d.ts +10 -9
- package/dist/types/modules/debug/chequebook.d.ts +18 -17
- package/dist/types/modules/debug/chunk.d.ts +6 -5
- package/dist/types/modules/debug/connectivity.d.ts +8 -7
- package/dist/types/modules/debug/settlements.d.ts +6 -5
- package/dist/types/modules/debug/stake.d.ts +5 -4
- package/dist/types/modules/debug/stamps.d.ts +8 -7
- package/dist/types/modules/debug/states.d.ts +8 -7
- package/dist/types/modules/debug/status.d.ts +15 -15
- package/dist/types/modules/debug/tag.d.ts +4 -3
- package/dist/types/modules/debug/transactions.d.ts +6 -5
- package/dist/types/modules/feed.d.ts +4 -3
- package/dist/types/modules/pinning.d.ts +8 -7
- package/dist/types/modules/pss.d.ts +4 -3
- package/dist/types/modules/soc.d.ts +3 -2
- package/dist/types/modules/status.d.ts +3 -3
- package/dist/types/modules/stewardship.d.ts +4 -3
- package/dist/types/modules/tag.d.ts +7 -6
- package/dist/types/types/debug.d.ts +1 -1
- package/dist/types/types/index.d.ts +30 -34
- package/dist/types/utils/eth.d.ts +3 -3
- package/dist/types/utils/hex.d.ts +2 -2
- package/dist/types/utils/http.d.ts +6 -6
- package/package.json +34 -39
- package/dist/cjs/types/ky-options.js +0 -8
- package/dist/cjs/types/ky-universal/common.js +0 -8
- package/dist/cjs/types/ky-universal/hooks.js +0 -8
- package/dist/cjs/types/ky-universal/retry.js +0 -8
- package/dist/mjs/types/ky-options.js +0 -7
- package/dist/mjs/types/ky-universal/common.js +0 -7
- package/dist/mjs/types/ky-universal/hooks.js +0 -7
- package/dist/mjs/types/ky-universal/retry.js +0 -7
- package/dist/types/types/ky-options.d.ts +0 -221
- package/dist/types/types/ky-universal/common.d.ts +0 -13
- package/dist/types/types/ky-universal/hooks.d.ts +0 -92
- package/dist/types/types/ky-universal/retry.d.ts +0 -38
package/dist/mjs/chunk/bmt.js
CHANGED
|
@@ -23,7 +23,6 @@ const HASH_SIZE = 32;
|
|
|
23
23
|
*
|
|
24
24
|
* @returns the keccak256 hash in a byte array
|
|
25
25
|
*/
|
|
26
|
-
|
|
27
26
|
export function bmtHash(chunkContent) {
|
|
28
27
|
const span = chunkContent.slice(0, 8);
|
|
29
28
|
const payload = chunkContent.slice(8);
|
|
@@ -32,25 +31,20 @@ export function bmtHash(chunkContent) {
|
|
|
32
31
|
const chunkHash = keccak256Hash(chunkHashInput);
|
|
33
32
|
return chunkHash;
|
|
34
33
|
}
|
|
35
|
-
|
|
36
34
|
function bmtRootHash(payload) {
|
|
37
35
|
if (payload.length > MAX_CHUNK_PAYLOAD_SIZE) {
|
|
38
36
|
throw new BeeArgumentError('invalid data length', payload);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
|
|
37
|
+
}
|
|
38
|
+
// create an input buffer padded with zeros
|
|
42
39
|
let input = new Uint8Array([...payload, ...new Uint8Array(MAX_CHUNK_PAYLOAD_SIZE - payload.length)]);
|
|
43
|
-
|
|
44
40
|
while (input.length !== HASH_SIZE) {
|
|
45
|
-
const output = new Uint8Array(input.length / 2);
|
|
46
|
-
|
|
41
|
+
const output = new Uint8Array(input.length / 2);
|
|
42
|
+
// in each round we hash the segment pairs together
|
|
47
43
|
for (let offset = 0; offset < input.length; offset += SEGMENT_PAIR_SIZE) {
|
|
48
44
|
const hashNumbers = keccak256.array(input.slice(offset, offset + SEGMENT_PAIR_SIZE));
|
|
49
45
|
output.set(hashNumbers, offset / 2);
|
|
50
46
|
}
|
|
51
|
-
|
|
52
47
|
input = output;
|
|
53
48
|
}
|
|
54
|
-
|
|
55
49
|
return input;
|
|
56
50
|
}
|
package/dist/mjs/chunk/cac.js
CHANGED
|
@@ -12,7 +12,6 @@ const CAC_PAYLOAD_OFFSET = CAC_SPAN_OFFSET + SPAN_SIZE;
|
|
|
12
12
|
*
|
|
13
13
|
* @param payloadBytes the data to be stored in the chunk
|
|
14
14
|
*/
|
|
15
|
-
|
|
16
15
|
export function makeContentAddressedChunk(payloadBytes) {
|
|
17
16
|
const span = makeSpan(payloadBytes.length);
|
|
18
17
|
assertFlexBytes(payloadBytes, MIN_PAYLOAD_SIZE, MAX_PAYLOAD_SIZE);
|
|
@@ -30,7 +29,6 @@ export function makeContentAddressedChunk(payloadBytes) {
|
|
|
30
29
|
* @param data The chunk data
|
|
31
30
|
* @param chunkAddress The address of the chunk
|
|
32
31
|
*/
|
|
33
|
-
|
|
34
32
|
export function isValidChunkData(data, chunkAddress) {
|
|
35
33
|
if (!(data instanceof Uint8Array)) return false;
|
|
36
34
|
const address = bmtHash(data);
|
|
@@ -44,7 +42,6 @@ export function isValidChunkData(data, chunkAddress) {
|
|
|
44
42
|
*
|
|
45
43
|
* @returns a valid content addressed chunk or throws error
|
|
46
44
|
*/
|
|
47
|
-
|
|
48
45
|
export function assertValidChunkData(data, chunkAddress) {
|
|
49
46
|
if (!isValidChunkData(data, chunkAddress)) {
|
|
50
47
|
throw new BeeError('Address of content address chunk does not match given data!');
|
package/dist/mjs/chunk/signer.js
CHANGED
|
@@ -1,36 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
function adopt(value) {
|
|
3
|
-
return value instanceof P ? value : new P(function (resolve) {
|
|
4
|
-
resolve(value);
|
|
5
|
-
});
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
9
|
-
function fulfilled(value) {
|
|
10
|
-
try {
|
|
11
|
-
step(generator.next(value));
|
|
12
|
-
} catch (e) {
|
|
13
|
-
reject(e);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function rejected(value) {
|
|
18
|
-
try {
|
|
19
|
-
step(generator["throw"](value));
|
|
20
|
-
} catch (e) {
|
|
21
|
-
reject(e);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function step(result) {
|
|
26
|
-
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
30
|
-
});
|
|
31
|
-
}; // For ESM compatibility
|
|
32
|
-
|
|
33
|
-
|
|
1
|
+
// For ESM compatibility
|
|
34
2
|
import pkg from 'elliptic';
|
|
35
3
|
const {
|
|
36
4
|
ec
|
|
@@ -42,7 +10,6 @@ import { hexToBytes, makeHexString } from "../utils/hex.js";
|
|
|
42
10
|
import { SIGNATURE_BYTES_LENGTH, SIGNATURE_HEX_LENGTH } from "../types/index.js";
|
|
43
11
|
import { isStrictlyObject } from "../utils/type.js";
|
|
44
12
|
const UNCOMPRESSED_RECOVERY_ID = 27;
|
|
45
|
-
|
|
46
13
|
function hashWithEthereumPrefix(data) {
|
|
47
14
|
const ethereumSignedMessagePrefix = `\x19Ethereum Signed Message:\n${data.length}`;
|
|
48
15
|
const prefixBytes = new TextEncoder().encode(ethereumSignedMessagePrefix);
|
|
@@ -55,8 +22,6 @@ function hashWithEthereumPrefix(data) {
|
|
|
55
22
|
* @param data The data to be signed
|
|
56
23
|
* @param privateKey The private key used for signing the data
|
|
57
24
|
*/
|
|
58
|
-
|
|
59
|
-
|
|
60
25
|
export function defaultSign(data, privateKey) {
|
|
61
26
|
const curve = new ec('secp256k1');
|
|
62
27
|
const keyPair = curve.keyFromPrivate(privateKey);
|
|
@@ -65,15 +30,12 @@ export function defaultSign(data, privateKey) {
|
|
|
65
30
|
canonical: true,
|
|
66
31
|
pers: undefined
|
|
67
32
|
});
|
|
68
|
-
|
|
69
33
|
if (sigRaw.recoveryParam === null) {
|
|
70
34
|
throw new BeeError('signDigest recovery param was null');
|
|
71
35
|
}
|
|
72
|
-
|
|
73
36
|
const signature = new Uint8Array([...sigRaw.r.toArray('be', 32), ...sigRaw.s.toArray('be', 32), sigRaw.recoveryParam + UNCOMPRESSED_RECOVERY_ID]);
|
|
74
37
|
return signature;
|
|
75
38
|
}
|
|
76
|
-
|
|
77
39
|
function publicKeyToAddress(pubKey) {
|
|
78
40
|
const pubBytes = pubKey.encode('array', false);
|
|
79
41
|
return keccak256Hash(pubBytes.slice(1)).slice(12);
|
|
@@ -89,8 +51,6 @@ function publicKeyToAddress(pubKey) {
|
|
|
89
51
|
*
|
|
90
52
|
* @returns the recovered address
|
|
91
53
|
*/
|
|
92
|
-
|
|
93
|
-
|
|
94
54
|
export function recoverAddress(signature, digest) {
|
|
95
55
|
const curve = new ec('secp256k1');
|
|
96
56
|
const sig = {
|
|
@@ -107,7 +67,6 @@ export function recoverAddress(signature, digest) {
|
|
|
107
67
|
*
|
|
108
68
|
* @param privateKey The private key
|
|
109
69
|
*/
|
|
110
|
-
|
|
111
70
|
export function makePrivateKeySigner(privateKey) {
|
|
112
71
|
const curve = new ec('secp256k1');
|
|
113
72
|
const keyPair = curve.keyFromPrivate(privateKey);
|
|
@@ -121,13 +80,10 @@ export function assertSigner(signer) {
|
|
|
121
80
|
if (!isStrictlyObject(signer)) {
|
|
122
81
|
throw new TypeError('Signer must be an object!');
|
|
123
82
|
}
|
|
124
|
-
|
|
125
83
|
const typedSigner = signer;
|
|
126
|
-
|
|
127
84
|
if (!isBytes(typedSigner.address, 20)) {
|
|
128
85
|
throw new TypeError("Signer's address must be Uint8Array with 20 bytes!");
|
|
129
86
|
}
|
|
130
|
-
|
|
131
87
|
if (typeof typedSigner.sign !== 'function') {
|
|
132
88
|
throw new TypeError('Signer sign property needs to be function!');
|
|
133
89
|
}
|
|
@@ -136,30 +92,23 @@ export function makeSigner(signer) {
|
|
|
136
92
|
if (typeof signer === 'string') {
|
|
137
93
|
const hexKey = makeHexString(signer, 64);
|
|
138
94
|
const keyBytes = hexToBytes(hexKey); // HexString is verified for 64 length => 32 is guaranteed
|
|
139
|
-
|
|
140
95
|
return makePrivateKeySigner(keyBytes);
|
|
141
96
|
} else if (signer instanceof Uint8Array) {
|
|
142
97
|
assertBytes(signer, 32);
|
|
143
98
|
return makePrivateKeySigner(signer);
|
|
144
99
|
}
|
|
145
|
-
|
|
146
100
|
assertSigner(signer);
|
|
147
101
|
return signer;
|
|
148
102
|
}
|
|
149
|
-
export function sign(signer, data) {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
return result;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
throw new TypeError('Invalid output of sign function!');
|
|
164
|
-
});
|
|
103
|
+
export async function sign(signer, data) {
|
|
104
|
+
const result = await signer.sign(wrapBytesWithHelpers(data));
|
|
105
|
+
if (typeof result === 'string') {
|
|
106
|
+
const hexString = makeHexString(result, SIGNATURE_HEX_LENGTH);
|
|
107
|
+
return hexToBytes(hexString);
|
|
108
|
+
}
|
|
109
|
+
if (result instanceof Uint8Array) {
|
|
110
|
+
assertBytes(result, SIGNATURE_BYTES_LENGTH);
|
|
111
|
+
return result;
|
|
112
|
+
}
|
|
113
|
+
throw new TypeError('Invalid output of sign function!');
|
|
165
114
|
}
|
package/dist/mjs/chunk/soc.js
CHANGED
|
@@ -1,35 +1,3 @@
|
|
|
1
|
-
var __awaiter = this && this.__awaiter || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) {
|
|
3
|
-
return value instanceof P ? value : new P(function (resolve) {
|
|
4
|
-
resolve(value);
|
|
5
|
-
});
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
9
|
-
function fulfilled(value) {
|
|
10
|
-
try {
|
|
11
|
-
step(generator.next(value));
|
|
12
|
-
} catch (e) {
|
|
13
|
-
reject(e);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function rejected(value) {
|
|
18
|
-
try {
|
|
19
|
-
step(generator["throw"](value));
|
|
20
|
-
} catch (e) {
|
|
21
|
-
reject(e);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function step(result) {
|
|
26
|
-
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
30
|
-
});
|
|
31
|
-
};
|
|
32
|
-
|
|
33
1
|
import { bytesAtOffset, bytesEqual, flexBytesAtOffset } from "../utils/bytes.js";
|
|
34
2
|
import { bmtHash } from "./bmt.js";
|
|
35
3
|
import { recoverAddress, sign } from "./signer.js";
|
|
@@ -48,7 +16,6 @@ const SOC_IDENTIFIER_OFFSET = 0;
|
|
|
48
16
|
const SOC_SIGNATURE_OFFSET = SOC_IDENTIFIER_OFFSET + IDENTIFIER_SIZE;
|
|
49
17
|
const SOC_SPAN_OFFSET = SOC_SIGNATURE_OFFSET + SIGNATURE_SIZE;
|
|
50
18
|
const SOC_PAYLOAD_OFFSET = SOC_SPAN_OFFSET + SPAN_SIZE;
|
|
51
|
-
|
|
52
19
|
function recoverChunkOwner(data) {
|
|
53
20
|
const cacData = data.slice(SOC_SPAN_OFFSET);
|
|
54
21
|
const chunkAddress = bmtHash(cacData);
|
|
@@ -66,23 +33,16 @@ function recoverChunkOwner(data) {
|
|
|
66
33
|
*
|
|
67
34
|
* @returns a single owner chunk or throws error
|
|
68
35
|
*/
|
|
69
|
-
|
|
70
|
-
|
|
71
36
|
export function makeSingleOwnerChunkFromData(data, address) {
|
|
72
37
|
const ownerAddress = recoverChunkOwner(data);
|
|
73
38
|
const identifier = bytesAtOffset(data, SOC_IDENTIFIER_OFFSET, IDENTIFIER_SIZE);
|
|
74
39
|
const socAddress = keccak256Hash(identifier, ownerAddress);
|
|
75
|
-
|
|
76
40
|
if (!bytesEqual(address, socAddress)) {
|
|
77
41
|
throw new BeeError('SOC Data does not match given address!');
|
|
78
42
|
}
|
|
79
|
-
|
|
80
43
|
const signature = () => bytesAtOffset(data, SOC_SIGNATURE_OFFSET, SIGNATURE_SIZE);
|
|
81
|
-
|
|
82
44
|
const span = () => bytesAtOffset(data, SOC_SPAN_OFFSET, SPAN_SIZE);
|
|
83
|
-
|
|
84
45
|
const payload = () => flexBytesAtOffset(data, SOC_PAYLOAD_OFFSET, MIN_PAYLOAD_SIZE, MAX_PAYLOAD_SIZE);
|
|
85
|
-
|
|
86
46
|
return {
|
|
87
47
|
data,
|
|
88
48
|
identifier: () => identifier,
|
|
@@ -103,64 +63,55 @@ export function makeSOCAddress(identifier, address) {
|
|
|
103
63
|
* @param identifier The identifier of the chunk
|
|
104
64
|
* @param signer The singer interface for signing the chunk
|
|
105
65
|
*/
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
owner: () => signer.address
|
|
123
|
-
};
|
|
124
|
-
});
|
|
66
|
+
export async function makeSingleOwnerChunk(chunk, identifier, signer) {
|
|
67
|
+
const chunkAddress = chunk.address();
|
|
68
|
+
assertValidChunkData(chunk.data, chunkAddress);
|
|
69
|
+
const digest = keccak256Hash(identifier, chunkAddress);
|
|
70
|
+
const signature = await sign(signer, digest);
|
|
71
|
+
const data = serializeBytes(identifier, signature, chunk.span(), chunk.payload());
|
|
72
|
+
const address = makeSOCAddress(identifier, signer.address);
|
|
73
|
+
return {
|
|
74
|
+
data,
|
|
75
|
+
identifier: () => identifier,
|
|
76
|
+
signature: () => signature,
|
|
77
|
+
span: () => chunk.span(),
|
|
78
|
+
payload: () => chunk.payload(),
|
|
79
|
+
address: () => address,
|
|
80
|
+
owner: () => signer.address
|
|
81
|
+
};
|
|
125
82
|
}
|
|
126
83
|
/**
|
|
127
84
|
* Helper function to upload a chunk.
|
|
128
85
|
*
|
|
129
86
|
* It uses the Chunk API and calculates the address before uploading.
|
|
130
87
|
*
|
|
131
|
-
* @param
|
|
88
|
+
* @param kyOptions Ky Options for making requests
|
|
132
89
|
* @param chunk A chunk object
|
|
133
90
|
* @param postageBatchId Postage BatchId that will be assigned to uploaded data
|
|
134
91
|
* @param options Upload options
|
|
135
92
|
*/
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
const data = serializeBytes(chunk.span(), chunk.payload());
|
|
143
|
-
return socAPI.upload(ky, owner, identifier, signature, data, postageBatchId, options);
|
|
144
|
-
});
|
|
93
|
+
export async function uploadSingleOwnerChunk(kyOptions, chunk, postageBatchId, options) {
|
|
94
|
+
const owner = bytesToHex(chunk.owner());
|
|
95
|
+
const identifier = bytesToHex(chunk.identifier());
|
|
96
|
+
const signature = bytesToHex(chunk.signature());
|
|
97
|
+
const data = serializeBytes(chunk.span(), chunk.payload());
|
|
98
|
+
return socAPI.upload(kyOptions, owner, identifier, signature, data, postageBatchId, options);
|
|
145
99
|
}
|
|
146
100
|
/**
|
|
147
101
|
* Helper function to create and upload SOC.
|
|
148
102
|
*
|
|
149
|
-
* @param
|
|
103
|
+
* @param kyOptions Ky Options for making requests
|
|
150
104
|
* @param signer The singer interface for signing the chunk
|
|
151
105
|
* @param postageBatchId
|
|
152
106
|
* @param identifier The identifier of the chunk
|
|
153
107
|
* @param data The chunk data
|
|
154
108
|
* @param options
|
|
155
109
|
*/
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
const soc = yield makeSingleOwnerChunk(cac, identifier, signer);
|
|
162
|
-
return uploadSingleOwnerChunk(ky, soc, postageBatchId, options);
|
|
163
|
-
});
|
|
110
|
+
export async function uploadSingleOwnerChunkData(kyOptions, signer, postageBatchId, identifier, data, options) {
|
|
111
|
+
assertAddress(postageBatchId);
|
|
112
|
+
const cac = makeContentAddressedChunk(data);
|
|
113
|
+
const soc = await makeSingleOwnerChunk(cac, identifier, signer);
|
|
114
|
+
return uploadSingleOwnerChunk(kyOptions, soc, postageBatchId, options);
|
|
164
115
|
}
|
|
165
116
|
/**
|
|
166
117
|
* Helper function to download SOC.
|
|
@@ -169,11 +120,8 @@ export function uploadSingleOwnerChunkData(ky, signer, postageBatchId, identifie
|
|
|
169
120
|
* @param ownerAddress The singer interface for signing the chunk
|
|
170
121
|
* @param identifier The identifier of the chunk
|
|
171
122
|
*/
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
const data = yield chunkAPI.download(ky, bytesToHex(address));
|
|
177
|
-
return makeSingleOwnerChunkFromData(data, address);
|
|
178
|
-
});
|
|
123
|
+
export async function downloadSingleOwnerChunk(kyOptions, ownerAddress, identifier) {
|
|
124
|
+
const address = makeSOCAddress(identifier, ownerAddress);
|
|
125
|
+
const data = await chunkAPI.download(kyOptions, bytesToHex(address));
|
|
126
|
+
return makeSingleOwnerChunkFromData(data, address);
|
|
179
127
|
}
|
package/dist/mjs/chunk/span.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { BeeArgumentError } from "../utils/error.js";
|
|
2
|
-
export const SPAN_SIZE = 8;
|
|
3
|
-
|
|
4
|
-
const MAX_SPAN_LENGTH =
|
|
2
|
+
export const SPAN_SIZE = 8;
|
|
3
|
+
// we limit the maximum span size in 32 bits to avoid BigInt compatibility issues
|
|
4
|
+
const MAX_SPAN_LENGTH = 2 ** 32 - 1;
|
|
5
5
|
/**
|
|
6
6
|
* Create a span for storing the length of the chunk
|
|
7
7
|
*
|
|
@@ -9,16 +9,13 @@ const MAX_SPAN_LENGTH = Math.pow(2, 32) - 1;
|
|
|
9
9
|
*
|
|
10
10
|
* @param length The length of the span
|
|
11
11
|
*/
|
|
12
|
-
|
|
13
12
|
export function makeSpan(length) {
|
|
14
13
|
if (length <= 0) {
|
|
15
14
|
throw new BeeArgumentError('invalid length for span', length);
|
|
16
15
|
}
|
|
17
|
-
|
|
18
16
|
if (length > MAX_SPAN_LENGTH) {
|
|
19
17
|
throw new BeeArgumentError('invalid length (> MAX_SPAN_LENGTH)', length);
|
|
20
18
|
}
|
|
21
|
-
|
|
22
19
|
const span = new Uint8Array(SPAN_SIZE);
|
|
23
20
|
const dataView = new DataView(span.buffer);
|
|
24
21
|
const littleEndian = true;
|
|
@@ -2,25 +2,20 @@ import { FEED_INDEX_HEX_LENGTH } from "../types/index.js";
|
|
|
2
2
|
import { keccak256Hash } from "../utils/hash.js";
|
|
3
3
|
import { hexToBytes, makeHexString } from "../utils/hex.js";
|
|
4
4
|
import { writeUint64BigEndian } from "../utils/uint64.js";
|
|
5
|
-
|
|
6
5
|
function isEpoch(epoch) {
|
|
7
6
|
return typeof epoch === 'object' && epoch !== null && 'time' in epoch && 'level' in epoch;
|
|
8
7
|
}
|
|
9
|
-
|
|
10
8
|
function hashFeedIdentifier(topic, index) {
|
|
11
9
|
return keccak256Hash(hexToBytes(topic), index);
|
|
12
10
|
}
|
|
13
|
-
|
|
14
11
|
function makeSequentialFeedIdentifier(topic, index) {
|
|
15
12
|
const indexBytes = writeUint64BigEndian(index);
|
|
16
13
|
return hashFeedIdentifier(topic, indexBytes);
|
|
17
14
|
}
|
|
18
|
-
|
|
19
15
|
function makeFeedIndexBytes(s) {
|
|
20
16
|
const hex = makeHexString(s, FEED_INDEX_HEX_LENGTH);
|
|
21
17
|
return hexToBytes(hex);
|
|
22
18
|
}
|
|
23
|
-
|
|
24
19
|
export function makeFeedIdentifier(topic, index) {
|
|
25
20
|
if (typeof index === 'number') {
|
|
26
21
|
return makeSequentialFeedIdentifier(topic, index);
|
|
@@ -30,6 +25,5 @@ export function makeFeedIdentifier(topic, index) {
|
|
|
30
25
|
} else if (isEpoch(index)) {
|
|
31
26
|
throw new TypeError('epoch is not yet implemented');
|
|
32
27
|
}
|
|
33
|
-
|
|
34
28
|
return hashFeedIdentifier(topic, index);
|
|
35
29
|
}
|
package/dist/mjs/feed/index.js
CHANGED
|
@@ -1,35 +1,3 @@
|
|
|
1
|
-
var __awaiter = this && this.__awaiter || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) {
|
|
3
|
-
return value instanceof P ? value : new P(function (resolve) {
|
|
4
|
-
resolve(value);
|
|
5
|
-
});
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
9
|
-
function fulfilled(value) {
|
|
10
|
-
try {
|
|
11
|
-
step(generator.next(value));
|
|
12
|
-
} catch (e) {
|
|
13
|
-
reject(e);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function rejected(value) {
|
|
18
|
-
try {
|
|
19
|
-
step(generator["throw"](value));
|
|
20
|
-
} catch (e) {
|
|
21
|
-
reject(e);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function step(result) {
|
|
26
|
-
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
30
|
-
});
|
|
31
|
-
};
|
|
32
|
-
|
|
33
1
|
import { keccak256Hash } from "../utils/hash.js";
|
|
34
2
|
import { serializeBytes } from "../chunk/serialize.js";
|
|
35
3
|
import { fetchLatestFeedUpdate } from "../modules/feed.js";
|
|
@@ -47,88 +15,76 @@ import { makeBytesReference } from "../utils/reference.js";
|
|
|
47
15
|
const TIMESTAMP_PAYLOAD_OFFSET = 0;
|
|
48
16
|
const TIMESTAMP_PAYLOAD_SIZE = 8;
|
|
49
17
|
const REFERENCE_PAYLOAD_OFFSET = TIMESTAMP_PAYLOAD_SIZE;
|
|
50
|
-
export function findNextIndex(
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
return bytesToHex(makeBytes(8));
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
throw e;
|
|
18
|
+
export async function findNextIndex(kyOptions, owner, topic, options) {
|
|
19
|
+
try {
|
|
20
|
+
const feedUpdate = await fetchLatestFeedUpdate(kyOptions, owner, topic, options);
|
|
21
|
+
return makeHexString(feedUpdate.feedIndexNext, FEED_INDEX_HEX_LENGTH);
|
|
22
|
+
} catch (e) {
|
|
23
|
+
if (e instanceof BeeResponseError && e.status === 404) {
|
|
24
|
+
return bytesToHex(makeBytes(8));
|
|
61
25
|
}
|
|
62
|
-
|
|
26
|
+
throw e;
|
|
27
|
+
}
|
|
63
28
|
}
|
|
64
|
-
export function updateFeed(
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
const timestamp = writeUint64BigEndian(at);
|
|
73
|
-
const payloadBytes = serializeBytes(timestamp, reference);
|
|
74
|
-
return uploadSingleOwnerChunkData(ky, signer, postageBatchId, identifier, payloadBytes, options);
|
|
75
|
-
});
|
|
29
|
+
export async function updateFeed(kyOptions, signer, topic, reference, postageBatchId, options, index = 'latest') {
|
|
30
|
+
const ownerHex = makeHexEthAddress(signer.address);
|
|
31
|
+
const nextIndex = index === 'latest' ? await findNextIndex(kyOptions, ownerHex, topic, options) : index;
|
|
32
|
+
const identifier = makeFeedIdentifier(topic, nextIndex);
|
|
33
|
+
const at = options?.at ?? Date.now() / 1000.0;
|
|
34
|
+
const timestamp = writeUint64BigEndian(at);
|
|
35
|
+
const payloadBytes = serializeBytes(timestamp, reference);
|
|
36
|
+
return uploadSingleOwnerChunkData(kyOptions, signer, postageBatchId, identifier, payloadBytes, options);
|
|
76
37
|
}
|
|
77
38
|
export function getFeedUpdateChunkReference(owner, topic, index) {
|
|
78
39
|
const identifier = makeFeedIdentifier(topic, index);
|
|
79
40
|
return keccak256Hash(identifier, owner);
|
|
80
41
|
}
|
|
81
|
-
export function downloadFeedUpdate(
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
};
|
|
95
|
-
});
|
|
42
|
+
export async function downloadFeedUpdate(kyOptions, owner, topic, index) {
|
|
43
|
+
const address = getFeedUpdateChunkReference(owner, topic, index);
|
|
44
|
+
const addressHex = bytesToHex(address);
|
|
45
|
+
const data = await chunkAPI.download(kyOptions, addressHex);
|
|
46
|
+
const soc = makeSingleOwnerChunkFromData(data, address);
|
|
47
|
+
const payload = soc.payload();
|
|
48
|
+
const timestampBytes = bytesAtOffset(payload, TIMESTAMP_PAYLOAD_OFFSET, TIMESTAMP_PAYLOAD_SIZE);
|
|
49
|
+
const timestamp = readUint64BigEndian(timestampBytes);
|
|
50
|
+
const reference = makeBytesReference(payload, REFERENCE_PAYLOAD_OFFSET);
|
|
51
|
+
return {
|
|
52
|
+
timestamp,
|
|
53
|
+
reference
|
|
54
|
+
};
|
|
96
55
|
}
|
|
97
|
-
export function makeFeedReader(
|
|
56
|
+
export function makeFeedReader(kyOptions, type, topic, owner) {
|
|
98
57
|
return {
|
|
99
58
|
type,
|
|
100
59
|
owner,
|
|
101
60
|
topic,
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
feedIndexNext: ''
|
|
116
|
-
};
|
|
117
|
-
});
|
|
61
|
+
async download(options) {
|
|
62
|
+
if (!options?.index) {
|
|
63
|
+
return fetchLatestFeedUpdate(kyOptions, owner, topic, {
|
|
64
|
+
...options,
|
|
65
|
+
type
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
const update = await downloadFeedUpdate(kyOptions, hexToBytes(owner), topic, options.index);
|
|
69
|
+
return {
|
|
70
|
+
reference: bytesToHex(update.reference),
|
|
71
|
+
feedIndex: options.index,
|
|
72
|
+
feedIndexNext: ''
|
|
73
|
+
};
|
|
118
74
|
}
|
|
119
|
-
|
|
120
75
|
};
|
|
121
76
|
}
|
|
122
|
-
export function makeFeedWriter(
|
|
123
|
-
const upload = (postageBatchId, reference, options) =>
|
|
77
|
+
export function makeFeedWriter(kyOptions, type, topic, signer) {
|
|
78
|
+
const upload = async (postageBatchId, reference, options) => {
|
|
124
79
|
assertAddress(postageBatchId);
|
|
125
80
|
const canonicalReference = makeBytesReference(reference);
|
|
126
|
-
return updateFeed(
|
|
81
|
+
return updateFeed(kyOptions, signer, topic, canonicalReference, postageBatchId, {
|
|
82
|
+
...options,
|
|
127
83
|
type
|
|
128
|
-
})
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
|
|
84
|
+
});
|
|
85
|
+
};
|
|
86
|
+
return {
|
|
87
|
+
...makeFeedReader(kyOptions, type, topic, makeHexEthAddress(signer.address)),
|
|
132
88
|
upload
|
|
133
|
-
}
|
|
89
|
+
};
|
|
134
90
|
}
|