@trufnetwork/sdk-js 0.0.0-dev-20241126103246
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/LICENSE.md +13 -0
- package/README.md +95 -0
- package/dist/cjs/client/browserClient.cjs +39 -0
- package/dist/cjs/client/browserClient.cjs.map +7 -0
- package/dist/cjs/client/client.cjs +200 -0
- package/dist/cjs/client/client.cjs.map +7 -0
- package/dist/cjs/client/client.test.cjs +32 -0
- package/dist/cjs/client/client.test.cjs.map +7 -0
- package/dist/cjs/client/listAllStreams.cjs +51 -0
- package/dist/cjs/client/listAllStreams.cjs.map +7 -0
- package/dist/cjs/client/nodeClient.cjs +39 -0
- package/dist/cjs/client/nodeClient.cjs.map +7 -0
- package/dist/cjs/contracts/composed_stream_template.json +1638 -0
- package/dist/cjs/contracts/contractsContent.cjs +53 -0
- package/dist/cjs/contracts/contractsContent.cjs.map +7 -0
- package/dist/cjs/contracts/primitive_stream_template.json +952 -0
- package/dist/cjs/contracts-api/composedStream.cjs +137 -0
- package/dist/cjs/contracts-api/composedStream.cjs.map +7 -0
- package/dist/cjs/contracts-api/contractValues.cjs +66 -0
- package/dist/cjs/contracts-api/contractValues.cjs.map +7 -0
- package/dist/cjs/contracts-api/deployStream.cjs +55 -0
- package/dist/cjs/contracts-api/deployStream.cjs.map +7 -0
- package/dist/cjs/contracts-api/destroyStream.cjs +45 -0
- package/dist/cjs/contracts-api/destroyStream.cjs.map +7 -0
- package/dist/cjs/contracts-api/primitiveStream.cjs +86 -0
- package/dist/cjs/contracts-api/primitiveStream.cjs.map +7 -0
- package/dist/cjs/contracts-api/stream.cjs +379 -0
- package/dist/cjs/contracts-api/stream.cjs.map +7 -0
- package/dist/cjs/index.browser.cjs +29 -0
- package/dist/cjs/index.browser.cjs.map +7 -0
- package/dist/cjs/index.cjs +31 -0
- package/dist/cjs/index.cjs.map +7 -0
- package/dist/cjs/index.common.cjs +39 -0
- package/dist/cjs/index.common.cjs.map +7 -0
- package/dist/cjs/index.node.cjs +29 -0
- package/dist/cjs/index.node.cjs.map +7 -0
- package/dist/cjs/types/other.cjs +19 -0
- package/dist/cjs/types/other.cjs.map +7 -0
- package/dist/cjs/types/stream.cjs +19 -0
- package/dist/cjs/types/stream.cjs.map +7 -0
- package/dist/cjs/util/EthereumAddress.cjs +108 -0
- package/dist/cjs/util/EthereumAddress.cjs.map +7 -0
- package/dist/cjs/util/StreamId.cjs +77 -0
- package/dist/cjs/util/StreamId.cjs.map +7 -0
- package/dist/cjs/util/head.cjs +30 -0
- package/dist/cjs/util/head.cjs.map +7 -0
- package/dist/cjs/util/visibility.cjs +42 -0
- package/dist/cjs/util/visibility.cjs.map +7 -0
- package/dist/esm/client/browserClient.mjs +18 -0
- package/dist/esm/client/browserClient.mjs.map +7 -0
- package/dist/esm/client/client.mjs +183 -0
- package/dist/esm/client/client.mjs.map +7 -0
- package/dist/esm/client/client.test.mjs +30 -0
- package/dist/esm/client/client.test.mjs.map +7 -0
- package/dist/esm/client/listAllStreams.mjs +30 -0
- package/dist/esm/client/listAllStreams.mjs.map +7 -0
- package/dist/esm/client/nodeClient.mjs +18 -0
- package/dist/esm/client/nodeClient.mjs.map +7 -0
- package/dist/esm/contracts/composed_stream_template.json +1638 -0
- package/dist/esm/contracts/contractsContent.mjs +21 -0
- package/dist/esm/contracts/contractsContent.mjs.map +7 -0
- package/dist/esm/contracts/primitive_stream_template.json +952 -0
- package/dist/esm/contracts-api/composedStream.mjs +116 -0
- package/dist/esm/contracts-api/composedStream.mjs.map +7 -0
- package/dist/esm/contracts-api/contractValues.mjs +45 -0
- package/dist/esm/contracts-api/contractValues.mjs.map +7 -0
- package/dist/esm/contracts-api/deployStream.mjs +37 -0
- package/dist/esm/contracts-api/deployStream.mjs.map +7 -0
- package/dist/esm/contracts-api/destroyStream.mjs +24 -0
- package/dist/esm/contracts-api/destroyStream.mjs.map +7 -0
- package/dist/esm/contracts-api/primitiveStream.mjs +65 -0
- package/dist/esm/contracts-api/primitiveStream.mjs.map +7 -0
- package/dist/esm/contracts-api/stream.mjs +367 -0
- package/dist/esm/contracts-api/stream.mjs.map +7 -0
- package/dist/esm/index.browser.mjs +7 -0
- package/dist/esm/index.browser.mjs.map +7 -0
- package/dist/esm/index.common.mjs +18 -0
- package/dist/esm/index.common.mjs.map +7 -0
- package/dist/esm/index.mjs +9 -0
- package/dist/esm/index.mjs.map +7 -0
- package/dist/esm/index.node.mjs +7 -0
- package/dist/esm/index.node.mjs.map +7 -0
- package/dist/esm/types/other.mjs +1 -0
- package/dist/esm/types/other.mjs.map +7 -0
- package/dist/esm/types/stream.mjs +1 -0
- package/dist/esm/types/stream.mjs.map +7 -0
- package/dist/esm/util/EthereumAddress.mjs +90 -0
- package/dist/esm/util/EthereumAddress.mjs.map +7 -0
- package/dist/esm/util/StreamId.mjs +59 -0
- package/dist/esm/util/StreamId.mjs.map +7 -0
- package/dist/esm/util/head.mjs +9 -0
- package/dist/esm/util/head.mjs.map +7 -0
- package/dist/esm/util/visibility.mjs +21 -0
- package/dist/esm/util/visibility.mjs.map +7 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/dist/types/client/browserClient.d.ts +7 -0
- package/dist/types/client/browserClient.d.ts.map +1 -0
- package/dist/types/client/client.d.ts +103 -0
- package/dist/types/client/client.d.ts.map +1 -0
- package/dist/types/client/client.test.d.ts +2 -0
- package/dist/types/client/client.test.d.ts.map +1 -0
- package/dist/types/client/listAllStreams.d.ts +14 -0
- package/dist/types/client/listAllStreams.d.ts.map +1 -0
- package/dist/types/client/nodeClient.d.ts +7 -0
- package/dist/types/client/nodeClient.d.ts.map +1 -0
- package/dist/types/contracts/contractsContent.d.ts +4 -0
- package/dist/types/contracts/contractsContent.d.ts.map +1 -0
- package/dist/types/contracts-api/composedStream.d.ts +57 -0
- package/dist/types/contracts-api/composedStream.d.ts.map +1 -0
- package/dist/types/contracts-api/contractValues.d.ts +49 -0
- package/dist/types/contracts-api/contractValues.d.ts.map +1 -0
- package/dist/types/contracts-api/deployStream.d.ts +23 -0
- package/dist/types/contracts-api/deployStream.d.ts.map +1 -0
- package/dist/types/contracts-api/destroyStream.d.ts +27 -0
- package/dist/types/contracts-api/destroyStream.d.ts.map +1 -0
- package/dist/types/contracts-api/primitiveStream.d.ts +39 -0
- package/dist/types/contracts-api/primitiveStream.d.ts.map +1 -0
- package/dist/types/contracts-api/stream.d.ts +135 -0
- package/dist/types/contracts-api/stream.d.ts.map +1 -0
- package/dist/types/index.browser.d.ts +3 -0
- package/dist/types/index.browser.d.ts.map +1 -0
- package/dist/types/index.common.d.ts +16 -0
- package/dist/types/index.common.d.ts.map +1 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.node.d.ts +3 -0
- package/dist/types/index.node.d.ts.map +1 -0
- package/dist/types/types/other.d.ts +2 -0
- package/dist/types/types/other.d.ts.map +1 -0
- package/dist/types/types/stream.d.ts +13 -0
- package/dist/types/types/stream.d.ts.map +1 -0
- package/dist/types/util/EthereumAddress.d.ts +14 -0
- package/dist/types/util/EthereumAddress.d.ts.map +1 -0
- package/dist/types/util/StreamId.d.ts +13 -0
- package/dist/types/util/StreamId.d.ts.map +1 -0
- package/dist/types/util/head.d.ts +3 -0
- package/dist/types/util/head.d.ts.map +1 -0
- package/dist/types/util/visibility.d.ts +10 -0
- package/dist/types/util/visibility.d.ts.map +1 -0
- package/package.json +82 -0
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
|
+
|
|
5
|
+
// src/contracts-api/stream.ts
|
|
6
|
+
import { ActionInput } from "@kwilteam/kwil-js/dist/core/action.js";
|
|
7
|
+
import { generateDBID } from "@kwilteam/kwil-js/dist/utils/dbid.js";
|
|
8
|
+
import { Either } from "monads-io";
|
|
9
|
+
import { EthereumAddress } from "../util/EthereumAddress.mjs";
|
|
10
|
+
import { head } from "../util/head.mjs";
|
|
11
|
+
import { StreamId } from "../util/StreamId.mjs";
|
|
12
|
+
import { toVisibilityEnum } from "../util/visibility.mjs";
|
|
13
|
+
import {
|
|
14
|
+
MetadataKey,
|
|
15
|
+
MetadataKeyValueMap,
|
|
16
|
+
MetadataTableKey,
|
|
17
|
+
StreamType
|
|
18
|
+
} from "./contractValues.mjs";
|
|
19
|
+
var Stream = class {
|
|
20
|
+
constructor(kwilClient, kwilSigner, locator) {
|
|
21
|
+
__publicField(this, "kwilClient");
|
|
22
|
+
__publicField(this, "kwilSigner");
|
|
23
|
+
__publicField(this, "locator");
|
|
24
|
+
__publicField(this, "dbid");
|
|
25
|
+
__publicField(this, "schema");
|
|
26
|
+
__publicField(this, "deployed", false);
|
|
27
|
+
__publicField(this, "initialized", false);
|
|
28
|
+
this.kwilClient = kwilClient;
|
|
29
|
+
this.kwilSigner = kwilSigner;
|
|
30
|
+
this.locator = locator;
|
|
31
|
+
this.dbid = generateDBID(
|
|
32
|
+
locator.dataProvider.getAddress(),
|
|
33
|
+
locator.streamId.getId()
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Loads the schema for this stream from the network.
|
|
38
|
+
* Throws if the stream is not deployed.
|
|
39
|
+
*/
|
|
40
|
+
async loadSchema() {
|
|
41
|
+
const response = await this.kwilClient.getSchema(this.dbid);
|
|
42
|
+
if (response.status !== 200 || !response.data) {
|
|
43
|
+
throw new Error(
|
|
44
|
+
`Failed to load schema for stream ${this.locator.streamId.getId()}`
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
this.schema = response.data;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Executes a method on the stream
|
|
51
|
+
*/
|
|
52
|
+
async execute(method, inputs) {
|
|
53
|
+
return this.kwilClient.execute(
|
|
54
|
+
{
|
|
55
|
+
dbid: this.dbid,
|
|
56
|
+
name: method,
|
|
57
|
+
inputs,
|
|
58
|
+
description: `TN SDK - Executing method on stream: ${method}`
|
|
59
|
+
},
|
|
60
|
+
this.kwilSigner
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Executes a method on the stream after checking if it's initialized
|
|
65
|
+
*/
|
|
66
|
+
async checkedExecute(method, inputs) {
|
|
67
|
+
await this.checkInitialized();
|
|
68
|
+
return this.execute(method, inputs);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Calls a method on the stream
|
|
72
|
+
*/
|
|
73
|
+
async call(method, inputs) {
|
|
74
|
+
const result = await this.kwilClient.call(
|
|
75
|
+
{
|
|
76
|
+
dbid: this.dbid,
|
|
77
|
+
name: method,
|
|
78
|
+
inputs
|
|
79
|
+
},
|
|
80
|
+
this.kwilSigner
|
|
81
|
+
);
|
|
82
|
+
if (result.status !== 200) {
|
|
83
|
+
return Either.left(result.status);
|
|
84
|
+
}
|
|
85
|
+
return Either.right(result.data?.result);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Checks if the stream is initialized
|
|
89
|
+
*/
|
|
90
|
+
async checkInitialized(expectedType) {
|
|
91
|
+
if (this.initialized) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
this.checkDeployed();
|
|
95
|
+
const type = await this.getType();
|
|
96
|
+
const expectedTypes = expectedType ? [expectedType] : [StreamType.Primitive, StreamType.Composed];
|
|
97
|
+
if (!expectedTypes.includes(type)) {
|
|
98
|
+
throw new Error(`Invalid stream type: ${type}`);
|
|
99
|
+
}
|
|
100
|
+
this.initialized = true;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Checks if the stream is deployed
|
|
104
|
+
*/
|
|
105
|
+
async checkDeployed() {
|
|
106
|
+
if (this.deployed) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
await this.loadSchema();
|
|
110
|
+
this.deployed = true;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Initializes the stream
|
|
114
|
+
*/
|
|
115
|
+
async initializeStream() {
|
|
116
|
+
return this.execute("init", []);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Returns the records of the stream within the given date range
|
|
120
|
+
*/
|
|
121
|
+
async getRecord(input) {
|
|
122
|
+
const result = await this.call(
|
|
123
|
+
"get_record",
|
|
124
|
+
[
|
|
125
|
+
ActionInput.fromObject({
|
|
126
|
+
$date_from: input.dateFrom,
|
|
127
|
+
$date_to: input.dateTo,
|
|
128
|
+
$frozen_at: input.frozenAt,
|
|
129
|
+
$base_date: input.baseDate
|
|
130
|
+
})
|
|
131
|
+
]
|
|
132
|
+
);
|
|
133
|
+
return result.mapRight(
|
|
134
|
+
(result2) => result2.map((row) => ({
|
|
135
|
+
dateValue: row.date_value,
|
|
136
|
+
value: row.value
|
|
137
|
+
}))
|
|
138
|
+
).throw();
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Returns the index of the stream within the given date range
|
|
142
|
+
*/
|
|
143
|
+
async getIndex(input) {
|
|
144
|
+
const result = await this.call(
|
|
145
|
+
"get_index",
|
|
146
|
+
[
|
|
147
|
+
ActionInput.fromObject({
|
|
148
|
+
$date_from: input.dateFrom,
|
|
149
|
+
$date_to: input.dateTo,
|
|
150
|
+
$frozen_at: input.frozenAt,
|
|
151
|
+
$base_date: input.baseDate
|
|
152
|
+
})
|
|
153
|
+
]
|
|
154
|
+
);
|
|
155
|
+
return result.mapRight(
|
|
156
|
+
(result2) => result2.map((row) => ({
|
|
157
|
+
dateValue: row.date_value,
|
|
158
|
+
value: row.value
|
|
159
|
+
}))
|
|
160
|
+
).throw();
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Returns the type of the stream
|
|
164
|
+
*/
|
|
165
|
+
async getType() {
|
|
166
|
+
const result = await this.getMetadata(MetadataKey.TypeKey, true);
|
|
167
|
+
if (!result) {
|
|
168
|
+
throw new Error("Failed to get stream type");
|
|
169
|
+
}
|
|
170
|
+
const type = head(result).unwrapOrElse(() => {
|
|
171
|
+
throw new Error(
|
|
172
|
+
"Failed to get stream type. Check if the stream is initialized."
|
|
173
|
+
);
|
|
174
|
+
});
|
|
175
|
+
const validTypes = [StreamType.Primitive, StreamType.Composed];
|
|
176
|
+
if (!validTypes.includes(type.value)) {
|
|
177
|
+
throw new Error(`Invalid stream type: ${type.value}`);
|
|
178
|
+
}
|
|
179
|
+
return type.value;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Returns the first record of the stream
|
|
183
|
+
*/
|
|
184
|
+
async getFirstRecord(input) {
|
|
185
|
+
const result = await this.call(
|
|
186
|
+
"get_first_record",
|
|
187
|
+
[
|
|
188
|
+
ActionInput.fromObject({
|
|
189
|
+
$after_date: input.afterDate,
|
|
190
|
+
$frozen_at: input.frozenAt
|
|
191
|
+
})
|
|
192
|
+
]
|
|
193
|
+
);
|
|
194
|
+
return result.mapRight(head).mapRight(
|
|
195
|
+
(result2) => result2.map((result3) => ({
|
|
196
|
+
dateValue: result3.date_value,
|
|
197
|
+
value: result3.value
|
|
198
|
+
})).unwrapOr(null)
|
|
199
|
+
).throw();
|
|
200
|
+
}
|
|
201
|
+
async setMetadata(key, value) {
|
|
202
|
+
return await this.execute("insert_metadata", [
|
|
203
|
+
ActionInput.fromObject({
|
|
204
|
+
$key: key,
|
|
205
|
+
$value: value,
|
|
206
|
+
$val_type: MetadataKeyValueMap[key]
|
|
207
|
+
})
|
|
208
|
+
]);
|
|
209
|
+
}
|
|
210
|
+
async getMetadata(key, onlyLatest = true, filteredRef) {
|
|
211
|
+
const result = await this.call("get_metadata", [
|
|
212
|
+
ActionInput.fromObject({
|
|
213
|
+
$key: key,
|
|
214
|
+
$only_latest: onlyLatest,
|
|
215
|
+
$ref: filteredRef
|
|
216
|
+
})
|
|
217
|
+
]);
|
|
218
|
+
return result.mapRight(
|
|
219
|
+
(result2) => result2.map((row) => ({
|
|
220
|
+
rowId: row.row_id,
|
|
221
|
+
value: row[MetadataTableKey[MetadataKeyValueMap[key]]],
|
|
222
|
+
createdAt: row.created_at
|
|
223
|
+
}))
|
|
224
|
+
).throw();
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Sets the read visibility of the stream
|
|
228
|
+
*/
|
|
229
|
+
async setReadVisibility(visibility) {
|
|
230
|
+
return await this.setMetadata(
|
|
231
|
+
MetadataKey.ReadVisibilityKey,
|
|
232
|
+
visibility.toString()
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Returns the read visibility of the stream
|
|
237
|
+
*/
|
|
238
|
+
async getReadVisibility() {
|
|
239
|
+
const result = await this.getMetadata(MetadataKey.ReadVisibilityKey, true);
|
|
240
|
+
return head(result).map((row) => toVisibilityEnum(row.value)).unwrapOr(null);
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Sets the compose visibility of the stream
|
|
244
|
+
*/
|
|
245
|
+
async setComposeVisibility(visibility) {
|
|
246
|
+
return await this.setMetadata(
|
|
247
|
+
MetadataKey.ComposeVisibilityKey,
|
|
248
|
+
visibility.toString()
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Returns the compose visibility of the stream
|
|
253
|
+
*/
|
|
254
|
+
async getComposeVisibility() {
|
|
255
|
+
const result = await this.getMetadata(
|
|
256
|
+
MetadataKey.ComposeVisibilityKey,
|
|
257
|
+
true
|
|
258
|
+
);
|
|
259
|
+
return head(result).map((row) => toVisibilityEnum(row.value)).unwrapOr(null);
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Allows a wallet to read the stream
|
|
263
|
+
*/
|
|
264
|
+
async allowReadWallet(wallet) {
|
|
265
|
+
return await this.setMetadata(
|
|
266
|
+
MetadataKey.AllowReadWalletKey,
|
|
267
|
+
wallet.getAddress()
|
|
268
|
+
);
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Disables a wallet from reading the stream
|
|
272
|
+
*/
|
|
273
|
+
async disableReadWallet(wallet) {
|
|
274
|
+
const result = await this.getMetadata(
|
|
275
|
+
MetadataKey.AllowReadWalletKey,
|
|
276
|
+
true,
|
|
277
|
+
wallet.getAddress()
|
|
278
|
+
);
|
|
279
|
+
const row_id = head(result).map((row) => row.rowId).unwrapOr(null);
|
|
280
|
+
if (!row_id) {
|
|
281
|
+
throw new Error("Wallet not found in allowed list");
|
|
282
|
+
}
|
|
283
|
+
return await this.disableMetadata(row_id);
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Allows a stream to use this stream as child
|
|
287
|
+
*/
|
|
288
|
+
async allowComposeStream(locator) {
|
|
289
|
+
const streamDbId = generateDBID(
|
|
290
|
+
locator.dataProvider.getAddress(),
|
|
291
|
+
locator.streamId.getId()
|
|
292
|
+
);
|
|
293
|
+
return await this.setMetadata(
|
|
294
|
+
MetadataKey.AllowComposeStreamKey,
|
|
295
|
+
streamDbId
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Disables a stream from using this stream as child
|
|
300
|
+
*/
|
|
301
|
+
async disableComposeStream(locator) {
|
|
302
|
+
const result = await this.getMetadata(
|
|
303
|
+
MetadataKey.AllowComposeStreamKey,
|
|
304
|
+
true,
|
|
305
|
+
locator.toString()
|
|
306
|
+
);
|
|
307
|
+
const row_id = head(result).map((row) => row.rowId).unwrapOr(null);
|
|
308
|
+
if (!row_id) {
|
|
309
|
+
throw new Error("Stream not found in allowed list");
|
|
310
|
+
}
|
|
311
|
+
return await this.disableMetadata(row_id);
|
|
312
|
+
}
|
|
313
|
+
async disableMetadata(rowId) {
|
|
314
|
+
return await this.execute("disable_metadata", [
|
|
315
|
+
ActionInput.fromObject({
|
|
316
|
+
$row_id: rowId
|
|
317
|
+
})
|
|
318
|
+
]);
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Returns the wallets allowed to read the stream
|
|
322
|
+
*/
|
|
323
|
+
async getAllowedReadWallets() {
|
|
324
|
+
const result = await this.getMetadata(MetadataKey.AllowReadWalletKey);
|
|
325
|
+
return result.filter((row) => row.value).map((row) => new EthereumAddress(row.value));
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Returns the streams allowed to compose the stream
|
|
329
|
+
*/
|
|
330
|
+
async getAllowedComposeStreams() {
|
|
331
|
+
const result = await this.getMetadata(MetadataKey.AllowComposeStreamKey);
|
|
332
|
+
return result.filter((row) => row.value).map((row) => {
|
|
333
|
+
const [streamId, dataProvider] = row.value.split(":");
|
|
334
|
+
return {
|
|
335
|
+
streamId: StreamId.fromString(streamId).throw(),
|
|
336
|
+
dataProvider: new EthereumAddress(dataProvider)
|
|
337
|
+
};
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Returns the index change of the stream within the given date range
|
|
342
|
+
*/
|
|
343
|
+
async getIndexChange(input) {
|
|
344
|
+
const result = await this.call(
|
|
345
|
+
"get_index_change",
|
|
346
|
+
[
|
|
347
|
+
ActionInput.fromObject({
|
|
348
|
+
$date_from: input.dateFrom,
|
|
349
|
+
$date_to: input.dateTo,
|
|
350
|
+
$frozen_at: input.frozenAt,
|
|
351
|
+
$base_date: input.baseDate,
|
|
352
|
+
$days_interval: input.daysInterval
|
|
353
|
+
})
|
|
354
|
+
]
|
|
355
|
+
);
|
|
356
|
+
return result.mapRight(
|
|
357
|
+
(result2) => result2.map((row) => ({
|
|
358
|
+
dateValue: row.date_value,
|
|
359
|
+
value: row.value
|
|
360
|
+
}))
|
|
361
|
+
).throw();
|
|
362
|
+
}
|
|
363
|
+
};
|
|
364
|
+
export {
|
|
365
|
+
Stream
|
|
366
|
+
};
|
|
367
|
+
//# sourceMappingURL=stream.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/contracts-api/stream.ts"],
|
|
4
|
+
"sourcesContent": ["import { KwilSigner, NodeKwil, WebKwil } from \"@kwilteam/kwil-js\";\nimport { ActionInput } from \"@kwilteam/kwil-js/dist/core/action\";\nimport { Database } from \"@kwilteam/kwil-js/dist/core/database\";\nimport { GenericResponse } from \"@kwilteam/kwil-js/dist/core/resreq\";\nimport { TxReceipt } from \"@kwilteam/kwil-js/dist/core/tx\";\nimport { generateDBID } from \"@kwilteam/kwil-js/dist/utils/dbid\";\nimport { Either } from \"monads-io\";\nimport { DateString } from \"../types/other\";\nimport { StreamLocator } from \"../types/stream\";\nimport { EthereumAddress } from \"../util/EthereumAddress\";\nimport { head } from \"../util/head\";\nimport { StreamId } from \"../util/StreamId\";\nimport { toVisibilityEnum, VisibilityEnum } from \"../util/visibility\";\nimport {\n MetadataKey,\n MetadataKeyValueMap,\n MetadataTableKey,\n MetadataValueTypeForKey,\n StreamType,\n} from \"./contractValues\";\n\nexport interface GetRecordInput {\n dateFrom?: DateString;\n dateTo?: DateString;\n frozenAt?: number;\n baseDate?: DateString;\n}\n\nexport interface GetFirstRecordInput {\n afterDate?: DateString;\n frozenAt?: DateString;\n}\n\nexport interface StreamRecord {\n dateValue: DateString;\n value: string;\n}\n\nexport interface GetIndexChangeInput extends GetRecordInput {\n daysInterval: number;\n}\n\nexport class Stream {\n protected kwilClient: WebKwil | NodeKwil;\n protected kwilSigner: KwilSigner;\n protected locator: StreamLocator;\n protected dbid: string;\n protected schema?: Database;\n protected deployed: boolean = false;\n protected initialized: boolean = false;\n constructor(\n kwilClient: WebKwil | NodeKwil,\n kwilSigner: KwilSigner,\n locator: StreamLocator,\n ) {\n this.kwilClient = kwilClient;\n this.kwilSigner = kwilSigner;\n this.locator = locator;\n this.dbid = generateDBID(\n locator.dataProvider.getAddress(),\n locator.streamId.getId(),\n );\n }\n\n /**\n * Loads the schema for this stream from the network.\n * Throws if the stream is not deployed.\n */\n public async loadSchema(): Promise<void> {\n const response = await this.kwilClient.getSchema(this.dbid);\n if (response.status !== 200 || !response.data) {\n throw new Error(\n `Failed to load schema for stream ${this.locator.streamId.getId()}`,\n );\n }\n this.schema = response.data;\n }\n\n /**\n * Executes a method on the stream\n */\n protected async execute(\n method: string,\n inputs: ActionInput[],\n ): Promise<GenericResponse<TxReceipt>> {\n return this.kwilClient.execute(\n {\n dbid: this.dbid,\n name: method,\n inputs,\n description: `TN SDK - Executing method on stream: ${method}`,\n },\n this.kwilSigner,\n );\n }\n\n /**\n * Executes a method on the stream after checking if it's initialized\n */\n protected async checkedExecute(\n method: string,\n inputs: ActionInput[],\n ): Promise<GenericResponse<TxReceipt>> {\n await this.checkInitialized();\n return this.execute(method, inputs);\n }\n\n /**\n * Calls a method on the stream\n */\n protected async call<T>(\n method: string,\n inputs: ActionInput[],\n ): Promise<Either<number, T>> {\n const result = await this.kwilClient.call(\n {\n dbid: this.dbid,\n name: method,\n inputs,\n },\n this.kwilSigner,\n );\n\n if (result.status !== 200) {\n return Either.left(result.status);\n }\n\n return Either.right(result.data?.result as T);\n }\n\n /**\n * Checks if the stream is initialized\n */\n protected async checkInitialized(expectedType?: StreamType): Promise<void> {\n if (this.initialized) {\n return;\n }\n\n this.checkDeployed();\n\n // check if is initialized by trying to get its type\n const type = await this.getType();\n\n // check if type is valid\n const expectedTypes = expectedType\n ? [expectedType]\n : [StreamType.Primitive, StreamType.Composed];\n if (!expectedTypes.includes(type)) {\n throw new Error(`Invalid stream type: ${type}`);\n }\n\n this.initialized = true;\n }\n\n /**\n * Checks if the stream is deployed\n */\n protected async checkDeployed(): Promise<void> {\n if (this.deployed) {\n return;\n }\n await this.loadSchema();\n this.deployed = true;\n }\n\n /**\n * Initializes the stream\n */\n public async initializeStream(): Promise<GenericResponse<TxReceipt>> {\n // shouldn't use checkedExecute, because it's not initialized yet\n return this.execute(\"init\", []);\n }\n\n /**\n * Returns the records of the stream within the given date range\n */\n public async getRecord(input: GetRecordInput): Promise<StreamRecord[]> {\n // TODO: change value to string when kwil-js is updated\n const result = await this.call<{ date_value: string; value: string }[]>(\n \"get_record\",\n [\n ActionInput.fromObject({\n $date_from: input.dateFrom,\n $date_to: input.dateTo,\n $frozen_at: input.frozenAt,\n $base_date: input.baseDate,\n }),\n ],\n );\n return result\n .mapRight((result) =>\n result.map((row) => ({\n dateValue: row.date_value,\n value: row.value,\n })),\n )\n .throw();\n }\n\n /**\n * Returns the index of the stream within the given date range\n */\n public async getIndex(input: GetRecordInput): Promise<StreamRecord[]> {\n const result = await this.call<{ date_value: string; value: string }[]>(\n \"get_index\",\n [\n ActionInput.fromObject({\n $date_from: input.dateFrom,\n $date_to: input.dateTo,\n $frozen_at: input.frozenAt,\n $base_date: input.baseDate,\n }),\n ],\n );\n return result\n .mapRight((result) =>\n result.map((row) => ({\n dateValue: row.date_value,\n value: row.value,\n })),\n )\n .throw();\n }\n\n /**\n * Returns the type of the stream\n */\n public async getType(): Promise<StreamType> {\n const result = await this.getMetadata(MetadataKey.TypeKey, true);\n\n if (!result) {\n throw new Error(\"Failed to get stream type\");\n }\n\n const type = head(result).unwrapOrElse(() => {\n throw new Error(\n \"Failed to get stream type. Check if the stream is initialized.\",\n );\n });\n\n const validTypes = [StreamType.Primitive, StreamType.Composed];\n\n if (!validTypes.includes(type.value as StreamType)) {\n throw new Error(`Invalid stream type: ${type.value}`);\n }\n\n return type.value as StreamType;\n }\n\n /**\n * Returns the first record of the stream\n */\n public async getFirstRecord(\n input: GetFirstRecordInput,\n ): Promise<StreamRecord | null> {\n const result = await this.call<{ date_value: string; value: string }[]>(\n \"get_first_record\",\n [\n ActionInput.fromObject({\n $after_date: input.afterDate,\n $frozen_at: input.frozenAt,\n }),\n ],\n );\n\n return result\n .mapRight(head)\n .mapRight((result) =>\n result\n .map((result) => ({\n dateValue: result.date_value,\n value: result.value,\n }))\n .unwrapOr(null),\n )\n .throw();\n }\n\n protected async setMetadata<K extends MetadataKey>(\n key: K,\n value: MetadataValueTypeForKey<K>,\n ): Promise<GenericResponse<TxReceipt>> {\n return await this.execute(\"insert_metadata\", [\n ActionInput.fromObject({\n $key: key,\n $value: value,\n $val_type: MetadataKeyValueMap[key],\n }),\n ]);\n }\n\n protected async getMetadata<K extends MetadataKey>(\n key: K,\n onlyLatest: boolean = true,\n filteredRef?: string,\n ): Promise<\n { rowId: string; value: MetadataValueTypeForKey<K>; createdAt: number }[]\n > {\n const result = await this.call<\n {\n row_id: string;\n value_i: number;\n value_f: string;\n value_b: boolean;\n value_s: string;\n value_ref: string;\n created_at: number;\n }[]\n >(\"get_metadata\", [\n ActionInput.fromObject({\n $key: key,\n $only_latest: onlyLatest,\n $ref: filteredRef,\n }),\n ]);\n return result\n .mapRight((result) =>\n result.map((row) => ({\n rowId: row.row_id,\n value: row[\n MetadataTableKey[MetadataKeyValueMap[key as MetadataKey]]\n ] as MetadataValueTypeForKey<K>,\n createdAt: row.created_at,\n })),\n )\n .throw();\n }\n\n /**\n * Sets the read visibility of the stream\n */\n public async setReadVisibility(\n visibility: VisibilityEnum,\n ): Promise<GenericResponse<TxReceipt>> {\n return await this.setMetadata(\n MetadataKey.ReadVisibilityKey,\n visibility.toString(),\n );\n }\n\n /**\n * Returns the read visibility of the stream\n */\n public async getReadVisibility(): Promise<VisibilityEnum | null> {\n const result = await this.getMetadata(MetadataKey.ReadVisibilityKey, true);\n\n return head(result)\n .map((row) => toVisibilityEnum(row.value))\n .unwrapOr(null);\n }\n\n /**\n * Sets the compose visibility of the stream\n */\n public async setComposeVisibility(\n visibility: VisibilityEnum,\n ): Promise<GenericResponse<TxReceipt>> {\n return await this.setMetadata(\n MetadataKey.ComposeVisibilityKey,\n visibility.toString(),\n );\n }\n\n /**\n * Returns the compose visibility of the stream\n */\n public async getComposeVisibility(): Promise<VisibilityEnum | null> {\n const result = await this.getMetadata(\n MetadataKey.ComposeVisibilityKey,\n true,\n );\n\n return head(result)\n .map((row) => toVisibilityEnum(row.value))\n .unwrapOr(null);\n }\n\n /**\n * Allows a wallet to read the stream\n */\n public async allowReadWallet(\n wallet: EthereumAddress,\n ): Promise<GenericResponse<TxReceipt>> {\n return await this.setMetadata(\n MetadataKey.AllowReadWalletKey,\n wallet.getAddress(),\n );\n }\n\n /**\n * Disables a wallet from reading the stream\n */\n public async disableReadWallet(\n wallet: EthereumAddress,\n ): Promise<GenericResponse<TxReceipt>> {\n const result = await this.getMetadata(\n MetadataKey.AllowReadWalletKey,\n true,\n wallet.getAddress(),\n );\n\n const row_id = head(result)\n .map((row) => row.rowId)\n .unwrapOr(null);\n\n if (!row_id) {\n throw new Error(\"Wallet not found in allowed list\");\n }\n\n return await this.disableMetadata(row_id);\n }\n\n /**\n * Allows a stream to use this stream as child\n */\n public async allowComposeStream(\n locator: StreamLocator,\n ): Promise<GenericResponse<TxReceipt>> {\n const streamDbId = generateDBID(\n locator.dataProvider.getAddress(),\n locator.streamId.getId(),\n );\n return await this.setMetadata(\n MetadataKey.AllowComposeStreamKey,\n streamDbId,\n );\n }\n\n /**\n * Disables a stream from using this stream as child\n */\n public async disableComposeStream(\n locator: StreamLocator,\n ): Promise<GenericResponse<TxReceipt>> {\n const result = await this.getMetadata(\n MetadataKey.AllowComposeStreamKey,\n true,\n locator.toString(),\n );\n\n const row_id = head(result)\n .map((row) => row.rowId)\n .unwrapOr(null);\n\n if (!row_id) {\n throw new Error(\"Stream not found in allowed list\");\n }\n\n return await this.disableMetadata(row_id);\n }\n\n protected async disableMetadata(\n rowId: string,\n ): Promise<GenericResponse<TxReceipt>> {\n return await this.execute(\"disable_metadata\", [\n ActionInput.fromObject({\n $row_id: rowId,\n }),\n ]);\n }\n\n /**\n * Returns the wallets allowed to read the stream\n */\n public async getAllowedReadWallets(): Promise<EthereumAddress[]> {\n const result = await this.getMetadata(MetadataKey.AllowReadWalletKey);\n\n return result\n .filter((row) => row.value)\n .map((row) => new EthereumAddress(row.value));\n }\n\n /**\n * Returns the streams allowed to compose the stream\n */\n public async getAllowedComposeStreams(): Promise<StreamLocator[]> {\n const result = await this.getMetadata(MetadataKey.AllowComposeStreamKey);\n\n return result\n .filter((row) => row.value)\n .map((row) => {\n const [streamId, dataProvider] = row.value.split(\":\");\n return {\n streamId: StreamId.fromString(streamId).throw(),\n dataProvider: new EthereumAddress(dataProvider),\n };\n });\n }\n\n /**\n * Returns the index change of the stream within the given date range\n */\n public async getIndexChange(\n input: GetIndexChangeInput,\n ): Promise<StreamRecord[]> {\n const result = await this.call<{ date_value: string; value: string }[]>(\n \"get_index_change\",\n [\n ActionInput.fromObject({\n $date_from: input.dateFrom,\n $date_to: input.dateTo,\n $frozen_at: input.frozenAt,\n $base_date: input.baseDate,\n $days_interval: input.daysInterval,\n }),\n ],\n );\n\n return result\n .mapRight((result) =>\n result.map((row) => ({\n dateValue: row.date_value,\n value: row.value,\n })),\n )\n .throw();\n }\n}\n"],
|
|
5
|
+
"mappings": ";;;;;AACA,SAAS,mBAAmB;AAI5B,SAAS,oBAAoB;AAC7B,SAAS,cAAc;AAGvB,SAAS,uBAAuB;AAChC,SAAS,YAAY;AACrB,SAAS,gBAAgB;AACzB,SAAS,wBAAwC;AACjD;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EAEA;AAAA,OACK;AAuBA,IAAM,SAAN,MAAa;AAAA,EAQlB,YACE,YACA,YACA,SACA;AAXF,wBAAU;AACV,wBAAU;AACV,wBAAU;AACV,wBAAU;AACV,wBAAU;AACV,wBAAU,YAAoB;AAC9B,wBAAU,eAAuB;AAM/B,SAAK,aAAa;AAClB,SAAK,aAAa;AAClB,SAAK,UAAU;AACf,SAAK,OAAO;AAAA,MACV,QAAQ,aAAa,WAAW;AAAA,MAChC,QAAQ,SAAS,MAAM;AAAA,IACzB;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAa,aAA4B;AACvC,UAAM,WAAW,MAAM,KAAK,WAAW,UAAU,KAAK,IAAI;AAC1D,QAAI,SAAS,WAAW,OAAO,CAAC,SAAS,MAAM;AAC7C,YAAM,IAAI;AAAA,QACR,oCAAoC,KAAK,QAAQ,SAAS,M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gBAAgB,YAAY;AAAA,MAChD;AAAA,IACF,CAAC;AAAA,EACL;AAAA;AAAA;AAAA;AAAA,EAKA,MAAa,eACX,OACyB;AACzB,UAAM,SAAS,MAAM,KAAK;AAAA,MACxB;AAAA,MACA;AAAA,QACE,YAAY,WAAW;AAAA,UACrB,YAAY,MAAM;AAAA,UAClB,UAAU,MAAM;AAAA,UAChB,YAAY,MAAM;AAAA,UAClB,YAAY,MAAM;AAAA,UAClB,gBAAgB,MAAM;AAAA,QACxB,CAAC;AAAA,MACH;AAAA,IACF;AAEA,WAAO,OACJ;AAAA,MAAS,CAACA,YACTA,QAAO,IAAI,CAAC,SAAS;AAAA,QACnB,WAAW,IAAI;AAAA,QACf,OAAO,IAAI;AAAA,MACb,EAAE;AAAA,IACJ,EACC,MAAM;AAAA,EACX;AACF;",
|
|
6
|
+
"names": ["result"]
|
|
7
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/index.browser.ts"],
|
|
4
|
+
"sourcesContent": ["// Browser/Web exports\nexport { BrowserTNClient } from \"./client/browserClient\";\nexport * from \"./index.common\";\n"],
|
|
5
|
+
"mappings": ";AACA,SAAS,uBAAuB;AAChC,cAAc;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// src/index.common.ts
|
|
2
|
+
import { StreamId } from "./util/StreamId.mjs";
|
|
3
|
+
import { EthereumAddress } from "./util/EthereumAddress.mjs";
|
|
4
|
+
import { visibility } from "./util/visibility.mjs";
|
|
5
|
+
import { StreamType } from "./contracts-api/contractValues.mjs";
|
|
6
|
+
import { Stream } from "./contracts-api/stream.mjs";
|
|
7
|
+
import { PrimitiveStream } from "./contracts-api/primitiveStream.mjs";
|
|
8
|
+
import { ComposedStream } from "./contracts-api/composedStream.mjs";
|
|
9
|
+
export {
|
|
10
|
+
ComposedStream,
|
|
11
|
+
EthereumAddress,
|
|
12
|
+
PrimitiveStream,
|
|
13
|
+
Stream,
|
|
14
|
+
StreamId,
|
|
15
|
+
StreamType,
|
|
16
|
+
visibility
|
|
17
|
+
};
|
|
18
|
+
//# sourceMappingURL=index.common.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/index.common.ts"],
|
|
4
|
+
"sourcesContent": ["// Core client types\nexport type { TNClientOptions } from \"./client/client\";\nexport type { SignerInfo as EthProvider } from \"./client/client\";\n\n// Stream types and interfaces\nexport type { StreamLocator } from \"./types/stream\";\nexport type { StreamRecord } from \"./contracts-api/stream\";\nexport type { GetRecordInput, GetFirstRecordInput } from \"./contracts-api/stream\";\nexport type { InsertRecordInput } from \"./contracts-api/primitiveStream\";\nexport type { TaxonomySet, TaxonomyItem } from \"./contracts-api/composedStream\";\n\n// Utility types and classes\nexport { StreamId } from \"./util/StreamId\";\nexport { EthereumAddress } from \"./util/EthereumAddress\";\nexport { visibility } from \"./util/visibility\";\nexport type { VisibilityEnum } from \"./util/visibility\";\n\n// Stream type constants\nexport { StreamType } from \"./contracts-api/contractValues\";\n\n// Base classes\nexport { Stream } from \"./contracts-api/stream\";\nexport { PrimitiveStream } from \"./contracts-api/primitiveStream\";\nexport { ComposedStream } from \"./contracts-api/composedStream\";\n\n"],
|
|
5
|
+
"mappings": ";AAYA,SAAS,gBAAgB;AACzB,SAAS,uBAAuB;AAChC,SAAS,kBAAkB;AAI3B,SAAS,kBAAkB;AAG3B,SAAS,cAAc;AACvB,SAAS,uBAAuB;AAChC,SAAS,sBAAsB;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/index.ts"],
|
|
4
|
+
"sourcesContent": ["// All exports, to provide types for both browser and node\nexport { BrowserTNClient } from \"./client/browserClient\";\nexport { NodeTNClient } from \"./client/nodeClient\";\nexport * from \"./index.common\";\n"],
|
|
5
|
+
"mappings": ";AACA,SAAS,uBAAuB;AAChC,SAAS,oBAAoB;AAC7B,cAAc;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=other.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=stream.mjs.map
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
|
+
|
|
5
|
+
// src/util/EthereumAddress.ts
|
|
6
|
+
import { ethers } from "ethers";
|
|
7
|
+
import { left, right } from "monads-io/either";
|
|
8
|
+
var EthereumAddress = class _EthereumAddress {
|
|
9
|
+
constructor(address) {
|
|
10
|
+
__publicField(this, "address");
|
|
11
|
+
__publicField(this, "correctlyCreated", false);
|
|
12
|
+
address = address.toLowerCase();
|
|
13
|
+
if (!address.startsWith("0x")) {
|
|
14
|
+
address = "0x" + address;
|
|
15
|
+
}
|
|
16
|
+
if (!this.validateEthereumAddress(address)) {
|
|
17
|
+
throw new Error("Invalid Ethereum address");
|
|
18
|
+
}
|
|
19
|
+
this.address = address;
|
|
20
|
+
this.correctlyCreated = true;
|
|
21
|
+
}
|
|
22
|
+
validateEthereumAddress(address) {
|
|
23
|
+
return /^(0x)?[0-9a-f]{40}$/i.test(address);
|
|
24
|
+
}
|
|
25
|
+
getAddress() {
|
|
26
|
+
if (!this.correctlyCreated) {
|
|
27
|
+
throw new Error("EthereumAddress not correctly created");
|
|
28
|
+
}
|
|
29
|
+
return this.address;
|
|
30
|
+
}
|
|
31
|
+
getBytes() {
|
|
32
|
+
return new TextEncoder().encode(this.getAddress());
|
|
33
|
+
}
|
|
34
|
+
toJSON() {
|
|
35
|
+
return this.getAddress();
|
|
36
|
+
}
|
|
37
|
+
static fromJSON(json) {
|
|
38
|
+
return new _EthereumAddress(json);
|
|
39
|
+
}
|
|
40
|
+
static fromBytes(bytes) {
|
|
41
|
+
try {
|
|
42
|
+
return right(new _EthereumAddress(ethers.hexlify(bytes)));
|
|
43
|
+
} catch (e) {
|
|
44
|
+
return left(e);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
static fromString(str) {
|
|
48
|
+
try {
|
|
49
|
+
return right(new _EthereumAddress(str));
|
|
50
|
+
} catch (e) {
|
|
51
|
+
return left(e);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
if (import.meta.vitest) {
|
|
56
|
+
const { describe, it, expect } = import.meta.vitest;
|
|
57
|
+
describe("EthereumAddress", () => {
|
|
58
|
+
it("should create a valid EthereumAddress with correct format", () => {
|
|
59
|
+
const address = new EthereumAddress(
|
|
60
|
+
"0x1234567890123456789012345678901234567890"
|
|
61
|
+
);
|
|
62
|
+
expect(address.getAddress()).toBe(
|
|
63
|
+
"0x1234567890123456789012345678901234567890"
|
|
64
|
+
);
|
|
65
|
+
});
|
|
66
|
+
it("should throw an error for an invalid Ethereum address", () => {
|
|
67
|
+
expect(() => new EthereumAddress("invalid_address")).toThrow(
|
|
68
|
+
"Invalid Ethereum address"
|
|
69
|
+
);
|
|
70
|
+
});
|
|
71
|
+
it("should enforce lowercase addresses", () => {
|
|
72
|
+
const mixedCaseAddress = "0xaaBbccDdEeff1234567890123456789012345678".toLowerCase();
|
|
73
|
+
const address = new EthereumAddress(mixedCaseAddress);
|
|
74
|
+
expect(address.getAddress()).toBe(
|
|
75
|
+
"0xaabbccddeeff1234567890123456789012345678"
|
|
76
|
+
);
|
|
77
|
+
});
|
|
78
|
+
it("should correctly serialize and deserialize to/from JSON", () => {
|
|
79
|
+
const originalAddress = "0x1234567890123456789012345678901234567890";
|
|
80
|
+
const address = new EthereumAddress(originalAddress);
|
|
81
|
+
const json = address.toJSON();
|
|
82
|
+
const deserializedAddress = EthereumAddress.fromJSON(json);
|
|
83
|
+
expect(deserializedAddress.getAddress()).toBe(originalAddress);
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
export {
|
|
88
|
+
EthereumAddress
|
|
89
|
+
};
|
|
90
|
+
//# sourceMappingURL=EthereumAddress.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/util/EthereumAddress.ts"],
|
|
4
|
+
"sourcesContent": ["import { ethers } from \"ethers\";\nimport { Either, left, right } from \"monads-io/either\";\n\nexport class EthereumAddress {\n private readonly address: string;\n private readonly correctlyCreated: boolean = false;\n\n constructor(address: string) {\n address = address.toLowerCase();\n if (!address.startsWith(\"0x\")) {\n address = \"0x\" + address;\n }\n\n if (!this.validateEthereumAddress(address)) {\n throw new Error(\"Invalid Ethereum address\");\n }\n\n this.address = address;\n this.correctlyCreated = true;\n }\n\n private validateEthereumAddress(address: string): boolean {\n return /^(0x)?[0-9a-f]{40}$/i.test(address);\n }\n\n public getAddress(): string {\n if (!this.correctlyCreated) {\n throw new Error(\"EthereumAddress not correctly created\");\n }\n\n return this.address;\n }\n\n public getBytes(): Uint8Array {\n return new TextEncoder().encode(this.getAddress());\n }\n\n public toJSON(): string {\n return this.getAddress();\n }\n\n public static fromJSON(json: string): EthereumAddress {\n return new EthereumAddress(json);\n }\n\n public static fromBytes(bytes: Uint8Array): Either<Error, EthereumAddress> {\n try {\n return right(new EthereumAddress(ethers.hexlify(bytes)));\n } catch (e) {\n return left(e as Error);\n }\n }\n\n public static fromString(str: string): Either<Error, EthereumAddress> {\n try {\n return right(new EthereumAddress(str));\n } catch (e) {\n return left(e as Error);\n }\n }\n}\n\nif (import.meta.vitest) {\n const { describe, it, expect } = import.meta.vitest;\n describe(\"EthereumAddress\", () => {\n it(\"should create a valid EthereumAddress with correct format\", () => {\n const address = new EthereumAddress(\n \"0x1234567890123456789012345678901234567890\",\n );\n expect(address.getAddress()).toBe(\n \"0x1234567890123456789012345678901234567890\",\n );\n });\n\n it(\"should throw an error for an invalid Ethereum address\", () => {\n expect(() => new EthereumAddress(\"invalid_address\")).toThrow(\n \"Invalid Ethereum address\",\n );\n });\n\n it(\"should enforce lowercase addresses\", () => {\n const mixedCaseAddress =\n \"0xaaBbccDdEeff1234567890123456789012345678\".toLowerCase();\n const address = new EthereumAddress(mixedCaseAddress);\n expect(address.getAddress()).toBe(\n \"0xaabbccddeeff1234567890123456789012345678\",\n );\n });\n\n it(\"should correctly serialize and deserialize to/from JSON\", () => {\n const originalAddress = \"0x1234567890123456789012345678901234567890\";\n const address = new EthereumAddress(originalAddress);\n const json = address.toJSON();\n const deserializedAddress = EthereumAddress.fromJSON(json);\n expect(deserializedAddress.getAddress()).toBe(originalAddress);\n });\n });\n}\n"],
|
|
5
|
+
"mappings": ";;;;;AAAA,SAAS,cAAc;AACvB,SAAiB,MAAM,aAAa;AAE7B,IAAM,kBAAN,MAAM,iBAAgB;AAAA,EAI3B,YAAY,SAAiB;AAH7B,wBAAiB;AACjB,wBAAiB,oBAA4B;AAG3C,cAAU,QAAQ,YAAY;AAC9B,QAAI,CAAC,QAAQ,WAAW,IAAI,GAAG;AAC7B,gBAAU,OAAO;AAAA,IACnB;AAEA,QAAI,CAAC,KAAK,wBAAwB,OAAO,GAAG;AAC1C,YAAM,IAAI,MAAM,0BAA0B;AAAA,IAC5C;AAEA,SAAK,UAAU;AACf,SAAK,mBAAmB;AAAA,EAC1B;AAAA,EAEQ,wBAAwB,SAA0B;AACxD,WAAO,uBAAuB,KAAK,OAAO;AAAA,EAC5C;AAAA,EAEO,aAAqB;AAC1B,QAAI,CAAC,KAAK,kBAAkB;AAC1B,YAAM,IAAI,MAAM,uCAAuC;AAAA,IACzD;AAEA,WAAO,KAAK;AAAA,EACd;AAAA,EAEO,WAAuB;AAC5B,WAAO,IA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