@ethersphere/bee-js 3.3.0 → 3.3.2-pre.1
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/dist/cjs/chunk/bmt.js +7 -2
- package/dist/cjs/chunk/signer.js +9 -4
- package/dist/cjs/modules/debug/status.js +8 -8
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/types/debug.js +1 -0
- package/dist/cjs/types/ky-options.js +8 -0
- package/dist/cjs/types/ky-universal/common.js +8 -0
- package/dist/cjs/types/ky-universal/hooks.js +8 -0
- package/dist/cjs/types/ky-universal/retry.js +8 -0
- package/dist/cjs/utils/eth.js +8 -3
- package/dist/cjs/utils/hash.js +7 -2
- package/dist/cjs/utils/stream.js +14 -5
- package/dist/index.browser.min.js +3 -0
- package/dist/index.browser.min.js.LICENSE.txt +60 -0
- package/dist/index.browser.min.js.map +1 -0
- package/dist/mjs/bee-debug.js +608 -508
- package/dist/mjs/bee.js +935 -836
- package/dist/mjs/chunk/bmt.js +34 -24
- package/dist/mjs/chunk/cac.js +24 -22
- package/dist/mjs/chunk/serialize.js +9 -9
- package/dist/mjs/chunk/signer.js +92 -73
- package/dist/mjs/chunk/soc.js +78 -66
- package/dist/mjs/chunk/span.js +19 -16
- package/dist/mjs/feed/index.js +110 -101
- package/dist/mjs/feed/json.js +21 -17
- package/dist/mjs/feed/topic.js +18 -17
- package/dist/mjs/feed/type.js +5 -5
- package/dist/mjs/index.js +7 -7
- package/dist/mjs/modules/bytes.js +33 -30
- package/dist/mjs/modules/bzz.js +80 -77
- package/dist/mjs/modules/chunk.js +22 -20
- package/dist/mjs/modules/debug/balance.js +26 -22
- package/dist/mjs/modules/debug/chequebook.js +84 -65
- package/dist/mjs/modules/debug/chunk.js +15 -13
- package/dist/mjs/modules/debug/connectivity.js +34 -34
- package/dist/mjs/modules/debug/settlements.js +14 -12
- package/dist/mjs/modules/debug/stamps.js +52 -47
- package/dist/mjs/modules/debug/states.js +16 -14
- package/dist/mjs/modules/debug/status.js +58 -38
- package/dist/mjs/modules/debug/tag.js +8 -7
- package/dist/mjs/modules/debug/transactions.js +31 -25
- package/dist/mjs/modules/feed.js +39 -33
- package/dist/mjs/modules/pinning.js +34 -28
- package/dist/mjs/modules/pss.js +18 -14
- package/dist/mjs/modules/soc.js +18 -15
- package/dist/mjs/modules/status.js +6 -5
- package/dist/mjs/modules/stewardship.js +13 -12
- package/dist/mjs/modules/tag.js +38 -30
- package/dist/mjs/package.json +1 -0
- package/dist/mjs/types/debug.js +6 -4
- package/dist/mjs/types/index.js +6 -2
- package/dist/mjs/types/ky-options.js +7 -0
- package/dist/mjs/types/ky-universal/common.js +7 -0
- package/dist/mjs/types/ky-universal/hooks.js +7 -0
- package/dist/mjs/types/ky-universal/retry.js +7 -0
- package/dist/mjs/utils/bytes.js +32 -26
- package/dist/mjs/utils/collection.browser.js +4 -3
- package/dist/mjs/utils/collection.js +47 -35
- package/dist/mjs/utils/collection.node.js +53 -42
- package/dist/mjs/utils/data.browser.js +64 -48
- package/dist/mjs/utils/data.js +29 -25
- package/dist/mjs/utils/error.js +47 -39
- package/dist/mjs/utils/eth.js +119 -104
- package/dist/mjs/utils/expose.js +9 -9
- package/dist/mjs/utils/file.js +22 -19
- package/dist/mjs/utils/hash.js +10 -5
- package/dist/mjs/utils/headers.js +43 -39
- package/dist/mjs/utils/hex.js +78 -63
- package/dist/mjs/utils/http.js +119 -100
- package/dist/mjs/utils/merge.js +26 -20
- package/dist/mjs/utils/pss.js +8 -6
- package/dist/mjs/utils/stamps.js +7 -3
- package/dist/mjs/utils/stream.js +107 -82
- package/dist/mjs/utils/tar.js +17 -14
- package/dist/mjs/utils/type.js +249 -209
- package/dist/mjs/utils/uint64.js +16 -16
- package/dist/mjs/utils/url.js +32 -25
- package/dist/types/bee-debug.d.ts +3 -3
- package/dist/types/modules/debug/stamps.d.ts +3 -3
- package/dist/types/modules/debug/status.d.ts +3 -3
- package/dist/types/types/debug.d.ts +1 -0
- package/dist/types/types/index.d.ts +2 -2
- package/dist/types/types/ky-options.d.ts +221 -0
- package/dist/types/types/ky-universal/common.d.ts +13 -0
- package/dist/types/types/ky-universal/hooks.d.ts +92 -0
- package/dist/types/types/ky-universal/retry.d.ts +38 -0
- package/dist/types/utils/stamps.d.ts +2 -2
- package/dist/types/utils/stream.d.ts +11 -8
- package/package.json +22 -19
- package/dist/index.js +0 -28326
- package/dist/index.js.map +0 -1
package/dist/mjs/chunk/bmt.js
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
import
|
|
3
|
-
|
|
1
|
+
// For ESM compatibility
|
|
2
|
+
import pkg from 'js-sha3';
|
|
3
|
+
const {
|
|
4
|
+
keccak256
|
|
5
|
+
} = pkg;
|
|
6
|
+
import { BeeArgumentError } from "../utils/error.js";
|
|
7
|
+
import { keccak256Hash } from "../utils/hash.js";
|
|
4
8
|
const MAX_CHUNK_PAYLOAD_SIZE = 4096;
|
|
5
9
|
const SEGMENT_SIZE = 32;
|
|
6
10
|
const SEGMENT_PAIR_SIZE = 2 * SEGMENT_SIZE;
|
|
@@ -19,28 +23,34 @@ const HASH_SIZE = 32;
|
|
|
19
23
|
*
|
|
20
24
|
* @returns the keccak256 hash in a byte array
|
|
21
25
|
*/
|
|
26
|
+
|
|
22
27
|
export function bmtHash(chunkContent) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
28
|
+
const span = chunkContent.slice(0, 8);
|
|
29
|
+
const payload = chunkContent.slice(8);
|
|
30
|
+
const rootHash = bmtRootHash(payload);
|
|
31
|
+
const chunkHashInput = new Uint8Array([...span, ...rootHash]);
|
|
32
|
+
const chunkHash = keccak256Hash(chunkHashInput);
|
|
33
|
+
return chunkHash;
|
|
29
34
|
}
|
|
35
|
+
|
|
30
36
|
function bmtRootHash(payload) {
|
|
31
|
-
|
|
32
|
-
|
|
37
|
+
if (payload.length > MAX_CHUNK_PAYLOAD_SIZE) {
|
|
38
|
+
throw new BeeArgumentError('invalid data length', payload);
|
|
39
|
+
} // create an input buffer padded with zeros
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
let input = new Uint8Array([...payload, ...new Uint8Array(MAX_CHUNK_PAYLOAD_SIZE - payload.length)]);
|
|
43
|
+
|
|
44
|
+
while (input.length !== HASH_SIZE) {
|
|
45
|
+
const output = new Uint8Array(input.length / 2); // in each round we hash the segment pairs together
|
|
46
|
+
|
|
47
|
+
for (let offset = 0; offset < input.length; offset += SEGMENT_PAIR_SIZE) {
|
|
48
|
+
const hashNumbers = keccak256.array(input.slice(offset, offset + SEGMENT_PAIR_SIZE));
|
|
49
|
+
output.set(hashNumbers, offset / 2);
|
|
33
50
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const hashNumbers = keccak256.array(input.slice(offset, offset + SEGMENT_PAIR_SIZE));
|
|
41
|
-
output.set(hashNumbers, offset / 2);
|
|
42
|
-
}
|
|
43
|
-
input = output;
|
|
44
|
-
}
|
|
45
|
-
return input;
|
|
46
|
-
}
|
|
51
|
+
|
|
52
|
+
input = output;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return input;
|
|
56
|
+
}
|
package/dist/mjs/chunk/cac.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { BeeError } from
|
|
2
|
-
import { bmtHash } from
|
|
3
|
-
import { bytesEqual, flexBytesAtOffset, assertFlexBytes } from
|
|
4
|
-
import { serializeBytes } from
|
|
5
|
-
import { makeSpan, SPAN_SIZE } from
|
|
1
|
+
import { BeeError } from "../utils/error.js";
|
|
2
|
+
import { bmtHash } from "./bmt.js";
|
|
3
|
+
import { bytesEqual, flexBytesAtOffset, assertFlexBytes } from "../utils/bytes.js";
|
|
4
|
+
import { serializeBytes } from "./serialize.js";
|
|
5
|
+
import { makeSpan, SPAN_SIZE } from "./span.js";
|
|
6
6
|
export const MIN_PAYLOAD_SIZE = 1;
|
|
7
7
|
export const MAX_PAYLOAD_SIZE = 4096;
|
|
8
8
|
const CAC_SPAN_OFFSET = 0;
|
|
@@ -12,16 +12,17 @@ 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
|
+
|
|
15
16
|
export function makeContentAddressedChunk(payloadBytes) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
17
|
+
const span = makeSpan(payloadBytes.length);
|
|
18
|
+
assertFlexBytes(payloadBytes, MIN_PAYLOAD_SIZE, MAX_PAYLOAD_SIZE);
|
|
19
|
+
const data = serializeBytes(span, payloadBytes);
|
|
20
|
+
return {
|
|
21
|
+
data,
|
|
22
|
+
span: () => span,
|
|
23
|
+
payload: () => flexBytesAtOffset(data, CAC_PAYLOAD_OFFSET, MIN_PAYLOAD_SIZE, MAX_PAYLOAD_SIZE),
|
|
24
|
+
address: () => bmtHash(data)
|
|
25
|
+
};
|
|
25
26
|
}
|
|
26
27
|
/**
|
|
27
28
|
* Type guard for valid content addressed chunk data
|
|
@@ -29,11 +30,11 @@ export function makeContentAddressedChunk(payloadBytes) {
|
|
|
29
30
|
* @param data The chunk data
|
|
30
31
|
* @param chunkAddress The address of the chunk
|
|
31
32
|
*/
|
|
33
|
+
|
|
32
34
|
export function isValidChunkData(data, chunkAddress) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
return bytesEqual(address, chunkAddress);
|
|
35
|
+
if (!(data instanceof Uint8Array)) return false;
|
|
36
|
+
const address = bmtHash(data);
|
|
37
|
+
return bytesEqual(address, chunkAddress);
|
|
37
38
|
}
|
|
38
39
|
/**
|
|
39
40
|
* Asserts if data are representing given address of its chunk.
|
|
@@ -43,8 +44,9 @@ export function isValidChunkData(data, chunkAddress) {
|
|
|
43
44
|
*
|
|
44
45
|
* @returns a valid content addressed chunk or throws error
|
|
45
46
|
*/
|
|
47
|
+
|
|
46
48
|
export function assertValidChunkData(data, chunkAddress) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
49
|
+
if (!isValidChunkData(data, chunkAddress)) {
|
|
50
|
+
throw new BeeError('Address of content address chunk does not match given data!');
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -4,12 +4,12 @@
|
|
|
4
4
|
* @param arrays Any number of byte array arguments
|
|
5
5
|
*/
|
|
6
6
|
export function serializeBytes(...arrays) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
7
|
+
const length = arrays.reduce((prev, curr) => prev + curr.length, 0);
|
|
8
|
+
const buffer = new Uint8Array(length);
|
|
9
|
+
let offset = 0;
|
|
10
|
+
arrays.forEach(arr => {
|
|
11
|
+
buffer.set(arr, offset);
|
|
12
|
+
offset += arr.length;
|
|
13
|
+
});
|
|
14
|
+
return buffer;
|
|
15
|
+
}
|
package/dist/mjs/chunk/signer.js
CHANGED
|
@@ -1,15 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
1
|
+
// For ESM compatibility
|
|
2
|
+
import pkg from 'elliptic';
|
|
3
|
+
const {
|
|
4
|
+
ec
|
|
5
|
+
} = pkg;
|
|
6
|
+
import { BeeError } from "../utils/error.js";
|
|
7
|
+
import { isBytes, assertBytes, wrapBytesWithHelpers } from "../utils/bytes.js";
|
|
8
|
+
import { keccak256Hash } from "../utils/hash.js";
|
|
9
|
+
import { hexToBytes, makeHexString } from "../utils/hex.js";
|
|
10
|
+
import { SIGNATURE_BYTES_LENGTH, SIGNATURE_HEX_LENGTH } from "../types/index.js";
|
|
11
|
+
import { isStrictlyObject } from "../utils/type.js";
|
|
8
12
|
const UNCOMPRESSED_RECOVERY_ID = 27;
|
|
13
|
+
|
|
9
14
|
function hashWithEthereumPrefix(data) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
15
|
+
const ethereumSignedMessagePrefix = `\x19Ethereum Signed Message:\n${data.length}`;
|
|
16
|
+
const prefixBytes = new TextEncoder().encode(ethereumSignedMessagePrefix);
|
|
17
|
+
return keccak256Hash(prefixBytes, data);
|
|
13
18
|
}
|
|
14
19
|
/**
|
|
15
20
|
* The default signer function that can be used for integrating with
|
|
@@ -18,24 +23,28 @@ function hashWithEthereumPrefix(data) {
|
|
|
18
23
|
* @param data The data to be signed
|
|
19
24
|
* @param privateKey The private key used for signing the data
|
|
20
25
|
*/
|
|
26
|
+
|
|
27
|
+
|
|
21
28
|
export function defaultSign(data, privateKey) {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
29
|
+
const curve = new ec('secp256k1');
|
|
30
|
+
const keyPair = curve.keyFromPrivate(privateKey);
|
|
31
|
+
const hashedDigest = hashWithEthereumPrefix(data);
|
|
32
|
+
const sigRaw = curve.sign(hashedDigest, keyPair, {
|
|
33
|
+
canonical: true,
|
|
34
|
+
pers: undefined
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
if (sigRaw.recoveryParam === null) {
|
|
38
|
+
throw new BeeError('signDigest recovery param was null');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const signature = new Uint8Array([...sigRaw.r.toArray('be', 32), ...sigRaw.s.toArray('be', 32), sigRaw.recoveryParam + UNCOMPRESSED_RECOVERY_ID]);
|
|
42
|
+
return signature;
|
|
35
43
|
}
|
|
44
|
+
|
|
36
45
|
function publicKeyToAddress(pubKey) {
|
|
37
|
-
|
|
38
|
-
|
|
46
|
+
const pubBytes = pubKey.encode('array', false);
|
|
47
|
+
return keccak256Hash(pubBytes.slice(1)).slice(12);
|
|
39
48
|
}
|
|
40
49
|
/**
|
|
41
50
|
* Recovers the ethereum address from a given signature.
|
|
@@ -48,65 +57,75 @@ function publicKeyToAddress(pubKey) {
|
|
|
48
57
|
*
|
|
49
58
|
* @returns the recovered address
|
|
50
59
|
*/
|
|
60
|
+
|
|
61
|
+
|
|
51
62
|
export function recoverAddress(signature, digest) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
63
|
+
const curve = new ec('secp256k1');
|
|
64
|
+
const sig = {
|
|
65
|
+
r: signature.slice(0, 32),
|
|
66
|
+
s: signature.slice(32, 64)
|
|
67
|
+
};
|
|
68
|
+
const recoveryParam = signature[64] - UNCOMPRESSED_RECOVERY_ID;
|
|
69
|
+
const hash = hashWithEthereumPrefix(digest);
|
|
70
|
+
const recPubKey = curve.recoverPubKey(hash, sig, recoveryParam);
|
|
71
|
+
return publicKeyToAddress(recPubKey);
|
|
61
72
|
}
|
|
62
73
|
/**
|
|
63
74
|
* Creates a singer object that can be used when the private key is known.
|
|
64
75
|
*
|
|
65
76
|
* @param privateKey The private key
|
|
66
77
|
*/
|
|
78
|
+
|
|
67
79
|
export function makePrivateKeySigner(privateKey) {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
80
|
+
const curve = new ec('secp256k1');
|
|
81
|
+
const keyPair = curve.keyFromPrivate(privateKey);
|
|
82
|
+
const address = publicKeyToAddress(keyPair.getPublic());
|
|
83
|
+
return {
|
|
84
|
+
sign: digest => defaultSign(digest, privateKey),
|
|
85
|
+
address
|
|
86
|
+
};
|
|
75
87
|
}
|
|
76
88
|
export function assertSigner(signer) {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
89
|
+
if (!isStrictlyObject(signer)) {
|
|
90
|
+
throw new TypeError('Signer must be an object!');
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const typedSigner = signer;
|
|
94
|
+
|
|
95
|
+
if (!isBytes(typedSigner.address, 20)) {
|
|
96
|
+
throw new TypeError("Signer's address must be Uint8Array with 20 bytes!");
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (typeof typedSigner.sign !== 'function') {
|
|
100
|
+
throw new TypeError('Signer sign property needs to be function!');
|
|
101
|
+
}
|
|
87
102
|
}
|
|
88
103
|
export function makeSigner(signer) {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
104
|
+
if (typeof signer === 'string') {
|
|
105
|
+
const hexKey = makeHexString(signer, 64);
|
|
106
|
+
const keyBytes = hexToBytes(hexKey); // HexString is verified for 64 length => 32 is guaranteed
|
|
107
|
+
|
|
108
|
+
return makePrivateKeySigner(keyBytes);
|
|
109
|
+
} else if (signer instanceof Uint8Array) {
|
|
110
|
+
assertBytes(signer, 32);
|
|
111
|
+
return makePrivateKeySigner(signer);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
assertSigner(signer);
|
|
115
|
+
return signer;
|
|
100
116
|
}
|
|
101
117
|
export async function sign(signer, data) {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
}
|
|
118
|
+
const result = await signer.sign(wrapBytesWithHelpers(data));
|
|
119
|
+
|
|
120
|
+
if (typeof result === 'string') {
|
|
121
|
+
const hexString = makeHexString(result, SIGNATURE_HEX_LENGTH);
|
|
122
|
+
return hexToBytes(hexString);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (result instanceof Uint8Array) {
|
|
126
|
+
assertBytes(result, SIGNATURE_BYTES_LENGTH);
|
|
127
|
+
return result;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
throw new TypeError('Invalid output of sign function!');
|
|
131
|
+
}
|
package/dist/mjs/chunk/soc.js
CHANGED
|
@@ -1,29 +1,30 @@
|
|
|
1
|
-
import { bytesAtOffset, bytesEqual, flexBytesAtOffset } from
|
|
2
|
-
import { bmtHash } from
|
|
3
|
-
import { recoverAddress, sign } from
|
|
4
|
-
import { keccak256Hash } from
|
|
5
|
-
import { SPAN_SIZE } from
|
|
6
|
-
import { serializeBytes } from
|
|
7
|
-
import { BeeError } from
|
|
8
|
-
import { makeContentAddressedChunk, MAX_PAYLOAD_SIZE, MIN_PAYLOAD_SIZE, assertValidChunkData
|
|
9
|
-
import { bytesToHex } from
|
|
10
|
-
import * as socAPI from
|
|
11
|
-
import * as chunkAPI from
|
|
12
|
-
import { assertAddress } from
|
|
1
|
+
import { bytesAtOffset, bytesEqual, flexBytesAtOffset } from "../utils/bytes.js";
|
|
2
|
+
import { bmtHash } from "./bmt.js";
|
|
3
|
+
import { recoverAddress, sign } from "./signer.js";
|
|
4
|
+
import { keccak256Hash } from "../utils/hash.js";
|
|
5
|
+
import { SPAN_SIZE } from "./span.js";
|
|
6
|
+
import { serializeBytes } from "./serialize.js";
|
|
7
|
+
import { BeeError } from "../utils/error.js";
|
|
8
|
+
import { makeContentAddressedChunk, MAX_PAYLOAD_SIZE, MIN_PAYLOAD_SIZE, assertValidChunkData } from "./cac.js";
|
|
9
|
+
import { bytesToHex } from "../utils/hex.js";
|
|
10
|
+
import * as socAPI from "../modules/soc.js";
|
|
11
|
+
import * as chunkAPI from "../modules/chunk.js";
|
|
12
|
+
import { assertAddress } from "../utils/type.js";
|
|
13
13
|
const IDENTIFIER_SIZE = 32;
|
|
14
14
|
const SIGNATURE_SIZE = 65;
|
|
15
15
|
const SOC_IDENTIFIER_OFFSET = 0;
|
|
16
16
|
const SOC_SIGNATURE_OFFSET = SOC_IDENTIFIER_OFFSET + IDENTIFIER_SIZE;
|
|
17
17
|
const SOC_SPAN_OFFSET = SOC_SIGNATURE_OFFSET + SIGNATURE_SIZE;
|
|
18
18
|
const SOC_PAYLOAD_OFFSET = SOC_SPAN_OFFSET + SPAN_SIZE;
|
|
19
|
+
|
|
19
20
|
function recoverChunkOwner(data) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
21
|
+
const cacData = data.slice(SOC_SPAN_OFFSET);
|
|
22
|
+
const chunkAddress = bmtHash(cacData);
|
|
23
|
+
const signature = bytesAtOffset(data, SOC_SIGNATURE_OFFSET, SIGNATURE_SIZE);
|
|
24
|
+
const identifier = bytesAtOffset(data, SOC_IDENTIFIER_OFFSET, IDENTIFIER_SIZE);
|
|
25
|
+
const digest = keccak256Hash(identifier, chunkAddress);
|
|
26
|
+
const ownerAddress = recoverAddress(signature, digest);
|
|
27
|
+
return ownerAddress;
|
|
27
28
|
}
|
|
28
29
|
/**
|
|
29
30
|
* Verifies if the data is a valid single owner chunk
|
|
@@ -33,28 +34,35 @@ function recoverChunkOwner(data) {
|
|
|
33
34
|
*
|
|
34
35
|
* @returns a single owner chunk or throws error
|
|
35
36
|
*/
|
|
37
|
+
|
|
38
|
+
|
|
36
39
|
export function makeSingleOwnerChunkFromData(data, address) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
40
|
+
const ownerAddress = recoverChunkOwner(data);
|
|
41
|
+
const identifier = bytesAtOffset(data, SOC_IDENTIFIER_OFFSET, IDENTIFIER_SIZE);
|
|
42
|
+
const socAddress = keccak256Hash(identifier, ownerAddress);
|
|
43
|
+
|
|
44
|
+
if (!bytesEqual(address, socAddress)) {
|
|
45
|
+
throw new BeeError('SOC Data does not match given address!');
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const signature = () => bytesAtOffset(data, SOC_SIGNATURE_OFFSET, SIGNATURE_SIZE);
|
|
49
|
+
|
|
50
|
+
const span = () => bytesAtOffset(data, SOC_SPAN_OFFSET, SPAN_SIZE);
|
|
51
|
+
|
|
52
|
+
const payload = () => flexBytesAtOffset(data, SOC_PAYLOAD_OFFSET, MIN_PAYLOAD_SIZE, MAX_PAYLOAD_SIZE);
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
data,
|
|
56
|
+
identifier: () => identifier,
|
|
57
|
+
signature,
|
|
58
|
+
span,
|
|
59
|
+
payload,
|
|
60
|
+
address: () => socAddress,
|
|
61
|
+
owner: () => ownerAddress
|
|
62
|
+
};
|
|
55
63
|
}
|
|
56
64
|
export function makeSOCAddress(identifier, address) {
|
|
57
|
-
|
|
65
|
+
return keccak256Hash(identifier, address);
|
|
58
66
|
}
|
|
59
67
|
/**
|
|
60
68
|
* Creates a single owner chunk object
|
|
@@ -63,22 +71,23 @@ export function makeSOCAddress(identifier, address) {
|
|
|
63
71
|
* @param identifier The identifier of the chunk
|
|
64
72
|
* @param signer The singer interface for signing the chunk
|
|
65
73
|
*/
|
|
74
|
+
|
|
66
75
|
export async function makeSingleOwnerChunk(chunk, identifier, signer) {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
76
|
+
const chunkAddress = chunk.address();
|
|
77
|
+
assertValidChunkData(chunk.data, chunkAddress);
|
|
78
|
+
const digest = keccak256Hash(identifier, chunkAddress);
|
|
79
|
+
const signature = await sign(signer, digest);
|
|
80
|
+
const data = serializeBytes(identifier, signature, chunk.span(), chunk.payload());
|
|
81
|
+
const address = makeSOCAddress(identifier, signer.address);
|
|
82
|
+
return {
|
|
83
|
+
data,
|
|
84
|
+
identifier: () => identifier,
|
|
85
|
+
signature: () => signature,
|
|
86
|
+
span: () => chunk.span(),
|
|
87
|
+
payload: () => chunk.payload(),
|
|
88
|
+
address: () => address,
|
|
89
|
+
owner: () => signer.address
|
|
90
|
+
};
|
|
82
91
|
}
|
|
83
92
|
/**
|
|
84
93
|
* Helper function to upload a chunk.
|
|
@@ -90,12 +99,13 @@ export async function makeSingleOwnerChunk(chunk, identifier, signer) {
|
|
|
90
99
|
* @param postageBatchId Postage BatchId that will be assigned to uploaded data
|
|
91
100
|
* @param options Upload options
|
|
92
101
|
*/
|
|
102
|
+
|
|
93
103
|
export async function uploadSingleOwnerChunk(ky, chunk, postageBatchId, options) {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
104
|
+
const owner = bytesToHex(chunk.owner());
|
|
105
|
+
const identifier = bytesToHex(chunk.identifier());
|
|
106
|
+
const signature = bytesToHex(chunk.signature());
|
|
107
|
+
const data = serializeBytes(chunk.span(), chunk.payload());
|
|
108
|
+
return socAPI.upload(ky, owner, identifier, signature, data, postageBatchId, options);
|
|
99
109
|
}
|
|
100
110
|
/**
|
|
101
111
|
* Helper function to create and upload SOC.
|
|
@@ -107,11 +117,12 @@ export async function uploadSingleOwnerChunk(ky, chunk, postageBatchId, options)
|
|
|
107
117
|
* @param data The chunk data
|
|
108
118
|
* @param options
|
|
109
119
|
*/
|
|
120
|
+
|
|
110
121
|
export async function uploadSingleOwnerChunkData(ky, signer, postageBatchId, identifier, data, options) {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
122
|
+
assertAddress(postageBatchId);
|
|
123
|
+
const cac = makeContentAddressedChunk(data);
|
|
124
|
+
const soc = await makeSingleOwnerChunk(cac, identifier, signer);
|
|
125
|
+
return uploadSingleOwnerChunk(ky, soc, postageBatchId, options);
|
|
115
126
|
}
|
|
116
127
|
/**
|
|
117
128
|
* Helper function to download SOC.
|
|
@@ -120,8 +131,9 @@ export async function uploadSingleOwnerChunkData(ky, signer, postageBatchId, ide
|
|
|
120
131
|
* @param ownerAddress The singer interface for signing the chunk
|
|
121
132
|
* @param identifier The identifier of the chunk
|
|
122
133
|
*/
|
|
134
|
+
|
|
123
135
|
export async function downloadSingleOwnerChunk(ky, ownerAddress, identifier) {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
}
|
|
136
|
+
const address = makeSOCAddress(identifier, ownerAddress);
|
|
137
|
+
const data = await chunkAPI.download(ky, bytesToHex(address));
|
|
138
|
+
return makeSingleOwnerChunkFromData(data, address);
|
|
139
|
+
}
|
package/dist/mjs/chunk/span.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { BeeArgumentError } from
|
|
2
|
-
export const SPAN_SIZE = 8;
|
|
3
|
-
|
|
1
|
+
import { BeeArgumentError } from "../utils/error.js";
|
|
2
|
+
export const SPAN_SIZE = 8; // we limit the maximum span size in 32 bits to avoid BigInt compatibility issues
|
|
3
|
+
|
|
4
4
|
const MAX_SPAN_LENGTH = 2 ** 32 - 1;
|
|
5
5
|
/**
|
|
6
6
|
* Create a span for storing the length of the chunk
|
|
@@ -9,17 +9,20 @@ const MAX_SPAN_LENGTH = 2 ** 32 - 1;
|
|
|
9
9
|
*
|
|
10
10
|
* @param length The length of the span
|
|
11
11
|
*/
|
|
12
|
+
|
|
12
13
|
export function makeSpan(length) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
14
|
+
if (length <= 0) {
|
|
15
|
+
throw new BeeArgumentError('invalid length for span', length);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (length > MAX_SPAN_LENGTH) {
|
|
19
|
+
throw new BeeArgumentError('invalid length (> MAX_SPAN_LENGTH)', length);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const span = new Uint8Array(SPAN_SIZE);
|
|
23
|
+
const dataView = new DataView(span.buffer);
|
|
24
|
+
const littleEndian = true;
|
|
25
|
+
const lengthLower32 = length & 0xffffffff;
|
|
26
|
+
dataView.setUint32(0, lengthLower32, littleEndian);
|
|
27
|
+
return span;
|
|
28
|
+
}
|