@ethersphere/bee-js 13.0.0-upcoming.gcfb9756 → 13.0.0
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 +104 -103
- package/dist/cjs/api/bytes.js +3 -4
- package/dist/cjs/api/bzz.js +5 -6
- package/dist/cjs/api/chunk.js +3 -3
- package/dist/cjs/api/envelope.js +3 -2
- package/dist/cjs/api/feed.js +4 -5
- package/dist/cjs/api/pin.js +2 -2
- package/dist/cjs/api/soc.js +2 -2
- package/dist/cjs/bee.js +18 -19
- package/dist/cjs/chunk/soc.js +21 -63
- package/dist/cjs/feed/identifier.js +3 -4
- package/dist/cjs/feed/index.js +17 -19
- package/dist/cjs/feed/retrievable.js +2 -2
- package/dist/cjs/index.js +18 -8
- package/dist/cjs/manifest/manifest.js +97 -333
- package/dist/cjs/modules/balance.js +3 -3
- package/dist/cjs/modules/cheque.js +4 -4
- package/dist/cjs/modules/chunk.js +8 -8
- package/dist/cjs/modules/collection.js +6 -6
- package/dist/cjs/modules/connectivity.js +3 -3
- package/dist/cjs/modules/data.js +6 -6
- package/dist/cjs/modules/feed.js +10 -10
- package/dist/cjs/modules/file.js +3 -3
- package/dist/cjs/modules/grantee.js +9 -9
- package/dist/cjs/modules/messaging.js +15 -17
- package/dist/cjs/modules/pin.js +6 -6
- package/dist/cjs/modules/settlement.js +2 -2
- package/dist/cjs/modules/soc.js +3 -3
- package/dist/cjs/modules/stamp.js +7 -7
- package/dist/cjs/modules/tag.js +2 -2
- package/dist/cjs/modules/transaction.js +4 -4
- package/dist/cjs/modules/wallet.js +3 -3
- package/dist/cjs/types/schema/chequebook.js +5 -5
- package/dist/cjs/types/schema/connectivity.js +5 -5
- package/dist/cjs/types/schema/envelope.js +5 -5
- package/dist/cjs/types/schema/grantee.js +4 -4
- package/dist/cjs/types/schema/stake.js +2 -2
- package/dist/cjs/types/schema/stamps.js +4 -4
- package/dist/cjs/types/schema/states.js +2 -2
- package/dist/cjs/types/schema/transactions.js +3 -3
- package/dist/cjs/types/schema/upload.js +2 -2
- package/dist/cjs/utils/chunk-stream.browser.js +24 -16
- package/dist/cjs/utils/chunk-stream.js +17 -9
- package/dist/cjs/utils/constants.js +3 -3
- package/dist/cjs/utils/expose.js +4 -4
- package/dist/cjs/utils/headers.js +4 -4
- package/dist/cjs/utils/http.js +3 -1
- package/dist/cjs/utils/pss.js +2 -2
- package/dist/cjs/utils/resource-locator.js +2 -2
- package/dist/cjs/utils/schema.js +4 -4
- package/dist/cjs/utils/stamps.js +15 -130
- package/dist/cjs/utils/type.js +2 -2
- package/dist/codemod/index.js +141 -0
- package/dist/codemod/transforms/v13.js +524 -0
- package/dist/index.browser.min.js +2 -1
- package/dist/index.browser.min.js.LICENSE.txt +3 -0
- package/dist/index.browser.min.js.map +1 -1
- package/dist/mjs/api/bytes.js +1 -2
- package/dist/mjs/api/bzz.js +1 -2
- package/dist/mjs/api/chunk.js +1 -1
- package/dist/mjs/api/envelope.js +3 -2
- package/dist/mjs/api/feed.js +1 -2
- package/dist/mjs/api/pin.js +1 -1
- package/dist/mjs/api/soc.js +1 -1
- package/dist/mjs/bee.js +13 -14
- package/dist/mjs/chunk/soc.js +11 -49
- package/dist/mjs/feed/identifier.js +2 -3
- package/dist/mjs/feed/index.js +6 -8
- package/dist/mjs/feed/retrievable.js +1 -1
- package/dist/mjs/index.js +2 -5
- package/dist/mjs/manifest/manifest.js +95 -339
- package/dist/mjs/modules/balance.js +1 -1
- package/dist/mjs/modules/cheque.js +1 -1
- package/dist/mjs/modules/chunk.js +1 -1
- package/dist/mjs/modules/collection.js +1 -1
- package/dist/mjs/modules/connectivity.js +1 -1
- package/dist/mjs/modules/data.js +3 -3
- package/dist/mjs/modules/feed.js +1 -1
- package/dist/mjs/modules/file.js +1 -1
- package/dist/mjs/modules/grantee.js +1 -1
- package/dist/mjs/modules/messaging.js +3 -5
- package/dist/mjs/modules/pin.js +1 -1
- package/dist/mjs/modules/settlement.js +1 -1
- package/dist/mjs/modules/soc.js +1 -1
- package/dist/mjs/modules/stamp.js +1 -1
- package/dist/mjs/modules/tag.js +1 -1
- package/dist/mjs/modules/transaction.js +1 -1
- package/dist/mjs/modules/wallet.js +1 -1
- package/dist/mjs/types/schema/chequebook.js +1 -1
- package/dist/mjs/types/schema/connectivity.js +1 -1
- package/dist/mjs/types/schema/envelope.js +5 -5
- package/dist/mjs/types/schema/grantee.js +1 -1
- package/dist/mjs/types/schema/stake.js +1 -1
- package/dist/mjs/types/schema/stamps.js +1 -1
- package/dist/mjs/types/schema/states.js +1 -1
- package/dist/mjs/types/schema/transactions.js +1 -1
- package/dist/mjs/types/schema/upload.js +1 -1
- package/dist/mjs/utils/chunk-stream.browser.js +28 -18
- package/dist/mjs/utils/chunk-stream.js +21 -11
- package/dist/mjs/utils/constants.js +1 -1
- package/dist/mjs/utils/expose.js +1 -1
- package/dist/mjs/utils/headers.js +1 -1
- package/dist/mjs/utils/http.js +6 -1
- package/dist/mjs/utils/pss.js +1 -1
- package/dist/mjs/utils/resource-locator.js +1 -1
- package/dist/mjs/utils/schema.js +1 -1
- package/dist/mjs/utils/stamps.js +5 -99
- package/dist/mjs/utils/type.js +2 -2
- package/dist/types/api/balance.d.ts +1 -1
- package/dist/types/api/batches.d.ts +1 -1
- package/dist/types/api/bytes.d.ts +3 -3
- package/dist/types/api/bzz.d.ts +1 -2
- package/dist/types/api/chequebook.d.ts +1 -1
- package/dist/types/api/chunk.d.ts +1 -1
- package/dist/types/api/connectivity.d.ts +1 -1
- package/dist/types/api/consumed.d.ts +1 -1
- package/dist/types/api/envelope.d.ts +1 -1
- package/dist/types/api/feed.d.ts +1 -2
- package/dist/types/api/grantee.d.ts +1 -1
- package/dist/types/api/pin.d.ts +1 -1
- package/dist/types/api/pss.d.ts +1 -1
- package/dist/types/api/settlement.d.ts +1 -1
- package/dist/types/api/soc.d.ts +1 -1
- package/dist/types/api/stake.d.ts +1 -1
- package/dist/types/api/stamp.d.ts +1 -1
- package/dist/types/api/stewardship.d.ts +1 -1
- package/dist/types/api/tag.d.ts +1 -1
- package/dist/types/api/transaction.d.ts +1 -1
- package/dist/types/api/wallet.d.ts +1 -1
- package/dist/types/bee.d.ts +11 -13
- package/dist/types/chunk/soc.d.ts +3 -48
- package/dist/types/feed/identifier.d.ts +1 -1
- package/dist/types/feed/index.d.ts +1 -3
- package/dist/types/feed/retrievable.d.ts +1 -1
- package/dist/types/index.d.ts +17 -20
- package/dist/types/manifest/manifest.d.ts +24 -33
- package/dist/types/modules/balance.d.ts +1 -1
- package/dist/types/modules/cheque.d.ts +1 -1
- package/dist/types/modules/chequebook.d.ts +1 -1
- package/dist/types/modules/chunk.d.ts +1 -2
- package/dist/types/modules/collection.d.ts +2 -2
- package/dist/types/modules/connectivity.d.ts +1 -1
- package/dist/types/modules/context.d.ts +1 -1
- package/dist/types/modules/data.d.ts +3 -3
- package/dist/types/modules/feed.d.ts +1 -1
- package/dist/types/modules/file.d.ts +1 -2
- package/dist/types/modules/grantee.d.ts +1 -1
- package/dist/types/modules/messaging.d.ts +1 -2
- package/dist/types/modules/pin.d.ts +1 -1
- package/dist/types/modules/settlement.d.ts +1 -1
- package/dist/types/modules/soc.d.ts +1 -1
- package/dist/types/modules/stake.d.ts +1 -1
- package/dist/types/modules/stamp.d.ts +1 -1
- package/dist/types/modules/storage.d.ts +1 -1
- package/dist/types/modules/tag.d.ts +1 -1
- package/dist/types/modules/transaction.d.ts +1 -1
- package/dist/types/modules/wallet.d.ts +1 -1
- package/dist/types/types/debug.d.ts +1 -1
- package/dist/types/types/index.d.ts +2 -11
- package/dist/types/types/schema/chequebook.d.ts +1 -1
- package/dist/types/types/schema/connectivity.d.ts +1 -1
- package/dist/types/types/schema/grantee.d.ts +1 -1
- package/dist/types/types/schema/stake.d.ts +1 -1
- package/dist/types/types/schema/stamps.d.ts +1 -1
- package/dist/types/types/schema/states.d.ts +1 -1
- package/dist/types/types/schema/transactions.d.ts +1 -1
- package/dist/types/types/schema/upload.d.ts +1 -1
- package/dist/types/utils/chunk-stream.browser.d.ts +1 -1
- package/dist/types/utils/chunk-stream.d.ts +2 -2
- package/dist/types/utils/constants.d.ts +1 -1
- package/dist/types/utils/expose.d.ts +1 -1
- package/dist/types/utils/headers.d.ts +1 -1
- package/dist/types/utils/pss.d.ts +1 -1
- package/dist/types/utils/resource-locator.d.ts +1 -1
- package/dist/types/utils/schema.d.ts +1 -1
- package/dist/types/utils/stamps.d.ts +2 -27
- package/dist/types/utils/tar-uploader.browser.d.ts +1 -1
- package/dist/types/utils/tar-uploader.d.ts +1 -1
- package/dist/types/utils/type.d.ts +1 -1
- package/package.json +7 -2
- package/dist/cjs/chunk/bmt.js +0 -37
- package/dist/cjs/chunk/cac.js +0 -43
- package/dist/cjs/stamper/stamper.js +0 -44
- package/dist/cjs/utils/bytes.js +0 -116
- package/dist/cjs/utils/cid.js +0 -37
- package/dist/cjs/utils/redundancy.js +0 -133
- package/dist/cjs/utils/typed-bytes.js +0 -186
- package/dist/mjs/chunk/bmt.js +0 -34
- package/dist/mjs/chunk/cac.js +0 -34
- package/dist/mjs/stamper/stamper.js +0 -40
- package/dist/mjs/utils/bytes.js +0 -102
- package/dist/mjs/utils/cid.js +0 -32
- package/dist/mjs/utils/redundancy.js +0 -98
- package/dist/mjs/utils/typed-bytes.js +0 -167
- package/dist/types/chunk/bmt.d.ts +0 -16
- package/dist/types/chunk/cac.d.ts +0 -40
- package/dist/types/stamper/stamper.d.ts +0 -15
- package/dist/types/utils/bytes.d.ts +0 -19
- package/dist/types/utils/cid.d.ts +0 -10
- package/dist/types/utils/redundancy.d.ts +0 -21
- package/dist/types/utils/typed-bytes.d.ts +0 -69
|
@@ -1,186 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.FeedIndex = exports.Topic = exports.Signature = exports.BatchId = exports.PeerAddress = exports.Span = exports.TransactionId = exports.Reference = exports.Identifier = exports.EthAddress = exports.PublicKey = exports.PrivateKey = void 0;
|
|
4
|
-
const cafe_utility_1 = require("cafe-utility");
|
|
5
|
-
const bytes_1 = require("./bytes");
|
|
6
|
-
const cid_1 = require("./cid");
|
|
7
|
-
// TODO: add JSdocs for each class
|
|
8
|
-
const ENCODER = new TextEncoder();
|
|
9
|
-
class PrivateKey extends bytes_1.Bytes {
|
|
10
|
-
constructor(bytes) {
|
|
11
|
-
super(bytes, 32);
|
|
12
|
-
}
|
|
13
|
-
publicKey() {
|
|
14
|
-
const [x, y] = cafe_utility_1.Elliptic.privateKeyToPublicKey(cafe_utility_1.Binary.uint256ToNumber(this.bytes, 'BE'));
|
|
15
|
-
return new PublicKey(cafe_utility_1.Binary.concatBytes(cafe_utility_1.Binary.numberToUint256(x, 'BE'), cafe_utility_1.Binary.numberToUint256(y, 'BE')));
|
|
16
|
-
}
|
|
17
|
-
sign(data) {
|
|
18
|
-
const digest = cafe_utility_1.Binary.concatBytes(ENCODER.encode(`\x19Ethereum Signed Message:\n32`), cafe_utility_1.Binary.keccak256(data instanceof Uint8Array ? data : ENCODER.encode(data)));
|
|
19
|
-
const [r, s, v] = cafe_utility_1.Elliptic.signMessage(digest, cafe_utility_1.Binary.uint256ToNumber(this.bytes, 'BE'));
|
|
20
|
-
return new Signature(cafe_utility_1.Binary.concatBytes(cafe_utility_1.Binary.numberToUint256(r, 'BE'), cafe_utility_1.Binary.numberToUint256(s, 'BE'), new Uint8Array([Number(v)])));
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
exports.PrivateKey = PrivateKey;
|
|
24
|
-
PrivateKey.LENGTH = 32;
|
|
25
|
-
class PublicKey extends bytes_1.Bytes {
|
|
26
|
-
constructor(bytes) {
|
|
27
|
-
const b = new bytes_1.Bytes(bytes);
|
|
28
|
-
if (b.length === 33) {
|
|
29
|
-
const [x, y] = cafe_utility_1.Elliptic.publicKeyFromCompressed(b.toUint8Array());
|
|
30
|
-
super(cafe_utility_1.Binary.concatBytes(cafe_utility_1.Binary.numberToUint256(x, 'BE'), cafe_utility_1.Binary.numberToUint256(y, 'BE')), 64);
|
|
31
|
-
}
|
|
32
|
-
else {
|
|
33
|
-
super(bytes, 64);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
address() {
|
|
37
|
-
const x = cafe_utility_1.Binary.uint256ToNumber(this.bytes.slice(0, 32), 'BE');
|
|
38
|
-
const y = cafe_utility_1.Binary.uint256ToNumber(this.bytes.slice(32, 64), 'BE');
|
|
39
|
-
return new EthAddress(cafe_utility_1.Elliptic.publicKeyToAddress([x, y]));
|
|
40
|
-
}
|
|
41
|
-
toCompressedUint8Array() {
|
|
42
|
-
const x = cafe_utility_1.Binary.uint256ToNumber(this.bytes.slice(0, 32), 'BE');
|
|
43
|
-
const y = cafe_utility_1.Binary.uint256ToNumber(this.bytes.slice(32, 64), 'BE');
|
|
44
|
-
return cafe_utility_1.Elliptic.compressPublicKey([x, y]);
|
|
45
|
-
}
|
|
46
|
-
toCompressedHex() {
|
|
47
|
-
return cafe_utility_1.Binary.uint8ArrayToHex(this.toCompressedUint8Array());
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
exports.PublicKey = PublicKey;
|
|
51
|
-
PublicKey.LENGTH = 64;
|
|
52
|
-
class EthAddress extends bytes_1.Bytes {
|
|
53
|
-
constructor(bytes) {
|
|
54
|
-
super(bytes, 20);
|
|
55
|
-
}
|
|
56
|
-
toChecksum() {
|
|
57
|
-
return cafe_utility_1.Elliptic.checksumEncode(this.bytes);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
exports.EthAddress = EthAddress;
|
|
61
|
-
EthAddress.LENGTH = 20;
|
|
62
|
-
class Identifier extends bytes_1.Bytes {
|
|
63
|
-
constructor(bytes) {
|
|
64
|
-
super(bytes, 32);
|
|
65
|
-
}
|
|
66
|
-
static fromString(value) {
|
|
67
|
-
return new Identifier(cafe_utility_1.Binary.keccak256(ENCODER.encode(value)));
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
exports.Identifier = Identifier;
|
|
71
|
-
Identifier.LENGTH = 32;
|
|
72
|
-
class Reference extends bytes_1.Bytes {
|
|
73
|
-
constructor(bytes) {
|
|
74
|
-
if (typeof bytes === 'string' && bytes.startsWith('bah5')) {
|
|
75
|
-
const decoded = (0, cid_1.convertCidToReference)(bytes);
|
|
76
|
-
super(decoded.reference.bytes, 32);
|
|
77
|
-
}
|
|
78
|
-
else {
|
|
79
|
-
super(bytes, [32, 64]);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
toCid(type) {
|
|
83
|
-
return (0, cid_1.convertReferenceToCid)(this.bytes, type);
|
|
84
|
-
}
|
|
85
|
-
static isValid(value) {
|
|
86
|
-
try {
|
|
87
|
-
new Reference(value);
|
|
88
|
-
return true;
|
|
89
|
-
}
|
|
90
|
-
catch {
|
|
91
|
-
return false;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
exports.Reference = Reference;
|
|
96
|
-
Reference.LENGTH = 32;
|
|
97
|
-
class TransactionId extends bytes_1.Bytes {
|
|
98
|
-
constructor(bytes) {
|
|
99
|
-
super(bytes, 32);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
exports.TransactionId = TransactionId;
|
|
103
|
-
TransactionId.LENGTH = 32;
|
|
104
|
-
class Span extends bytes_1.Bytes {
|
|
105
|
-
constructor(bytes) {
|
|
106
|
-
super(bytes, 8);
|
|
107
|
-
}
|
|
108
|
-
static fromBigInt(number) {
|
|
109
|
-
return new Span(cafe_utility_1.Binary.numberToUint64(number, 'LE'));
|
|
110
|
-
}
|
|
111
|
-
toBigInt() {
|
|
112
|
-
return cafe_utility_1.Binary.uint64ToNumber(this.bytes, 'LE');
|
|
113
|
-
}
|
|
114
|
-
static fromSlice(bytes, start) {
|
|
115
|
-
return new Span(bytes.slice(start, start + Span.LENGTH));
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
exports.Span = Span;
|
|
119
|
-
Span.LENGTH = 8;
|
|
120
|
-
class PeerAddress extends bytes_1.Bytes {
|
|
121
|
-
constructor(bytes) {
|
|
122
|
-
super(bytes, 32);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
exports.PeerAddress = PeerAddress;
|
|
126
|
-
PeerAddress.LENGTH = 32;
|
|
127
|
-
class BatchId extends bytes_1.Bytes {
|
|
128
|
-
constructor(bytes) {
|
|
129
|
-
super(bytes, 32);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
exports.BatchId = BatchId;
|
|
133
|
-
BatchId.LENGTH = 32;
|
|
134
|
-
class Signature extends bytes_1.Bytes {
|
|
135
|
-
constructor(bytes) {
|
|
136
|
-
super(bytes, 65);
|
|
137
|
-
}
|
|
138
|
-
static fromSlice(bytes, start) {
|
|
139
|
-
return new Signature(bytes.slice(start, start + Signature.LENGTH));
|
|
140
|
-
}
|
|
141
|
-
recoverPublicKey(digest) {
|
|
142
|
-
const r = cafe_utility_1.Binary.uint256ToNumber(this.bytes.slice(0, 32), 'BE');
|
|
143
|
-
const s = cafe_utility_1.Binary.uint256ToNumber(this.bytes.slice(32, 64), 'BE');
|
|
144
|
-
const v = BigInt(this.bytes[64]);
|
|
145
|
-
const [x, y] = cafe_utility_1.Elliptic.recoverPublicKey(cafe_utility_1.Binary.concatBytes(ENCODER.encode(`\x19Ethereum Signed Message:\n32`), cafe_utility_1.Binary.keccak256(digest instanceof Uint8Array ? digest : ENCODER.encode(digest))), r, s, v);
|
|
146
|
-
return new PublicKey(cafe_utility_1.Binary.concatBytes(cafe_utility_1.Binary.numberToUint256(x, 'BE'), cafe_utility_1.Binary.numberToUint256(y, 'BE')));
|
|
147
|
-
}
|
|
148
|
-
isValid(digest, expectedAddress) {
|
|
149
|
-
const publicKey = this.recoverPublicKey(digest);
|
|
150
|
-
const address = publicKey.address();
|
|
151
|
-
return address.equals(expectedAddress);
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
exports.Signature = Signature;
|
|
155
|
-
Signature.LENGTH = 65;
|
|
156
|
-
class Topic extends bytes_1.Bytes {
|
|
157
|
-
constructor(bytes) {
|
|
158
|
-
super(bytes, 32);
|
|
159
|
-
}
|
|
160
|
-
static fromString(value) {
|
|
161
|
-
return new Topic(cafe_utility_1.Binary.keccak256(ENCODER.encode(value)));
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
exports.Topic = Topic;
|
|
165
|
-
Topic.LENGTH = 32;
|
|
166
|
-
const MAX_UINT64 = new Uint8Array(8).fill(0xff, 0, 8);
|
|
167
|
-
class FeedIndex extends bytes_1.Bytes {
|
|
168
|
-
constructor(bytes) {
|
|
169
|
-
super(bytes, 8);
|
|
170
|
-
}
|
|
171
|
-
static fromBigInt(number) {
|
|
172
|
-
return new FeedIndex(cafe_utility_1.Binary.numberToUint64(number, 'BE'));
|
|
173
|
-
}
|
|
174
|
-
toBigInt() {
|
|
175
|
-
return cafe_utility_1.Binary.uint64ToNumber(this.bytes, 'BE');
|
|
176
|
-
}
|
|
177
|
-
next() {
|
|
178
|
-
if (cafe_utility_1.Binary.equals(this.bytes, MAX_UINT64)) {
|
|
179
|
-
return FeedIndex.fromBigInt(0n);
|
|
180
|
-
}
|
|
181
|
-
return FeedIndex.fromBigInt(this.toBigInt() + 1n);
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
exports.FeedIndex = FeedIndex;
|
|
185
|
-
FeedIndex.LENGTH = 8;
|
|
186
|
-
FeedIndex.MINUS_ONE = new FeedIndex(MAX_UINT64);
|
package/dist/mjs/chunk/bmt.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { Binary } from 'cafe-utility';
|
|
2
|
-
import { BeeArgumentError } from "../utils/error.js";
|
|
3
|
-
import { Reference, Span } from "../utils/typed-bytes.js";
|
|
4
|
-
const MAX_CHUNK_PAYLOAD_SIZE = 4096;
|
|
5
|
-
const SEGMENT_SIZE = 32;
|
|
6
|
-
/**
|
|
7
|
-
* Calculate a Binary Merkle Tree hash for a chunk
|
|
8
|
-
*
|
|
9
|
-
* The BMT chunk address is the hash of the 8 byte span and the root
|
|
10
|
-
* hash of a binary Merkle tree (BMT) built on the 32-byte segments
|
|
11
|
-
* of the underlying data.
|
|
12
|
-
*
|
|
13
|
-
* If the chunk content is less than 4k, the hash is calculated as
|
|
14
|
-
* if the chunk was padded with all zeros up to 4096 bytes.
|
|
15
|
-
*
|
|
16
|
-
* @param chunkContent Chunk data including span and payload as well
|
|
17
|
-
*
|
|
18
|
-
* @returns the keccak256 hash in a byte array
|
|
19
|
-
*/
|
|
20
|
-
export function calculateChunkAddress(chunkContent) {
|
|
21
|
-
const span = chunkContent.slice(0, Span.LENGTH);
|
|
22
|
-
const payload = chunkContent.slice(Span.LENGTH);
|
|
23
|
-
const rootHash = calculateBmtRootHash(payload);
|
|
24
|
-
const chunkHash = Binary.keccak256(Binary.concatBytes(span, rootHash));
|
|
25
|
-
return new Reference(chunkHash);
|
|
26
|
-
}
|
|
27
|
-
function calculateBmtRootHash(payload) {
|
|
28
|
-
if (payload.length > MAX_CHUNK_PAYLOAD_SIZE) {
|
|
29
|
-
throw new BeeArgumentError(`payload size ${payload.length} exceeds maximum chunk payload size ${MAX_CHUNK_PAYLOAD_SIZE}`, payload);
|
|
30
|
-
}
|
|
31
|
-
const input = new Uint8Array(MAX_CHUNK_PAYLOAD_SIZE);
|
|
32
|
-
input.set(payload);
|
|
33
|
-
return Binary.log2Reduce(Binary.partition(input, SEGMENT_SIZE), (a, b) => Binary.keccak256(Binary.concatBytes(a, b)));
|
|
34
|
-
}
|
package/dist/mjs/chunk/cac.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { Binary } from 'cafe-utility';
|
|
2
|
-
import { z } from 'zod';
|
|
3
|
-
import { Bytes } from "../utils/bytes.js";
|
|
4
|
-
import { Span } from "../utils/typed-bytes.js";
|
|
5
|
-
import { calculateChunkAddress } from "./bmt.js";
|
|
6
|
-
import { makeSingleOwnerChunk } from "./soc.js";
|
|
7
|
-
export const MIN_PAYLOAD_SIZE = 1;
|
|
8
|
-
export const MAX_PAYLOAD_SIZE = 4096;
|
|
9
|
-
export function unmarshalContentAddressedChunk(data) {
|
|
10
|
-
data = new Bytes(data);
|
|
11
|
-
return makeContentAddressedChunk(data.toUint8Array().slice(Span.LENGTH), Span.fromSlice(data.toUint8Array(), 0));
|
|
12
|
-
}
|
|
13
|
-
export function makeContentAddressedChunk(rawPayload, span) {
|
|
14
|
-
const asString = z.string().safeParse(rawPayload);
|
|
15
|
-
if (asString.success) {
|
|
16
|
-
rawPayload = Bytes.fromUtf8(asString.data);
|
|
17
|
-
}
|
|
18
|
-
if (rawPayload.length < MIN_PAYLOAD_SIZE || rawPayload.length > MAX_PAYLOAD_SIZE) {
|
|
19
|
-
throw new RangeError(`payload size ${rawPayload.length} exceeds limits [${MIN_PAYLOAD_SIZE}, ${MAX_PAYLOAD_SIZE}]`);
|
|
20
|
-
}
|
|
21
|
-
const typedSpan = span ? typeof span === 'bigint' ? Span.fromBigInt(span) : span : Span.fromBigInt(BigInt(rawPayload.length));
|
|
22
|
-
const payload = new Bytes(rawPayload);
|
|
23
|
-
const data = Binary.concatBytes(typedSpan.toUint8Array(), payload.toUint8Array());
|
|
24
|
-
const address = calculateChunkAddress(data);
|
|
25
|
-
return {
|
|
26
|
-
data,
|
|
27
|
-
span: typedSpan,
|
|
28
|
-
payload,
|
|
29
|
-
address,
|
|
30
|
-
toSingleOwnerChunk: (identifier, signer) => {
|
|
31
|
-
return makeSingleOwnerChunk(address, typedSpan, payload, identifier, signer);
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
|
-
}
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { Binary } from 'cafe-utility';
|
|
2
|
-
import { BatchId, PrivateKey } from "../utils/typed-bytes.js";
|
|
3
|
-
export class Stamper {
|
|
4
|
-
constructor(signer, batchId, buckets, depth) {
|
|
5
|
-
this.signer = new PrivateKey(signer);
|
|
6
|
-
this.batchId = new BatchId(batchId);
|
|
7
|
-
this.buckets = buckets;
|
|
8
|
-
this.depth = depth;
|
|
9
|
-
this.maxSlot = 2 ** (this.depth - 16);
|
|
10
|
-
}
|
|
11
|
-
static fromBlank(signer, batchId, depth) {
|
|
12
|
-
return new Stamper(signer, batchId, new Uint32Array(65536), depth);
|
|
13
|
-
}
|
|
14
|
-
static fromState(signer, batchId, buckets, depth) {
|
|
15
|
-
return new Stamper(signer, batchId, buckets, depth);
|
|
16
|
-
}
|
|
17
|
-
stamp(chunk) {
|
|
18
|
-
const address = chunk.hash();
|
|
19
|
-
const bucket = Binary.uint16ToNumber(address, 'BE');
|
|
20
|
-
const height = this.buckets[bucket];
|
|
21
|
-
if (height >= this.maxSlot) {
|
|
22
|
-
throw Error('Bucket is full');
|
|
23
|
-
}
|
|
24
|
-
this.buckets[bucket]++;
|
|
25
|
-
const index = Binary.concatBytes(Binary.numberToUint32(bucket, 'BE'), Binary.numberToUint32(height, 'BE'));
|
|
26
|
-
const currentTimeNs = BigInt(Date.now()) * 1000000n; // Bee uses unix timestamp in nanoseconds
|
|
27
|
-
const timestamp = Binary.numberToUint64(currentTimeNs, 'BE');
|
|
28
|
-
const signature = this.signer.sign(Binary.concatBytes(address, this.batchId.toUint8Array(), index, timestamp));
|
|
29
|
-
return {
|
|
30
|
-
batchId: this.batchId,
|
|
31
|
-
index,
|
|
32
|
-
issuer: this.signer.publicKey().address().toUint8Array(),
|
|
33
|
-
signature: signature.toUint8Array(),
|
|
34
|
-
timestamp
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
getState() {
|
|
38
|
-
return this.buckets;
|
|
39
|
-
}
|
|
40
|
-
}
|
package/dist/mjs/utils/bytes.js
DELETED
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
import { Binary, Objects } from 'cafe-utility';
|
|
2
|
-
import _debug from 'debug';
|
|
3
|
-
import { z } from 'zod';
|
|
4
|
-
import { HexStringSchema } from "./hex-schema.js";
|
|
5
|
-
const debug = _debug('bee-js:bytes');
|
|
6
|
-
const DECODER = new TextDecoder();
|
|
7
|
-
const ENCODER = new TextEncoder();
|
|
8
|
-
export class Bytes {
|
|
9
|
-
constructor(bytes, byteLength) {
|
|
10
|
-
if (!bytes) {
|
|
11
|
-
throw Error(`Bytes#constructor: constructor parameter is falsy: ${bytes}`);
|
|
12
|
-
}
|
|
13
|
-
if (bytes instanceof Bytes) {
|
|
14
|
-
this.bytes = bytes.bytes;
|
|
15
|
-
} else if (typeof bytes === 'string') {
|
|
16
|
-
this.bytes = Binary.hexToUint8Array(HexStringSchema.parse(bytes));
|
|
17
|
-
} else if (bytes instanceof ArrayBuffer) {
|
|
18
|
-
this.bytes = new Uint8Array(bytes);
|
|
19
|
-
} else if (bytes instanceof Uint8Array) {
|
|
20
|
-
this.bytes = bytes;
|
|
21
|
-
} else {
|
|
22
|
-
const unknownInput = bytes;
|
|
23
|
-
const toHex = Objects.getDeep(unknownInput, 'toHex');
|
|
24
|
-
if (z.function().safeParse(toHex).success) {
|
|
25
|
-
const hex = toHex.call(unknownInput);
|
|
26
|
-
this.bytes = Binary.hexToUint8Array(HexStringSchema.parse(hex));
|
|
27
|
-
} else {
|
|
28
|
-
debug('bytes', bytes);
|
|
29
|
-
throw new Error(`Bytes#constructor: unsupported type: ${typeof bytes}`);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
this.length = this.bytes.length;
|
|
33
|
-
if (byteLength) {
|
|
34
|
-
if (Array.isArray(byteLength)) {
|
|
35
|
-
if (!byteLength.includes(this.length)) {
|
|
36
|
-
throw new Error(`Bytes#checkByteLength: bytes length is ${this.length} but expected ${byteLength.join(' or ')}`);
|
|
37
|
-
}
|
|
38
|
-
} else if (this.length !== byteLength) {
|
|
39
|
-
throw new Error(`Bytes#checkByteLength: bytes length is ${this.length} but expected ${byteLength}`);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
static keccak256(bytes) {
|
|
44
|
-
return new Bytes(Binary.keccak256(new Bytes(bytes).toUint8Array()));
|
|
45
|
-
}
|
|
46
|
-
static fromUtf8(utf8) {
|
|
47
|
-
return new Bytes(ENCODER.encode(utf8));
|
|
48
|
-
}
|
|
49
|
-
static fromSlice(bytes, start, length) {
|
|
50
|
-
if (length === undefined) {
|
|
51
|
-
return new Bytes(bytes.slice(start));
|
|
52
|
-
}
|
|
53
|
-
return new Bytes(bytes.slice(start, start + length));
|
|
54
|
-
}
|
|
55
|
-
offset(index) {
|
|
56
|
-
return new Uint8Array(this.bytes.slice(index));
|
|
57
|
-
}
|
|
58
|
-
toUint8Array() {
|
|
59
|
-
return new Uint8Array(this.bytes);
|
|
60
|
-
}
|
|
61
|
-
toHex() {
|
|
62
|
-
return Binary.uint8ArrayToHex(this.bytes);
|
|
63
|
-
}
|
|
64
|
-
toBase64() {
|
|
65
|
-
return Binary.uint8ArrayToBase64(this.bytes);
|
|
66
|
-
}
|
|
67
|
-
toBase32() {
|
|
68
|
-
return Binary.uint8ArrayToBase32(this.bytes);
|
|
69
|
-
}
|
|
70
|
-
toString() {
|
|
71
|
-
return this.toHex();
|
|
72
|
-
}
|
|
73
|
-
toUtf8() {
|
|
74
|
-
return DECODER.decode(this.bytes);
|
|
75
|
-
}
|
|
76
|
-
toJSON() {
|
|
77
|
-
return JSON.parse(this.toUtf8());
|
|
78
|
-
}
|
|
79
|
-
equals(other) {
|
|
80
|
-
return this.toHex() === new Bytes(other).toHex();
|
|
81
|
-
}
|
|
82
|
-
represent() {
|
|
83
|
-
return this.toHex();
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
export function parseSizeToBytes(sizeStr) {
|
|
87
|
-
const units = {
|
|
88
|
-
B: 1,
|
|
89
|
-
kB: 1000,
|
|
90
|
-
MB: 1000 ** 2,
|
|
91
|
-
GB: 1000 ** 3,
|
|
92
|
-
TB: 1000 ** 4,
|
|
93
|
-
PB: 1000 ** 5
|
|
94
|
-
};
|
|
95
|
-
const match = sizeStr.match(/^([\d.]+)\s*(B|kB|MB|GB|TB|PB)$/);
|
|
96
|
-
if (!match) {
|
|
97
|
-
throw new Error(`Invalid size format: ${sizeStr}`);
|
|
98
|
-
}
|
|
99
|
-
const value = parseFloat(match[1]);
|
|
100
|
-
const unit = match[2];
|
|
101
|
-
return Math.ceil(value * units[unit]);
|
|
102
|
-
}
|
package/dist/mjs/utils/cid.js
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { Binary } from 'cafe-utility';
|
|
2
|
-
import { Reference } from "./typed-bytes.js";
|
|
3
|
-
export const SWARM_MANIFEST_CODEC = 0xfa;
|
|
4
|
-
export const SWARM_FEED_CODEC = 0xfb;
|
|
5
|
-
const CODEC_TABLE = {
|
|
6
|
-
[SWARM_MANIFEST_CODEC]: 'manifest',
|
|
7
|
-
[SWARM_FEED_CODEC]: 'feed'
|
|
8
|
-
};
|
|
9
|
-
export function convertReferenceToCid(reference, type) {
|
|
10
|
-
reference = new Reference(reference);
|
|
11
|
-
const base32 = 'b';
|
|
12
|
-
const version = new Uint8Array([1]);
|
|
13
|
-
const codec = new Uint8Array([type === 'feed' ? SWARM_FEED_CODEC : SWARM_MANIFEST_CODEC]);
|
|
14
|
-
const unknown = new Uint8Array([1]);
|
|
15
|
-
const sha256 = new Uint8Array([27]);
|
|
16
|
-
const size = new Uint8Array([32]);
|
|
17
|
-
const header = Binary.uint8ArrayToBase32(Binary.concatBytes(version, codec, unknown, sha256, size)).replace(/\=+$/, '');
|
|
18
|
-
const hash = reference.toBase32().replace(/\=+$/, '');
|
|
19
|
-
return `${base32}${header}${hash}`.toLowerCase();
|
|
20
|
-
}
|
|
21
|
-
export function convertCidToReference(cid) {
|
|
22
|
-
const bytes = Binary.base32ToUint8Array(cid.toUpperCase().slice(1));
|
|
23
|
-
const codec = bytes[1];
|
|
24
|
-
if (!CODEC_TABLE[codec]) {
|
|
25
|
-
throw new Error('Unknown codec');
|
|
26
|
-
}
|
|
27
|
-
const reference = new Reference(bytes.slice(-32));
|
|
28
|
-
return {
|
|
29
|
-
type: CODEC_TABLE[codec],
|
|
30
|
-
reference
|
|
31
|
-
};
|
|
32
|
-
}
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import { RedundancyLevel } from "../index.js";
|
|
2
|
-
const mediumTable = [[94, 68, 46, 28, 14, 5, 1], [9, 8, 7, 6, 5, 4, 3]];
|
|
3
|
-
const encMediumTable = [[47, 34, 23, 14, 7, 2], [9, 8, 7, 6, 5, 4]];
|
|
4
|
-
const strongTable = [[104, 95, 86, 77, 69, 61, 53, 46, 39, 32, 26, 20, 15, 10, 6, 3, 1], [21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5]];
|
|
5
|
-
const encStrongTable = [[52, 47, 43, 38, 34, 30, 26, 23, 19, 16, 13, 10, 7, 5, 3, 1], [21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6]];
|
|
6
|
-
const insaneTable = [[92, 87, 82, 77, 73, 68, 63, 59, 54, 50, 45, 41, 37, 33, 29, 26, 22, 19, 16, 13, 10, 8, 5, 3, 2, 1], [31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6]];
|
|
7
|
-
const encInsaneTable = [[46, 43, 41, 38, 36, 34, 31, 29, 27, 25, 22, 20, 18, 16, 14, 13, 11, 9, 8, 6, 5, 4, 2, 1], [31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 7]];
|
|
8
|
-
const paranoidTable = [[37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1], [90, 88, 87, 85, 84, 82, 81, 79, 77, 76, 74, 72, 71, 69, 67, 66, 64, 62, 60, 59, 57, 55, 53, 51, 49, 48, 46, 44, 41, 39, 37, 35, 32, 30, 27, 24, 20]];
|
|
9
|
-
const encParanoidTable = [[18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1], [88, 85, 82, 79, 76, 72, 69, 66, 62, 59, 55, 51, 48, 44, 39, 35, 30, 24]];
|
|
10
|
-
/**
|
|
11
|
-
* Returns an approximate multiplier for the overhead of a given redundancy level.
|
|
12
|
-
* Redundancy level is a tradeoff between storage overhead and fault tolerance.
|
|
13
|
-
* Use this number to estimate the amount of chunks that will be stored for a given
|
|
14
|
-
* redundancy level.
|
|
15
|
-
*/
|
|
16
|
-
export function approximateOverheadForRedundancyLevel(chunks, level, encrypted) {
|
|
17
|
-
if (level === RedundancyLevel.OFF) {
|
|
18
|
-
return 0;
|
|
19
|
-
}
|
|
20
|
-
const table = selectTable(level, encrypted);
|
|
21
|
-
const [supportedChunks, parities] = table;
|
|
22
|
-
for (let i = 0; i < supportedChunks.length; i++) {
|
|
23
|
-
if (chunks >= supportedChunks[i]) {
|
|
24
|
-
return parities[i] / supportedChunks[i];
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
return parities[parities.length - 1] / supportedChunks[supportedChunks.length - 1];
|
|
28
|
-
}
|
|
29
|
-
function selectTable(level, encrypted) {
|
|
30
|
-
switch (level) {
|
|
31
|
-
case RedundancyLevel.MEDIUM:
|
|
32
|
-
return encrypted ? encMediumTable : mediumTable;
|
|
33
|
-
case RedundancyLevel.STRONG:
|
|
34
|
-
return encrypted ? encStrongTable : strongTable;
|
|
35
|
-
case RedundancyLevel.INSANE:
|
|
36
|
-
return encrypted ? encInsaneTable : insaneTable;
|
|
37
|
-
case RedundancyLevel.PARANOID:
|
|
38
|
-
return encrypted ? encParanoidTable : paranoidTable;
|
|
39
|
-
default:
|
|
40
|
-
throw new Error(`Unknown redundancy level ${level}`);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
const medium = {
|
|
44
|
-
label: 'medium',
|
|
45
|
-
value: RedundancyLevel.MEDIUM,
|
|
46
|
-
errorTolerance: 0.01
|
|
47
|
-
};
|
|
48
|
-
const strong = {
|
|
49
|
-
label: 'strong',
|
|
50
|
-
value: RedundancyLevel.STRONG,
|
|
51
|
-
errorTolerance: 0.05
|
|
52
|
-
};
|
|
53
|
-
const insane = {
|
|
54
|
-
label: 'insane',
|
|
55
|
-
value: RedundancyLevel.INSANE,
|
|
56
|
-
errorTolerance: 0.1
|
|
57
|
-
};
|
|
58
|
-
const paranoid = {
|
|
59
|
-
label: 'paranoid',
|
|
60
|
-
value: RedundancyLevel.PARANOID,
|
|
61
|
-
errorTolerance: 0.5
|
|
62
|
-
};
|
|
63
|
-
export function getRedundancyStats() {
|
|
64
|
-
return {
|
|
65
|
-
medium,
|
|
66
|
-
strong,
|
|
67
|
-
insane,
|
|
68
|
-
paranoid
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
export function getRedundancyStat(level) {
|
|
72
|
-
if (typeof level === 'string') {
|
|
73
|
-
switch (level.toLowerCase()) {
|
|
74
|
-
case 'medium':
|
|
75
|
-
return medium;
|
|
76
|
-
case 'strong':
|
|
77
|
-
return strong;
|
|
78
|
-
case 'insane':
|
|
79
|
-
return insane;
|
|
80
|
-
case 'paranoid':
|
|
81
|
-
return paranoid;
|
|
82
|
-
default:
|
|
83
|
-
throw new Error(`Unknown redundancy level '${level}'`);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
switch (level) {
|
|
87
|
-
case RedundancyLevel.MEDIUM:
|
|
88
|
-
return medium;
|
|
89
|
-
case RedundancyLevel.STRONG:
|
|
90
|
-
return strong;
|
|
91
|
-
case RedundancyLevel.INSANE:
|
|
92
|
-
return insane;
|
|
93
|
-
case RedundancyLevel.PARANOID:
|
|
94
|
-
return paranoid;
|
|
95
|
-
default:
|
|
96
|
-
throw new Error(`Unknown redundancy level '${level}'`);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
import { Binary, Elliptic } from 'cafe-utility';
|
|
2
|
-
import { Bytes } from "./bytes.js";
|
|
3
|
-
import { convertCidToReference, convertReferenceToCid } from "./cid.js"; // TODO: add JSdocs for each class
|
|
4
|
-
const ENCODER = new TextEncoder();
|
|
5
|
-
export class PrivateKey extends Bytes {
|
|
6
|
-
constructor(bytes) {
|
|
7
|
-
super(bytes, 32);
|
|
8
|
-
}
|
|
9
|
-
publicKey() {
|
|
10
|
-
const [x, y] = Elliptic.privateKeyToPublicKey(Binary.uint256ToNumber(this.bytes, 'BE'));
|
|
11
|
-
return new PublicKey(Binary.concatBytes(Binary.numberToUint256(x, 'BE'), Binary.numberToUint256(y, 'BE')));
|
|
12
|
-
}
|
|
13
|
-
sign(data) {
|
|
14
|
-
const digest = Binary.concatBytes(ENCODER.encode(`\x19Ethereum Signed Message:\n32`), Binary.keccak256(data instanceof Uint8Array ? data : ENCODER.encode(data)));
|
|
15
|
-
const [r, s, v] = Elliptic.signMessage(digest, Binary.uint256ToNumber(this.bytes, 'BE'));
|
|
16
|
-
return new Signature(Binary.concatBytes(Binary.numberToUint256(r, 'BE'), Binary.numberToUint256(s, 'BE'), new Uint8Array([Number(v)])));
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
PrivateKey.LENGTH = 32;
|
|
20
|
-
export class PublicKey extends Bytes {
|
|
21
|
-
constructor(bytes) {
|
|
22
|
-
const b = new Bytes(bytes);
|
|
23
|
-
if (b.length === 33) {
|
|
24
|
-
const [x, y] = Elliptic.publicKeyFromCompressed(b.toUint8Array());
|
|
25
|
-
super(Binary.concatBytes(Binary.numberToUint256(x, 'BE'), Binary.numberToUint256(y, 'BE')), 64);
|
|
26
|
-
} else {
|
|
27
|
-
super(bytes, 64);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
address() {
|
|
31
|
-
const x = Binary.uint256ToNumber(this.bytes.slice(0, 32), 'BE');
|
|
32
|
-
const y = Binary.uint256ToNumber(this.bytes.slice(32, 64), 'BE');
|
|
33
|
-
return new EthAddress(Elliptic.publicKeyToAddress([x, y]));
|
|
34
|
-
}
|
|
35
|
-
toCompressedUint8Array() {
|
|
36
|
-
const x = Binary.uint256ToNumber(this.bytes.slice(0, 32), 'BE');
|
|
37
|
-
const y = Binary.uint256ToNumber(this.bytes.slice(32, 64), 'BE');
|
|
38
|
-
return Elliptic.compressPublicKey([x, y]);
|
|
39
|
-
}
|
|
40
|
-
toCompressedHex() {
|
|
41
|
-
return Binary.uint8ArrayToHex(this.toCompressedUint8Array());
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
PublicKey.LENGTH = 64;
|
|
45
|
-
export class EthAddress extends Bytes {
|
|
46
|
-
constructor(bytes) {
|
|
47
|
-
super(bytes, 20);
|
|
48
|
-
}
|
|
49
|
-
toChecksum() {
|
|
50
|
-
return Elliptic.checksumEncode(this.bytes);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
EthAddress.LENGTH = 20;
|
|
54
|
-
export class Identifier extends Bytes {
|
|
55
|
-
constructor(bytes) {
|
|
56
|
-
super(bytes, 32);
|
|
57
|
-
}
|
|
58
|
-
static fromString(value) {
|
|
59
|
-
return new Identifier(Binary.keccak256(ENCODER.encode(value)));
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
Identifier.LENGTH = 32;
|
|
63
|
-
export class Reference extends Bytes {
|
|
64
|
-
constructor(bytes) {
|
|
65
|
-
if (typeof bytes === 'string' && bytes.startsWith('bah5')) {
|
|
66
|
-
const decoded = convertCidToReference(bytes);
|
|
67
|
-
super(decoded.reference.bytes, 32);
|
|
68
|
-
} else {
|
|
69
|
-
super(bytes, [32, 64]);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
toCid(type) {
|
|
73
|
-
return convertReferenceToCid(this.bytes, type);
|
|
74
|
-
}
|
|
75
|
-
static isValid(value) {
|
|
76
|
-
try {
|
|
77
|
-
new Reference(value);
|
|
78
|
-
return true;
|
|
79
|
-
} catch {
|
|
80
|
-
return false;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
Reference.LENGTH = 32;
|
|
85
|
-
export class TransactionId extends Bytes {
|
|
86
|
-
constructor(bytes) {
|
|
87
|
-
super(bytes, 32);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
TransactionId.LENGTH = 32;
|
|
91
|
-
export class Span extends Bytes {
|
|
92
|
-
constructor(bytes) {
|
|
93
|
-
super(bytes, 8);
|
|
94
|
-
}
|
|
95
|
-
static fromBigInt(number) {
|
|
96
|
-
return new Span(Binary.numberToUint64(number, 'LE'));
|
|
97
|
-
}
|
|
98
|
-
toBigInt() {
|
|
99
|
-
return Binary.uint64ToNumber(this.bytes, 'LE');
|
|
100
|
-
}
|
|
101
|
-
static fromSlice(bytes, start) {
|
|
102
|
-
return new Span(bytes.slice(start, start + Span.LENGTH));
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
Span.LENGTH = 8;
|
|
106
|
-
export class PeerAddress extends Bytes {
|
|
107
|
-
constructor(bytes) {
|
|
108
|
-
super(bytes, 32);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
PeerAddress.LENGTH = 32;
|
|
112
|
-
export class BatchId extends Bytes {
|
|
113
|
-
constructor(bytes) {
|
|
114
|
-
super(bytes, 32);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
BatchId.LENGTH = 32;
|
|
118
|
-
export class Signature extends Bytes {
|
|
119
|
-
constructor(bytes) {
|
|
120
|
-
super(bytes, 65);
|
|
121
|
-
}
|
|
122
|
-
static fromSlice(bytes, start) {
|
|
123
|
-
return new Signature(bytes.slice(start, start + Signature.LENGTH));
|
|
124
|
-
}
|
|
125
|
-
recoverPublicKey(digest) {
|
|
126
|
-
const r = Binary.uint256ToNumber(this.bytes.slice(0, 32), 'BE');
|
|
127
|
-
const s = Binary.uint256ToNumber(this.bytes.slice(32, 64), 'BE');
|
|
128
|
-
const v = BigInt(this.bytes[64]);
|
|
129
|
-
const [x, y] = Elliptic.recoverPublicKey(Binary.concatBytes(ENCODER.encode(`\x19Ethereum Signed Message:\n32`), Binary.keccak256(digest instanceof Uint8Array ? digest : ENCODER.encode(digest))), r, s, v);
|
|
130
|
-
return new PublicKey(Binary.concatBytes(Binary.numberToUint256(x, 'BE'), Binary.numberToUint256(y, 'BE')));
|
|
131
|
-
}
|
|
132
|
-
isValid(digest, expectedAddress) {
|
|
133
|
-
const publicKey = this.recoverPublicKey(digest);
|
|
134
|
-
const address = publicKey.address();
|
|
135
|
-
return address.equals(expectedAddress);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
Signature.LENGTH = 65;
|
|
139
|
-
export class Topic extends Bytes {
|
|
140
|
-
constructor(bytes) {
|
|
141
|
-
super(bytes, 32);
|
|
142
|
-
}
|
|
143
|
-
static fromString(value) {
|
|
144
|
-
return new Topic(Binary.keccak256(ENCODER.encode(value)));
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
Topic.LENGTH = 32;
|
|
148
|
-
const MAX_UINT64 = new Uint8Array(8).fill(0xff, 0, 8);
|
|
149
|
-
export class FeedIndex extends Bytes {
|
|
150
|
-
constructor(bytes) {
|
|
151
|
-
super(bytes, 8);
|
|
152
|
-
}
|
|
153
|
-
static fromBigInt(number) {
|
|
154
|
-
return new FeedIndex(Binary.numberToUint64(number, 'BE'));
|
|
155
|
-
}
|
|
156
|
-
toBigInt() {
|
|
157
|
-
return Binary.uint64ToNumber(this.bytes, 'BE');
|
|
158
|
-
}
|
|
159
|
-
next() {
|
|
160
|
-
if (Binary.equals(this.bytes, MAX_UINT64)) {
|
|
161
|
-
return FeedIndex.fromBigInt(0n);
|
|
162
|
-
}
|
|
163
|
-
return FeedIndex.fromBigInt(this.toBigInt() + 1n);
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
FeedIndex.LENGTH = 8;
|
|
167
|
-
FeedIndex.MINUS_ONE = new FeedIndex(MAX_UINT64);
|