@sovereign-sdk/web3 0.0.11 → 0.0.13
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 +119 -0
- package/dist/index.d.mts +222 -1
- package/dist/index.d.ts +222 -1
- package/dist/index.js +54 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +53 -0
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
# @sovereign-sdk/web3
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@sovereign-sdk/web3)
|
|
4
|
+
[](https://github.com/Sovereign-Labs/sovereign-sdk-web3-js/actions/workflows/ci.yaml)
|
|
5
|
+
|
|
6
|
+
A TypeScript client library for interacting with Sovereign SDK rollups.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @sovereign-sdk/web3
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Features
|
|
15
|
+
|
|
16
|
+
- 🔄 Type-safe transaction submission and signing
|
|
17
|
+
- 🔍 Runtime call simulation for gas estimation
|
|
18
|
+
- 📦 Borsh serialization with schema validation
|
|
19
|
+
- 🎯 Strongly typed rollup interactions with customizable type specifications
|
|
20
|
+
- 🌐 Full access to ledger, sequencer, and rollup APIs
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
### Basic Setup
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { StandardRollup } from '@sovereign-sdk/web3';
|
|
28
|
+
import { newMetaMaskSnapSigner } from '@sovereign-sdk/signers';
|
|
29
|
+
|
|
30
|
+
// Initialize the rollup client
|
|
31
|
+
const rollup = new StandardRollup({
|
|
32
|
+
url: 'https://your-rollup-node.com',
|
|
33
|
+
schema: yourSchema,
|
|
34
|
+
defaultTxDetails: {
|
|
35
|
+
max_priority_fee_bips: 1000,
|
|
36
|
+
max_fee: 1000000,
|
|
37
|
+
chain_id: 1,
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// Initialize a signer
|
|
42
|
+
const signer = newMetaMaskSnapSigner({
|
|
43
|
+
curve: 'ed25519',
|
|
44
|
+
schema: yourSchema,
|
|
45
|
+
snapId: 'npm:@sovereign-sdk/metamask-snap',
|
|
46
|
+
});
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Submitting Transactions
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
// Submit a runtime call
|
|
53
|
+
const result = await rollup.call(
|
|
54
|
+
{
|
|
55
|
+
value_setter: {
|
|
56
|
+
set_value: 100,
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
{ signer }
|
|
60
|
+
);
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Type Safety
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
type MyRollupTypes = RollupTypeSpec<{
|
|
67
|
+
RuntimeCall: {
|
|
68
|
+
value_setter: {
|
|
69
|
+
set_value: number;
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
// Add other type specifications as needed
|
|
73
|
+
}>;
|
|
74
|
+
|
|
75
|
+
const typedRollup = new StandardRollup<MyRollupTypes>({
|
|
76
|
+
// ... config
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
// Now your calls will be type-checked!
|
|
80
|
+
const result = await typedRollup.call(
|
|
81
|
+
{
|
|
82
|
+
value_setter: {
|
|
83
|
+
set_value: 100, // Type checked and automatically serialized to Borsh bytes
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
{ signer }
|
|
87
|
+
);
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Simulation
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
// Simulate a call to estimate gas
|
|
94
|
+
const simulation = await rollup.simulate(
|
|
95
|
+
{
|
|
96
|
+
value_setter: {
|
|
97
|
+
set_value: 100,
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
signer,
|
|
102
|
+
txDetails: {
|
|
103
|
+
max_priority_fee_bips: 1000,
|
|
104
|
+
max_fee: 1000000,
|
|
105
|
+
chain_id: 1,
|
|
106
|
+
},
|
|
107
|
+
}
|
|
108
|
+
);
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## API Reference
|
|
112
|
+
|
|
113
|
+
The package exports the following main components:
|
|
114
|
+
|
|
115
|
+
- `StandardRollup`: The main client class for interacting with rollups
|
|
116
|
+
- `RollupTypeSpec`: Type utility for creating type-safe rollup interactions
|
|
117
|
+
- `createSerializer`: Function to create a Borsh serializer for your rollup schema
|
|
118
|
+
|
|
119
|
+
For detailed API documentation, please refer to the inline TypeScript documentation in the source code.
|
package/dist/index.d.mts
CHANGED
|
@@ -2,33 +2,174 @@ import SovereignClient__default from '@sovereign-sdk/client';
|
|
|
2
2
|
import * as SovereignClient from '@sovereign-sdk/client';
|
|
3
3
|
export { SovereignClient };
|
|
4
4
|
import { Signer } from '@sovereign-sdk/signers';
|
|
5
|
+
export { Schema } from '@sovereign-sdk/universal-wallet-wasm';
|
|
5
6
|
|
|
7
|
+
/**
|
|
8
|
+
* A rollup schema is a description of the types that are utilized in the rollup.
|
|
9
|
+
* It is used to serialize and deserialize the types.
|
|
10
|
+
* As well as display them in a human-readable way that is verified by the rollup.
|
|
11
|
+
*/
|
|
6
12
|
type RollupSchema = object;
|
|
13
|
+
/**
|
|
14
|
+
* A serializer is used to serialize rollup types to Borsh bytes.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* const serializer = createSerializer(yourSchema);
|
|
19
|
+
* const runtimeCall = {
|
|
20
|
+
* value_setter: {
|
|
21
|
+
* set_value: 100,
|
|
22
|
+
* },
|
|
23
|
+
* };
|
|
24
|
+
* const borshBytes = serializer.serializeRuntimeCall(runtimeCall);
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
7
27
|
type RollupSerializer = {
|
|
28
|
+
/**
|
|
29
|
+
* Serialize an input to Borsh bytes.
|
|
30
|
+
*
|
|
31
|
+
* Treats `Uint8Array` as a plain array of numbers as this is the format used by universal-wallet-wasm
|
|
32
|
+
* to represent Borsh bytes.
|
|
33
|
+
*
|
|
34
|
+
* @param input - The input to serialize.
|
|
35
|
+
* @param index - The index of the type in the schema to serialize.
|
|
36
|
+
* @returns The serialized Borsh bytes.
|
|
37
|
+
*/
|
|
8
38
|
serialize(input: unknown, index: number): Uint8Array;
|
|
39
|
+
/**
|
|
40
|
+
* Serialize a runtime call to Borsh bytes.
|
|
41
|
+
*
|
|
42
|
+
* @param input - The runtime call to serialize.
|
|
43
|
+
* @returns The serialized Borsh bytes.
|
|
44
|
+
*/
|
|
9
45
|
serializeRuntimeCall(input: unknown): Uint8Array;
|
|
46
|
+
/**
|
|
47
|
+
* Serialize an unsigned transaction to Borsh bytes.
|
|
48
|
+
*
|
|
49
|
+
* @param input - The unsigned transaction to serialize.
|
|
50
|
+
* @returns The serialized Borsh bytes.
|
|
51
|
+
*/
|
|
10
52
|
serializeUnsignedTx(input: unknown): Uint8Array;
|
|
53
|
+
/**
|
|
54
|
+
* Serialize a transaction to Borsh bytes.
|
|
55
|
+
*
|
|
56
|
+
* @param input - The transaction to serialize.
|
|
57
|
+
* @returns The serialized Borsh bytes.
|
|
58
|
+
*/
|
|
11
59
|
serializeTx(input: unknown): Uint8Array;
|
|
12
60
|
};
|
|
61
|
+
/**
|
|
62
|
+
* Create a serializer for a rollup schema.
|
|
63
|
+
*
|
|
64
|
+
* @throws {@link SovereignError} - If the schema is invalid.
|
|
65
|
+
* @param schemaObject - The rollup schema.
|
|
66
|
+
* @returns The serializer.
|
|
67
|
+
*/
|
|
13
68
|
declare function createSerializer(schemaObject: RollupSchema): RollupSerializer;
|
|
14
69
|
|
|
70
|
+
/**
|
|
71
|
+
* The base type specification for a rollup.
|
|
72
|
+
*
|
|
73
|
+
* Defines a centralised set of types that are used in the rollup.
|
|
74
|
+
* This allows for a single source of truth for the types used in the rollup.
|
|
75
|
+
*/
|
|
15
76
|
type BaseTypeSpec = {
|
|
77
|
+
/**
|
|
78
|
+
* The type of transaction used in the rollup.
|
|
79
|
+
*/
|
|
16
80
|
Transaction: any;
|
|
81
|
+
/**
|
|
82
|
+
* The type of unsigned transaction used in the rollup.
|
|
83
|
+
*/
|
|
17
84
|
UnsignedTransaction: any;
|
|
85
|
+
/**
|
|
86
|
+
* The type of runtime call used in the rollup.
|
|
87
|
+
*/
|
|
18
88
|
RuntimeCall: any;
|
|
19
89
|
};
|
|
20
90
|
type OverrideType<Base, Override> = {
|
|
21
91
|
[K in keyof Base]: K extends keyof Override ? Override[K] : Base[K];
|
|
22
92
|
};
|
|
93
|
+
/**
|
|
94
|
+
* The type specification for a rollup.
|
|
95
|
+
*
|
|
96
|
+
* Extends the base type specification with concrete types.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```typescript
|
|
100
|
+
* type TxDetails = {
|
|
101
|
+
* maxPriorityFeeBips: number;
|
|
102
|
+
* maxFee: number;
|
|
103
|
+
* gasLimit?: bigint;
|
|
104
|
+
* chainId: number;
|
|
105
|
+
* };
|
|
106
|
+
*
|
|
107
|
+
* type UnsignedTransaction = {
|
|
108
|
+
* runtimeMsg: Uint8Array;
|
|
109
|
+
* nonce: number;
|
|
110
|
+
* details: TxDetails;
|
|
111
|
+
* };
|
|
112
|
+
*
|
|
113
|
+
* type Transaction = {
|
|
114
|
+
* pubKey: string;
|
|
115
|
+
* signature: string;
|
|
116
|
+
* } & UnsignedTransaction;
|
|
117
|
+
*
|
|
118
|
+
* type MyTypeSpec = RollupTypeSpec<{
|
|
119
|
+
* UnsignedTransaction: UnsignedTransaction,
|
|
120
|
+
* Transaction: Transaction,
|
|
121
|
+
* // ...
|
|
122
|
+
* }>;
|
|
123
|
+
*
|
|
124
|
+
* // The typespec can now be used with the rollup class to provide type safety
|
|
125
|
+
* // for the transaction types.
|
|
126
|
+
* const rollup = new StandardRollup<MyTypeSpec>({
|
|
127
|
+
* schema: yourSchema,
|
|
128
|
+
* // ...
|
|
129
|
+
* });
|
|
130
|
+
* const result = await rollup.signAndSubmitTransaction({
|
|
131
|
+
* // ...
|
|
132
|
+
* });
|
|
133
|
+
* const tx = result.transaction; // Strongly typed as `Transaction` on your RollupTypeSpec.
|
|
134
|
+
* ```
|
|
135
|
+
*
|
|
136
|
+
* @param T - The type specification to extend the base type specification with.
|
|
137
|
+
*/
|
|
23
138
|
type RollupTypeSpec<T extends Partial<BaseTypeSpec>> = OverrideType<BaseTypeSpec, T>;
|
|
24
139
|
|
|
140
|
+
/**
|
|
141
|
+
* The result of signing and submitting a transaction.
|
|
142
|
+
*
|
|
143
|
+
* @template Tx The type of the transaction
|
|
144
|
+
*/
|
|
25
145
|
type TransactionResult<Tx> = {
|
|
146
|
+
/**
|
|
147
|
+
* The response from the sequencer.
|
|
148
|
+
*/
|
|
26
149
|
response: SovereignClient__default.Sequencer.TxCreateResponse;
|
|
150
|
+
/**
|
|
151
|
+
* The transaction that was submitted.
|
|
152
|
+
*/
|
|
27
153
|
transaction: Tx;
|
|
28
154
|
};
|
|
155
|
+
/**
|
|
156
|
+
* The configuration for a rollup client.
|
|
157
|
+
*/
|
|
29
158
|
type RollupConfig = {
|
|
159
|
+
/**
|
|
160
|
+
* The base URL of the rollup full node API.
|
|
161
|
+
*/
|
|
30
162
|
url?: string;
|
|
163
|
+
/**
|
|
164
|
+
* The schema of the rollup.
|
|
165
|
+
*/
|
|
31
166
|
schema: RollupSchema;
|
|
167
|
+
/**
|
|
168
|
+
* The default transaction details.
|
|
169
|
+
* This will be deprecated soon to remove Sovereign SDK specific types.
|
|
170
|
+
* @deprecated
|
|
171
|
+
* @private
|
|
172
|
+
*/
|
|
32
173
|
defaultTxDetails: TxDetails;
|
|
33
174
|
};
|
|
34
175
|
type TxDetails = {
|
|
@@ -37,32 +178,112 @@ type TxDetails = {
|
|
|
37
178
|
gas_limit?: number[];
|
|
38
179
|
chain_id: number;
|
|
39
180
|
};
|
|
181
|
+
/**
|
|
182
|
+
* The parameters for signing and submitting a transaction.
|
|
183
|
+
*/
|
|
40
184
|
type SignerParams = {
|
|
185
|
+
/**
|
|
186
|
+
* The signer to use for signing the transaction.
|
|
187
|
+
*/
|
|
41
188
|
signer: Signer;
|
|
42
189
|
};
|
|
190
|
+
/**
|
|
191
|
+
* The parameters for calling executing a runtime call transaction.
|
|
192
|
+
*/
|
|
43
193
|
type CallParams = {
|
|
194
|
+
/**
|
|
195
|
+
* The transaction details to use for the call.
|
|
196
|
+
* @deprecated This will be removed soon to remove Sovereign SDK specific types.
|
|
197
|
+
* In favor of a looser type specification.
|
|
198
|
+
*/
|
|
44
199
|
txDetails?: TxDetails;
|
|
45
200
|
} & SignerParams;
|
|
201
|
+
/**
|
|
202
|
+
* The parameters for simulating a runtime call transaction.
|
|
203
|
+
*/
|
|
46
204
|
type SimulateParams = {
|
|
205
|
+
/**
|
|
206
|
+
* The transaction details to use for the simulation.
|
|
207
|
+
* @deprecated This will be removed soon to remove Sovereign SDK specific types.
|
|
208
|
+
* In favor of a looser type specification.
|
|
209
|
+
*/
|
|
47
210
|
txDetails: TxDetails;
|
|
48
211
|
} & SignerParams;
|
|
212
|
+
/**
|
|
213
|
+
* The rollup client.
|
|
214
|
+
*
|
|
215
|
+
* @template T - The type specification for the rollup.
|
|
216
|
+
* If not provided, the base type specification will be used which uses `any` for all types.
|
|
217
|
+
*/
|
|
49
218
|
declare class StandardRollup<T extends BaseTypeSpec = BaseTypeSpec> {
|
|
50
219
|
private readonly _config;
|
|
51
220
|
private readonly _client;
|
|
52
221
|
private readonly _serializer;
|
|
222
|
+
/**
|
|
223
|
+
* Creates a new rollup client.
|
|
224
|
+
*
|
|
225
|
+
* @param config - The configuration for the rollup client.
|
|
226
|
+
*/
|
|
53
227
|
constructor(config: RollupConfig);
|
|
228
|
+
/**
|
|
229
|
+
* Submits a transaction to the rollup.
|
|
230
|
+
*
|
|
231
|
+
* @param transaction - The transaction to submit.
|
|
232
|
+
*/
|
|
54
233
|
submitTransaction(transaction: T["Transaction"]): Promise<SovereignClient__default.Sequencer.TxCreateResponse>;
|
|
234
|
+
/**
|
|
235
|
+
* Signs and submits a transaction to the rollup.
|
|
236
|
+
* Utilizes the provided signer to sign the transaction.
|
|
237
|
+
*
|
|
238
|
+
* @param unsignedTx - The unsigned transaction to sign and submit.
|
|
239
|
+
*/
|
|
55
240
|
signAndSubmitTransaction(unsignedTx: T["UnsignedTransaction"], { signer }: SignerParams): Promise<TransactionResult<T["Transaction"]>>;
|
|
241
|
+
/**
|
|
242
|
+
* Performs a runtime call transaction.
|
|
243
|
+
* Utilizes the provided signer to sign the transaction.
|
|
244
|
+
*
|
|
245
|
+
* @param runtimeMessage - The runtime message to call.
|
|
246
|
+
*/
|
|
56
247
|
call(runtimeMessage: T["RuntimeCall"], { signer, txDetails }: CallParams): Promise<TransactionResult<T["Transaction"]>>;
|
|
248
|
+
/**
|
|
249
|
+
* Simulates a runtime call transaction.
|
|
250
|
+
*
|
|
251
|
+
* This method can be useful to estimate the gas cost of a runtime call transaction.
|
|
252
|
+
*
|
|
253
|
+
* @param runtimeMessage - The runtime message to call.
|
|
254
|
+
*/
|
|
57
255
|
simulate(runtimeMessage: T["RuntimeCall"], { signer, txDetails }: SimulateParams): Promise<SovereignClient__default.Rollup.SimulateExecutionResponse>;
|
|
256
|
+
/**
|
|
257
|
+
* A ledger client that can be used to query the ledger.
|
|
258
|
+
*/
|
|
58
259
|
get ledger(): SovereignClient__default.Ledger;
|
|
260
|
+
/**
|
|
261
|
+
* A sequencer client that can be used to submit transactions & batches to the rollup.
|
|
262
|
+
*/
|
|
59
263
|
get sequencer(): SovereignClient__default.Sequencer;
|
|
264
|
+
/**
|
|
265
|
+
* A rollup client that can be used to perform operations on the rollup.
|
|
266
|
+
*/
|
|
60
267
|
get rollup(): SovereignClient__default.Rollup;
|
|
268
|
+
/**
|
|
269
|
+
* A client that can be used to perform HTTP requests to the Sovereign API.
|
|
270
|
+
* This can be used as an escape hatch to execute arbitrary requests to the Sovereign API
|
|
271
|
+
* if the operation is not supported by `ledger`, `sequencer`, or `rollup` clients.
|
|
272
|
+
*/
|
|
273
|
+
get http(): SovereignClient__default;
|
|
274
|
+
/**
|
|
275
|
+
* The serializer for the rollup.
|
|
276
|
+
* Can be used to serialize transactions, runtime calls, etc.
|
|
277
|
+
* See {@link RollupSerializer} for more information.
|
|
278
|
+
*/
|
|
61
279
|
get serializer(): RollupSerializer;
|
|
62
280
|
}
|
|
63
281
|
|
|
282
|
+
/**
|
|
283
|
+
* Base error class for Sovereign SDK Web3.
|
|
284
|
+
*/
|
|
64
285
|
declare class SovereignError extends Error {
|
|
65
286
|
constructor(message: string);
|
|
66
287
|
}
|
|
67
288
|
|
|
68
|
-
export { type BaseTypeSpec, type RollupConfig, type RollupSchema, type RollupSerializer, type RollupTypeSpec, SovereignError, StandardRollup, type TransactionResult, createSerializer };
|
|
289
|
+
export { type BaseTypeSpec, type CallParams, type RollupConfig, type RollupSchema, type RollupSerializer, type RollupTypeSpec, type SignerParams, type SimulateParams, SovereignError, StandardRollup, type TransactionResult, createSerializer };
|
package/dist/index.d.ts
CHANGED
|
@@ -2,33 +2,174 @@ import SovereignClient__default from '@sovereign-sdk/client';
|
|
|
2
2
|
import * as SovereignClient from '@sovereign-sdk/client';
|
|
3
3
|
export { SovereignClient };
|
|
4
4
|
import { Signer } from '@sovereign-sdk/signers';
|
|
5
|
+
export { Schema } from '@sovereign-sdk/universal-wallet-wasm';
|
|
5
6
|
|
|
7
|
+
/**
|
|
8
|
+
* A rollup schema is a description of the types that are utilized in the rollup.
|
|
9
|
+
* It is used to serialize and deserialize the types.
|
|
10
|
+
* As well as display them in a human-readable way that is verified by the rollup.
|
|
11
|
+
*/
|
|
6
12
|
type RollupSchema = object;
|
|
13
|
+
/**
|
|
14
|
+
* A serializer is used to serialize rollup types to Borsh bytes.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* const serializer = createSerializer(yourSchema);
|
|
19
|
+
* const runtimeCall = {
|
|
20
|
+
* value_setter: {
|
|
21
|
+
* set_value: 100,
|
|
22
|
+
* },
|
|
23
|
+
* };
|
|
24
|
+
* const borshBytes = serializer.serializeRuntimeCall(runtimeCall);
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
7
27
|
type RollupSerializer = {
|
|
28
|
+
/**
|
|
29
|
+
* Serialize an input to Borsh bytes.
|
|
30
|
+
*
|
|
31
|
+
* Treats `Uint8Array` as a plain array of numbers as this is the format used by universal-wallet-wasm
|
|
32
|
+
* to represent Borsh bytes.
|
|
33
|
+
*
|
|
34
|
+
* @param input - The input to serialize.
|
|
35
|
+
* @param index - The index of the type in the schema to serialize.
|
|
36
|
+
* @returns The serialized Borsh bytes.
|
|
37
|
+
*/
|
|
8
38
|
serialize(input: unknown, index: number): Uint8Array;
|
|
39
|
+
/**
|
|
40
|
+
* Serialize a runtime call to Borsh bytes.
|
|
41
|
+
*
|
|
42
|
+
* @param input - The runtime call to serialize.
|
|
43
|
+
* @returns The serialized Borsh bytes.
|
|
44
|
+
*/
|
|
9
45
|
serializeRuntimeCall(input: unknown): Uint8Array;
|
|
46
|
+
/**
|
|
47
|
+
* Serialize an unsigned transaction to Borsh bytes.
|
|
48
|
+
*
|
|
49
|
+
* @param input - The unsigned transaction to serialize.
|
|
50
|
+
* @returns The serialized Borsh bytes.
|
|
51
|
+
*/
|
|
10
52
|
serializeUnsignedTx(input: unknown): Uint8Array;
|
|
53
|
+
/**
|
|
54
|
+
* Serialize a transaction to Borsh bytes.
|
|
55
|
+
*
|
|
56
|
+
* @param input - The transaction to serialize.
|
|
57
|
+
* @returns The serialized Borsh bytes.
|
|
58
|
+
*/
|
|
11
59
|
serializeTx(input: unknown): Uint8Array;
|
|
12
60
|
};
|
|
61
|
+
/**
|
|
62
|
+
* Create a serializer for a rollup schema.
|
|
63
|
+
*
|
|
64
|
+
* @throws {@link SovereignError} - If the schema is invalid.
|
|
65
|
+
* @param schemaObject - The rollup schema.
|
|
66
|
+
* @returns The serializer.
|
|
67
|
+
*/
|
|
13
68
|
declare function createSerializer(schemaObject: RollupSchema): RollupSerializer;
|
|
14
69
|
|
|
70
|
+
/**
|
|
71
|
+
* The base type specification for a rollup.
|
|
72
|
+
*
|
|
73
|
+
* Defines a centralised set of types that are used in the rollup.
|
|
74
|
+
* This allows for a single source of truth for the types used in the rollup.
|
|
75
|
+
*/
|
|
15
76
|
type BaseTypeSpec = {
|
|
77
|
+
/**
|
|
78
|
+
* The type of transaction used in the rollup.
|
|
79
|
+
*/
|
|
16
80
|
Transaction: any;
|
|
81
|
+
/**
|
|
82
|
+
* The type of unsigned transaction used in the rollup.
|
|
83
|
+
*/
|
|
17
84
|
UnsignedTransaction: any;
|
|
85
|
+
/**
|
|
86
|
+
* The type of runtime call used in the rollup.
|
|
87
|
+
*/
|
|
18
88
|
RuntimeCall: any;
|
|
19
89
|
};
|
|
20
90
|
type OverrideType<Base, Override> = {
|
|
21
91
|
[K in keyof Base]: K extends keyof Override ? Override[K] : Base[K];
|
|
22
92
|
};
|
|
93
|
+
/**
|
|
94
|
+
* The type specification for a rollup.
|
|
95
|
+
*
|
|
96
|
+
* Extends the base type specification with concrete types.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```typescript
|
|
100
|
+
* type TxDetails = {
|
|
101
|
+
* maxPriorityFeeBips: number;
|
|
102
|
+
* maxFee: number;
|
|
103
|
+
* gasLimit?: bigint;
|
|
104
|
+
* chainId: number;
|
|
105
|
+
* };
|
|
106
|
+
*
|
|
107
|
+
* type UnsignedTransaction = {
|
|
108
|
+
* runtimeMsg: Uint8Array;
|
|
109
|
+
* nonce: number;
|
|
110
|
+
* details: TxDetails;
|
|
111
|
+
* };
|
|
112
|
+
*
|
|
113
|
+
* type Transaction = {
|
|
114
|
+
* pubKey: string;
|
|
115
|
+
* signature: string;
|
|
116
|
+
* } & UnsignedTransaction;
|
|
117
|
+
*
|
|
118
|
+
* type MyTypeSpec = RollupTypeSpec<{
|
|
119
|
+
* UnsignedTransaction: UnsignedTransaction,
|
|
120
|
+
* Transaction: Transaction,
|
|
121
|
+
* // ...
|
|
122
|
+
* }>;
|
|
123
|
+
*
|
|
124
|
+
* // The typespec can now be used with the rollup class to provide type safety
|
|
125
|
+
* // for the transaction types.
|
|
126
|
+
* const rollup = new StandardRollup<MyTypeSpec>({
|
|
127
|
+
* schema: yourSchema,
|
|
128
|
+
* // ...
|
|
129
|
+
* });
|
|
130
|
+
* const result = await rollup.signAndSubmitTransaction({
|
|
131
|
+
* // ...
|
|
132
|
+
* });
|
|
133
|
+
* const tx = result.transaction; // Strongly typed as `Transaction` on your RollupTypeSpec.
|
|
134
|
+
* ```
|
|
135
|
+
*
|
|
136
|
+
* @param T - The type specification to extend the base type specification with.
|
|
137
|
+
*/
|
|
23
138
|
type RollupTypeSpec<T extends Partial<BaseTypeSpec>> = OverrideType<BaseTypeSpec, T>;
|
|
24
139
|
|
|
140
|
+
/**
|
|
141
|
+
* The result of signing and submitting a transaction.
|
|
142
|
+
*
|
|
143
|
+
* @template Tx The type of the transaction
|
|
144
|
+
*/
|
|
25
145
|
type TransactionResult<Tx> = {
|
|
146
|
+
/**
|
|
147
|
+
* The response from the sequencer.
|
|
148
|
+
*/
|
|
26
149
|
response: SovereignClient__default.Sequencer.TxCreateResponse;
|
|
150
|
+
/**
|
|
151
|
+
* The transaction that was submitted.
|
|
152
|
+
*/
|
|
27
153
|
transaction: Tx;
|
|
28
154
|
};
|
|
155
|
+
/**
|
|
156
|
+
* The configuration for a rollup client.
|
|
157
|
+
*/
|
|
29
158
|
type RollupConfig = {
|
|
159
|
+
/**
|
|
160
|
+
* The base URL of the rollup full node API.
|
|
161
|
+
*/
|
|
30
162
|
url?: string;
|
|
163
|
+
/**
|
|
164
|
+
* The schema of the rollup.
|
|
165
|
+
*/
|
|
31
166
|
schema: RollupSchema;
|
|
167
|
+
/**
|
|
168
|
+
* The default transaction details.
|
|
169
|
+
* This will be deprecated soon to remove Sovereign SDK specific types.
|
|
170
|
+
* @deprecated
|
|
171
|
+
* @private
|
|
172
|
+
*/
|
|
32
173
|
defaultTxDetails: TxDetails;
|
|
33
174
|
};
|
|
34
175
|
type TxDetails = {
|
|
@@ -37,32 +178,112 @@ type TxDetails = {
|
|
|
37
178
|
gas_limit?: number[];
|
|
38
179
|
chain_id: number;
|
|
39
180
|
};
|
|
181
|
+
/**
|
|
182
|
+
* The parameters for signing and submitting a transaction.
|
|
183
|
+
*/
|
|
40
184
|
type SignerParams = {
|
|
185
|
+
/**
|
|
186
|
+
* The signer to use for signing the transaction.
|
|
187
|
+
*/
|
|
41
188
|
signer: Signer;
|
|
42
189
|
};
|
|
190
|
+
/**
|
|
191
|
+
* The parameters for calling executing a runtime call transaction.
|
|
192
|
+
*/
|
|
43
193
|
type CallParams = {
|
|
194
|
+
/**
|
|
195
|
+
* The transaction details to use for the call.
|
|
196
|
+
* @deprecated This will be removed soon to remove Sovereign SDK specific types.
|
|
197
|
+
* In favor of a looser type specification.
|
|
198
|
+
*/
|
|
44
199
|
txDetails?: TxDetails;
|
|
45
200
|
} & SignerParams;
|
|
201
|
+
/**
|
|
202
|
+
* The parameters for simulating a runtime call transaction.
|
|
203
|
+
*/
|
|
46
204
|
type SimulateParams = {
|
|
205
|
+
/**
|
|
206
|
+
* The transaction details to use for the simulation.
|
|
207
|
+
* @deprecated This will be removed soon to remove Sovereign SDK specific types.
|
|
208
|
+
* In favor of a looser type specification.
|
|
209
|
+
*/
|
|
47
210
|
txDetails: TxDetails;
|
|
48
211
|
} & SignerParams;
|
|
212
|
+
/**
|
|
213
|
+
* The rollup client.
|
|
214
|
+
*
|
|
215
|
+
* @template T - The type specification for the rollup.
|
|
216
|
+
* If not provided, the base type specification will be used which uses `any` for all types.
|
|
217
|
+
*/
|
|
49
218
|
declare class StandardRollup<T extends BaseTypeSpec = BaseTypeSpec> {
|
|
50
219
|
private readonly _config;
|
|
51
220
|
private readonly _client;
|
|
52
221
|
private readonly _serializer;
|
|
222
|
+
/**
|
|
223
|
+
* Creates a new rollup client.
|
|
224
|
+
*
|
|
225
|
+
* @param config - The configuration for the rollup client.
|
|
226
|
+
*/
|
|
53
227
|
constructor(config: RollupConfig);
|
|
228
|
+
/**
|
|
229
|
+
* Submits a transaction to the rollup.
|
|
230
|
+
*
|
|
231
|
+
* @param transaction - The transaction to submit.
|
|
232
|
+
*/
|
|
54
233
|
submitTransaction(transaction: T["Transaction"]): Promise<SovereignClient__default.Sequencer.TxCreateResponse>;
|
|
234
|
+
/**
|
|
235
|
+
* Signs and submits a transaction to the rollup.
|
|
236
|
+
* Utilizes the provided signer to sign the transaction.
|
|
237
|
+
*
|
|
238
|
+
* @param unsignedTx - The unsigned transaction to sign and submit.
|
|
239
|
+
*/
|
|
55
240
|
signAndSubmitTransaction(unsignedTx: T["UnsignedTransaction"], { signer }: SignerParams): Promise<TransactionResult<T["Transaction"]>>;
|
|
241
|
+
/**
|
|
242
|
+
* Performs a runtime call transaction.
|
|
243
|
+
* Utilizes the provided signer to sign the transaction.
|
|
244
|
+
*
|
|
245
|
+
* @param runtimeMessage - The runtime message to call.
|
|
246
|
+
*/
|
|
56
247
|
call(runtimeMessage: T["RuntimeCall"], { signer, txDetails }: CallParams): Promise<TransactionResult<T["Transaction"]>>;
|
|
248
|
+
/**
|
|
249
|
+
* Simulates a runtime call transaction.
|
|
250
|
+
*
|
|
251
|
+
* This method can be useful to estimate the gas cost of a runtime call transaction.
|
|
252
|
+
*
|
|
253
|
+
* @param runtimeMessage - The runtime message to call.
|
|
254
|
+
*/
|
|
57
255
|
simulate(runtimeMessage: T["RuntimeCall"], { signer, txDetails }: SimulateParams): Promise<SovereignClient__default.Rollup.SimulateExecutionResponse>;
|
|
256
|
+
/**
|
|
257
|
+
* A ledger client that can be used to query the ledger.
|
|
258
|
+
*/
|
|
58
259
|
get ledger(): SovereignClient__default.Ledger;
|
|
260
|
+
/**
|
|
261
|
+
* A sequencer client that can be used to submit transactions & batches to the rollup.
|
|
262
|
+
*/
|
|
59
263
|
get sequencer(): SovereignClient__default.Sequencer;
|
|
264
|
+
/**
|
|
265
|
+
* A rollup client that can be used to perform operations on the rollup.
|
|
266
|
+
*/
|
|
60
267
|
get rollup(): SovereignClient__default.Rollup;
|
|
268
|
+
/**
|
|
269
|
+
* A client that can be used to perform HTTP requests to the Sovereign API.
|
|
270
|
+
* This can be used as an escape hatch to execute arbitrary requests to the Sovereign API
|
|
271
|
+
* if the operation is not supported by `ledger`, `sequencer`, or `rollup` clients.
|
|
272
|
+
*/
|
|
273
|
+
get http(): SovereignClient__default;
|
|
274
|
+
/**
|
|
275
|
+
* The serializer for the rollup.
|
|
276
|
+
* Can be used to serialize transactions, runtime calls, etc.
|
|
277
|
+
* See {@link RollupSerializer} for more information.
|
|
278
|
+
*/
|
|
61
279
|
get serializer(): RollupSerializer;
|
|
62
280
|
}
|
|
63
281
|
|
|
282
|
+
/**
|
|
283
|
+
* Base error class for Sovereign SDK Web3.
|
|
284
|
+
*/
|
|
64
285
|
declare class SovereignError extends Error {
|
|
65
286
|
constructor(message: string);
|
|
66
287
|
}
|
|
67
288
|
|
|
68
|
-
export { type BaseTypeSpec, type RollupConfig, type RollupSchema, type RollupSerializer, type RollupTypeSpec, SovereignError, StandardRollup, type TransactionResult, createSerializer };
|
|
289
|
+
export { type BaseTypeSpec, type CallParams, type RollupConfig, type RollupSchema, type RollupSerializer, type RollupTypeSpec, type SignerParams, type SimulateParams, SovereignError, StandardRollup, type TransactionResult, createSerializer };
|
package/dist/index.js
CHANGED
|
@@ -64,6 +64,7 @@ var __async = (__this, __arguments, generator) => {
|
|
|
64
64
|
// src/index.ts
|
|
65
65
|
var src_exports = {};
|
|
66
66
|
__export(src_exports, {
|
|
67
|
+
Schema: () => import_universal_wallet_wasm2.Schema,
|
|
67
68
|
SovereignClient: () => SovereignClient2,
|
|
68
69
|
SovereignError: () => SovereignError,
|
|
69
70
|
StandardRollup: () => StandardRollup,
|
|
@@ -149,11 +150,21 @@ function bytesToHex(arr) {
|
|
|
149
150
|
|
|
150
151
|
// src/rollup.ts
|
|
151
152
|
var StandardRollup = class {
|
|
153
|
+
/**
|
|
154
|
+
* Creates a new rollup client.
|
|
155
|
+
*
|
|
156
|
+
* @param config - The configuration for the rollup client.
|
|
157
|
+
*/
|
|
152
158
|
constructor(config) {
|
|
153
159
|
this._client = new import_client.default({ baseURL: config.url });
|
|
154
160
|
this._serializer = createSerializer(config.schema);
|
|
155
161
|
this._config = config;
|
|
156
162
|
}
|
|
163
|
+
/**
|
|
164
|
+
* Submits a transaction to the rollup.
|
|
165
|
+
*
|
|
166
|
+
* @param transaction - The transaction to submit.
|
|
167
|
+
*/
|
|
157
168
|
submitTransaction(transaction) {
|
|
158
169
|
return __async(this, null, function* () {
|
|
159
170
|
const serializedTx = this.serializer.serializeTx(transaction);
|
|
@@ -162,6 +173,12 @@ var StandardRollup = class {
|
|
|
162
173
|
});
|
|
163
174
|
});
|
|
164
175
|
}
|
|
176
|
+
/**
|
|
177
|
+
* Signs and submits a transaction to the rollup.
|
|
178
|
+
* Utilizes the provided signer to sign the transaction.
|
|
179
|
+
*
|
|
180
|
+
* @param unsignedTx - The unsigned transaction to sign and submit.
|
|
181
|
+
*/
|
|
165
182
|
signAndSubmitTransaction(_0, _1) {
|
|
166
183
|
return __async(this, arguments, function* (unsignedTx, { signer }) {
|
|
167
184
|
const serializedUnsignedTx = this.serializer.serializeUnsignedTx(unsignedTx);
|
|
@@ -179,6 +196,12 @@ var StandardRollup = class {
|
|
|
179
196
|
return { transaction: tx, response: result };
|
|
180
197
|
});
|
|
181
198
|
}
|
|
199
|
+
/**
|
|
200
|
+
* Performs a runtime call transaction.
|
|
201
|
+
* Utilizes the provided signer to sign the transaction.
|
|
202
|
+
*
|
|
203
|
+
* @param runtimeMessage - The runtime message to call.
|
|
204
|
+
*/
|
|
182
205
|
call(_0, _1) {
|
|
183
206
|
return __async(this, arguments, function* (runtimeMessage, { signer, txDetails }) {
|
|
184
207
|
const runtimeCall = this.serializer.serializeRuntimeCall(runtimeMessage);
|
|
@@ -198,6 +221,13 @@ var StandardRollup = class {
|
|
|
198
221
|
);
|
|
199
222
|
});
|
|
200
223
|
}
|
|
224
|
+
/**
|
|
225
|
+
* Simulates a runtime call transaction.
|
|
226
|
+
*
|
|
227
|
+
* This method can be useful to estimate the gas cost of a runtime call transaction.
|
|
228
|
+
*
|
|
229
|
+
* @param runtimeMessage - The runtime message to call.
|
|
230
|
+
*/
|
|
201
231
|
simulate(_0, _1) {
|
|
202
232
|
return __async(this, arguments, function* (runtimeMessage, { signer, txDetails }) {
|
|
203
233
|
const runtimeCall = this.serializer.serializeRuntimeCall(runtimeMessage);
|
|
@@ -215,15 +245,37 @@ var StandardRollup = class {
|
|
|
215
245
|
return response.data;
|
|
216
246
|
});
|
|
217
247
|
}
|
|
248
|
+
/**
|
|
249
|
+
* A ledger client that can be used to query the ledger.
|
|
250
|
+
*/
|
|
218
251
|
get ledger() {
|
|
219
252
|
return this._client.ledger;
|
|
220
253
|
}
|
|
254
|
+
/**
|
|
255
|
+
* A sequencer client that can be used to submit transactions & batches to the rollup.
|
|
256
|
+
*/
|
|
221
257
|
get sequencer() {
|
|
222
258
|
return this._client.sequencer;
|
|
223
259
|
}
|
|
260
|
+
/**
|
|
261
|
+
* A rollup client that can be used to perform operations on the rollup.
|
|
262
|
+
*/
|
|
224
263
|
get rollup() {
|
|
225
264
|
return this._client.rollup;
|
|
226
265
|
}
|
|
266
|
+
/**
|
|
267
|
+
* A client that can be used to perform HTTP requests to the Sovereign API.
|
|
268
|
+
* This can be used as an escape hatch to execute arbitrary requests to the Sovereign API
|
|
269
|
+
* if the operation is not supported by `ledger`, `sequencer`, or `rollup` clients.
|
|
270
|
+
*/
|
|
271
|
+
get http() {
|
|
272
|
+
return this._client;
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* The serializer for the rollup.
|
|
276
|
+
* Can be used to serialize transactions, runtime calls, etc.
|
|
277
|
+
* See {@link RollupSerializer} for more information.
|
|
278
|
+
*/
|
|
227
279
|
get serializer() {
|
|
228
280
|
return this._serializer;
|
|
229
281
|
}
|
|
@@ -231,8 +283,10 @@ var StandardRollup = class {
|
|
|
231
283
|
|
|
232
284
|
// src/index.ts
|
|
233
285
|
var SovereignClient2 = __toESM(require("@sovereign-sdk/client"));
|
|
286
|
+
var import_universal_wallet_wasm2 = require("@sovereign-sdk/universal-wallet-wasm");
|
|
234
287
|
// Annotate the CommonJS export names for ESM import in node:
|
|
235
288
|
0 && (module.exports = {
|
|
289
|
+
Schema,
|
|
236
290
|
SovereignClient,
|
|
237
291
|
SovereignError,
|
|
238
292
|
StandardRollup,
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/rollup.ts","../src/serialization.ts","../src/errors.ts","../src/utils.ts"],"sourcesContent":["export * from \"./rollup\";\nexport * from \"./serialization\";\nexport * from \"./errors\";\nexport * from \"./type-spec\";\nexport * as SovereignClient from \"@sovereign-sdk/client\";\n","import SovereignClient from \"@sovereign-sdk/client\";\nimport type { Signer } from \"@sovereign-sdk/signers\";\nimport { Base64 } from \"js-base64\";\nimport {\n type RollupSchema,\n type RollupSerializer,\n createSerializer,\n} from \"./serialization\";\nimport type { BaseTypeSpec } from \"./type-spec\";\nimport { bytesToHex } from \"./utils\";\n\nexport type TransactionResult<Tx> = {\n response: SovereignClient.Sequencer.TxCreateResponse;\n transaction: Tx;\n};\n\nexport type RollupConfig = {\n url?: string;\n schema: RollupSchema;\n defaultTxDetails: TxDetails;\n};\n\ntype TxDetails = {\n max_priority_fee_bips: number;\n max_fee: number;\n gas_limit?: number[];\n chain_id: number;\n};\n\ntype SignerParams = {\n signer: Signer;\n};\n\ntype CallParams = {\n txDetails?: TxDetails;\n} & SignerParams;\n\ntype SimulateParams = {\n txDetails: TxDetails;\n} & SignerParams;\n\nexport class StandardRollup<T extends BaseTypeSpec = BaseTypeSpec> {\n private readonly _config: RollupConfig;\n private readonly _client: SovereignClient;\n private readonly _serializer: RollupSerializer;\n\n constructor(config: RollupConfig) {\n this._client = new SovereignClient({ baseURL: config.url });\n this._serializer = createSerializer(config.schema);\n this._config = config;\n }\n\n async submitTransaction(\n transaction: T[\"Transaction\"],\n ): Promise<SovereignClient.Sequencer.TxCreateResponse> {\n const serializedTx = this.serializer.serializeTx(transaction);\n\n return this.sequencer.txs.create({\n body: Base64.fromUint8Array(serializedTx),\n });\n }\n\n async signAndSubmitTransaction(\n unsignedTx: T[\"UnsignedTransaction\"],\n { signer }: SignerParams,\n ): Promise<TransactionResult<T[\"Transaction\"]>> {\n const serializedUnsignedTx =\n this.serializer.serializeUnsignedTx(unsignedTx);\n const signature = await signer.sign(serializedUnsignedTx);\n const publicKey = await signer.publicKey();\n const tx = {\n pub_key: {\n pub_key: publicKey,\n },\n signature: {\n msg_sig: signature,\n },\n ...unsignedTx,\n } as T[\"Transaction\"];\n const result = await this.submitTransaction(tx);\n\n return { transaction: tx, response: result };\n }\n\n async call(\n runtimeMessage: T[\"RuntimeCall\"],\n { signer, txDetails }: CallParams,\n ): Promise<TransactionResult<T[\"Transaction\"]>> {\n const runtimeCall = this.serializer.serializeRuntimeCall(runtimeMessage);\n const publicKey = await signer.publicKey();\n const dedup = await this.rollup.addresses.dedup(bytesToHex(publicKey));\n // biome-ignore lint/suspicious/noExplicitAny: fix later\n const nonce = (dedup.data as any).nonce as number;\n const unsignedTx = {\n runtime_msg: runtimeCall,\n nonce,\n details: txDetails ?? this._config.defaultTxDetails,\n };\n\n return this.signAndSubmitTransaction(\n unsignedTx as T[\"UnsignedTransaction\"],\n {\n signer,\n },\n );\n }\n\n async simulate(\n runtimeMessage: T[\"RuntimeCall\"],\n { signer, txDetails }: SimulateParams,\n ): Promise<SovereignClient.Rollup.SimulateExecutionResponse> {\n const runtimeCall = this.serializer.serializeRuntimeCall(runtimeMessage);\n const publicKey = await signer.publicKey();\n const dedup = await this.rollup.addresses.dedup(bytesToHex(publicKey));\n // biome-ignore lint/suspicious/noExplicitAny: fix later\n const nonce = (dedup.data as any).nonce as number;\n const response = await this.rollup.simulate({\n body: {\n details: txDetails,\n encoded_call_message: bytesToHex(runtimeCall),\n nonce,\n sender_pub_key: bytesToHex(publicKey),\n },\n });\n\n // biome-ignore lint/style/noNonNullAssertion: fix later\n return response.data!;\n }\n\n get ledger(): SovereignClient.Ledger {\n return this._client.ledger;\n }\n\n get sequencer(): SovereignClient.Sequencer {\n return this._client.sequencer;\n }\n\n get rollup(): SovereignClient.Rollup {\n return this._client.rollup;\n }\n\n get serializer(): RollupSerializer {\n return this._serializer;\n }\n}\n","import { KnownTypeId, Schema } from \"@sovereign-sdk/universal-wallet-wasm\";\nimport { SovereignError } from \"./errors\";\n\nexport type RollupSchema = object;\n\nexport type RollupSerializer = {\n serialize(input: unknown, index: number): Uint8Array;\n serializeRuntimeCall(input: unknown): Uint8Array;\n serializeUnsignedTx(input: unknown): Uint8Array;\n serializeTx(input: unknown): Uint8Array;\n};\n\nfunction loadSchema(schemaObject: RollupSchema): Schema {\n try {\n return Schema.fromJSON(JSON.stringify(schemaObject));\n } catch (err) {\n throw new SovereignError(`Failed to create runtime schema due to: ${err}`);\n }\n}\n\nexport function createSerializer(schemaObject: RollupSchema): RollupSerializer {\n const schema = loadSchema(schemaObject);\n\n return {\n serialize(input: unknown, index: number): Uint8Array {\n try {\n return schema.jsonToBorsh(\n index,\n JSON.stringify(input, (_, value) => {\n if (value instanceof Uint8Array) {\n return Array.from(value);\n }\n return value;\n }),\n );\n } catch (err) {\n throw new SovereignError(`Failed to serialize input due to: ${err}`);\n }\n },\n serializeRuntimeCall(input: unknown): Uint8Array {\n return this.serialize(\n input,\n schema.knownTypeIndex(KnownTypeId.RuntimeCall),\n );\n },\n serializeUnsignedTx(input: unknown): Uint8Array {\n return this.serialize(\n input,\n schema.knownTypeIndex(KnownTypeId.UnsignedTransaction),\n );\n },\n serializeTx(input: unknown): Uint8Array {\n return this.serialize(\n input,\n schema.knownTypeIndex(KnownTypeId.Transaction),\n );\n },\n };\n}\n","export class SovereignError extends Error {\n constructor(message: string) {\n super(message);\n\n this.name = this.constructor.name;\n this.stack = new Error().stack;\n }\n}\n","export function hexToBytes(hex: string) {\n if (hex.length && hex.length % 2 !== 0) {\n throw new Error(`Invalid hex string length: ${hex.length}`);\n }\n\n const matches = hex.match(/[\\da-f]{2}/gi);\n\n if (!matches) {\n throw new Error(\"Invalid hex string: No valid hex digits found\");\n }\n\n return new Uint8Array(matches.map((h) => Number.parseInt(h, 16)));\n}\n\nexport function bytesToHex(arr: Uint8Array): string {\n let hex = \"\";\n for (let i = 0; i < arr.length; i++) {\n let hexValue = arr[i].toString(16);\n if (hexValue.length === 1) {\n hexValue = `0${hexValue}`;\n }\n hex += hexValue;\n }\n return hex;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA,yBAAAA;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,oBAA4B;AAE5B,uBAAuB;;;ACFvB,mCAAoC;;;ACA7B,IAAM,iBAAN,cAA6B,MAAM;AAAA,EACxC,YAAY,SAAiB;AAC3B,UAAM,OAAO;AAEb,SAAK,OAAO,KAAK,YAAY;AAC7B,SAAK,QAAQ,IAAI,MAAM,EAAE;AAAA,EAC3B;AACF;;;ADKA,SAAS,WAAW,cAAoC;AACtD,MAAI;AACF,WAAO,oCAAO,SAAS,KAAK,UAAU,YAAY,CAAC;AAAA,EACrD,SAAS,KAAK;AACZ,UAAM,IAAI,eAAe,2CAA2C,GAAG,EAAE;AAAA,EAC3E;AACF;AAEO,SAAS,iBAAiB,cAA8C;AAC7E,QAAM,SAAS,WAAW,YAAY;AAEtC,SAAO;AAAA,IACL,UAAU,OAAgB,OAA2B;AACnD,UAAI;AACF,eAAO,OAAO;AAAA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,WAAW,WAAW;AAAA,UAC5C;AAAA,UACA,gBAAgB,WAAW,SAAS;AAAA,QACtC;AAAA,MACF,CAAC;AAGD,aAAO,SAAS;AAAA,IAClB;AAAA;AAAA,EAEA,IAAI,SAAiC;AACnC,WAAO,KAAK,QAAQ;AAAA,EACtB;AAAA,EAEA,IAAI,YAAuC;AACzC,WAAO,KAAK,QAAQ;AAAA,EACtB;AAAA,EAEA,IAAI,SAAiC;AACnC,WAAO,KAAK,QAAQ;AAAA,EACtB;AAAA,EAEA,IAAI,aAA+B;AACjC,WAAO,KAAK;AAAA,EACd;AACF;;;AD5IA,IAAAC,mBAAiC;","names":["SovereignClient","SovereignClient","SovereignClient"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/rollup.ts","../src/serialization.ts","../src/errors.ts","../src/utils.ts"],"sourcesContent":["export * from \"./rollup\";\nexport * from \"./serialization\";\nexport * from \"./errors\";\nexport * from \"./type-spec\";\nexport * as SovereignClient from \"@sovereign-sdk/client\";\nexport { Schema } from \"@sovereign-sdk/universal-wallet-wasm\";\n","import SovereignClient from \"@sovereign-sdk/client\";\nimport type { Signer } from \"@sovereign-sdk/signers\";\nimport { Base64 } from \"js-base64\";\nimport {\n type RollupSchema,\n type RollupSerializer,\n createSerializer,\n} from \"./serialization\";\nimport type { BaseTypeSpec } from \"./type-spec\";\nimport { bytesToHex } from \"./utils\";\n\n/**\n * The result of signing and submitting a transaction.\n *\n * @template Tx The type of the transaction\n */\nexport type TransactionResult<Tx> = {\n /**\n * The response from the sequencer.\n */\n response: SovereignClient.Sequencer.TxCreateResponse;\n /**\n * The transaction that was submitted.\n */\n transaction: Tx;\n};\n\n/**\n * The configuration for a rollup client.\n */\nexport type RollupConfig = {\n /**\n * The base URL of the rollup full node API.\n */\n url?: string;\n /**\n * The schema of the rollup.\n */\n schema: RollupSchema;\n /**\n * The default transaction details.\n * This will be deprecated soon to remove Sovereign SDK specific types.\n * @deprecated\n * @private\n */\n defaultTxDetails: TxDetails;\n};\n\ntype TxDetails = {\n max_priority_fee_bips: number;\n max_fee: number;\n gas_limit?: number[];\n chain_id: number;\n};\n\n/**\n * The parameters for signing and submitting a transaction.\n */\nexport type SignerParams = {\n /**\n * The signer to use for signing the transaction.\n */\n signer: Signer;\n};\n\n/**\n * The parameters for calling executing a runtime call transaction.\n */\nexport type CallParams = {\n /**\n * The transaction details to use for the call.\n * @deprecated This will be removed soon to remove Sovereign SDK specific types.\n * In favor of a looser type specification.\n */\n txDetails?: TxDetails;\n} & SignerParams;\n\n/**\n * The parameters for simulating a runtime call transaction.\n */\nexport type SimulateParams = {\n /**\n * The transaction details to use for the simulation.\n * @deprecated This will be removed soon to remove Sovereign SDK specific types.\n * In favor of a looser type specification.\n */\n txDetails: TxDetails;\n} & SignerParams;\n\n/**\n * The rollup client.\n *\n * @template T - The type specification for the rollup.\n * If not provided, the base type specification will be used which uses `any` for all types.\n */\nexport class StandardRollup<T extends BaseTypeSpec = BaseTypeSpec> {\n private readonly _config: RollupConfig;\n private readonly _client: SovereignClient;\n private readonly _serializer: RollupSerializer;\n\n /**\n * Creates a new rollup client.\n *\n * @param config - The configuration for the rollup client.\n */\n constructor(config: RollupConfig) {\n this._client = new SovereignClient({ baseURL: config.url });\n this._serializer = createSerializer(config.schema);\n this._config = config;\n }\n\n /**\n * Submits a transaction to the rollup.\n *\n * @param transaction - The transaction to submit.\n */\n async submitTransaction(\n transaction: T[\"Transaction\"],\n ): Promise<SovereignClient.Sequencer.TxCreateResponse> {\n const serializedTx = this.serializer.serializeTx(transaction);\n\n return this.sequencer.txs.create({\n body: Base64.fromUint8Array(serializedTx),\n });\n }\n\n /**\n * Signs and submits a transaction to the rollup.\n * Utilizes the provided signer to sign the transaction.\n *\n * @param unsignedTx - The unsigned transaction to sign and submit.\n */\n async signAndSubmitTransaction(\n unsignedTx: T[\"UnsignedTransaction\"],\n { signer }: SignerParams,\n ): Promise<TransactionResult<T[\"Transaction\"]>> {\n const serializedUnsignedTx =\n this.serializer.serializeUnsignedTx(unsignedTx);\n const signature = await signer.sign(serializedUnsignedTx);\n const publicKey = await signer.publicKey();\n const tx = {\n pub_key: {\n pub_key: publicKey,\n },\n signature: {\n msg_sig: signature,\n },\n ...unsignedTx,\n } as T[\"Transaction\"];\n const result = await this.submitTransaction(tx);\n\n return { transaction: tx, response: result };\n }\n\n /**\n * Performs a runtime call transaction.\n * Utilizes the provided signer to sign the transaction.\n *\n * @param runtimeMessage - The runtime message to call.\n */\n async call(\n runtimeMessage: T[\"RuntimeCall\"],\n { signer, txDetails }: CallParams,\n ): Promise<TransactionResult<T[\"Transaction\"]>> {\n const runtimeCall = this.serializer.serializeRuntimeCall(runtimeMessage);\n const publicKey = await signer.publicKey();\n const dedup = await this.rollup.addresses.dedup(bytesToHex(publicKey));\n // biome-ignore lint/suspicious/noExplicitAny: fix later\n const nonce = (dedup.data as any).nonce as number;\n const unsignedTx = {\n runtime_msg: runtimeCall,\n nonce,\n details: txDetails ?? this._config.defaultTxDetails,\n };\n\n return this.signAndSubmitTransaction(\n unsignedTx as T[\"UnsignedTransaction\"],\n {\n signer,\n },\n );\n }\n\n /**\n * Simulates a runtime call transaction.\n *\n * This method can be useful to estimate the gas cost of a runtime call transaction.\n *\n * @param runtimeMessage - The runtime message to call.\n */\n async simulate(\n runtimeMessage: T[\"RuntimeCall\"],\n { signer, txDetails }: SimulateParams,\n ): Promise<SovereignClient.Rollup.SimulateExecutionResponse> {\n const runtimeCall = this.serializer.serializeRuntimeCall(runtimeMessage);\n const publicKey = await signer.publicKey();\n const dedup = await this.rollup.addresses.dedup(bytesToHex(publicKey));\n // biome-ignore lint/suspicious/noExplicitAny: fix later\n const nonce = (dedup.data as any).nonce as number;\n const response = await this.rollup.simulate({\n body: {\n details: txDetails,\n encoded_call_message: bytesToHex(runtimeCall),\n nonce,\n sender_pub_key: bytesToHex(publicKey),\n },\n });\n\n // biome-ignore lint/style/noNonNullAssertion: fix later\n return response.data!;\n }\n\n /**\n * A ledger client that can be used to query the ledger.\n */\n get ledger(): SovereignClient.Ledger {\n return this._client.ledger;\n }\n\n /**\n * A sequencer client that can be used to submit transactions & batches to the rollup.\n */\n get sequencer(): SovereignClient.Sequencer {\n return this._client.sequencer;\n }\n\n /**\n * A rollup client that can be used to perform operations on the rollup.\n */\n get rollup(): SovereignClient.Rollup {\n return this._client.rollup;\n }\n\n /**\n * A client that can be used to perform HTTP requests to the Sovereign API.\n * This can be used as an escape hatch to execute arbitrary requests to the Sovereign API\n * if the operation is not supported by `ledger`, `sequencer`, or `rollup` clients.\n */\n get http(): SovereignClient {\n return this._client;\n }\n\n /**\n * The serializer for the rollup.\n * Can be used to serialize transactions, runtime calls, etc.\n * See {@link RollupSerializer} for more information.\n */\n get serializer(): RollupSerializer {\n return this._serializer;\n }\n}\n","import { KnownTypeId, Schema } from \"@sovereign-sdk/universal-wallet-wasm\";\nimport { SovereignError } from \"./errors\";\n\n/**\n * A rollup schema is a description of the types that are utilized in the rollup.\n * It is used to serialize and deserialize the types.\n * As well as display them in a human-readable way that is verified by the rollup.\n */\nexport type RollupSchema = object;\n\n/**\n * A serializer is used to serialize rollup types to Borsh bytes.\n *\n * @example\n * ```typescript\n * const serializer = createSerializer(yourSchema);\n * const runtimeCall = {\n * value_setter: {\n * set_value: 100,\n * },\n * };\n * const borshBytes = serializer.serializeRuntimeCall(runtimeCall);\n * ```\n */\nexport type RollupSerializer = {\n /**\n * Serialize an input to Borsh bytes.\n *\n * Treats `Uint8Array` as a plain array of numbers as this is the format used by universal-wallet-wasm\n * to represent Borsh bytes.\n *\n * @param input - The input to serialize.\n * @param index - The index of the type in the schema to serialize.\n * @returns The serialized Borsh bytes.\n */\n serialize(input: unknown, index: number): Uint8Array;\n\n /**\n * Serialize a runtime call to Borsh bytes.\n *\n * @param input - The runtime call to serialize.\n * @returns The serialized Borsh bytes.\n */\n serializeRuntimeCall(input: unknown): Uint8Array;\n\n /**\n * Serialize an unsigned transaction to Borsh bytes.\n *\n * @param input - The unsigned transaction to serialize.\n * @returns The serialized Borsh bytes.\n */\n serializeUnsignedTx(input: unknown): Uint8Array;\n\n /**\n * Serialize a transaction to Borsh bytes.\n *\n * @param input - The transaction to serialize.\n * @returns The serialized Borsh bytes.\n */\n serializeTx(input: unknown): Uint8Array;\n};\n\nfunction loadSchema(schemaObject: RollupSchema): Schema {\n try {\n return Schema.fromJSON(JSON.stringify(schemaObject));\n } catch (err) {\n throw new SovereignError(`Failed to create runtime schema due to: ${err}`);\n }\n}\n\n/**\n * Create a serializer for a rollup schema.\n *\n * @throws {@link SovereignError} - If the schema is invalid.\n * @param schemaObject - The rollup schema.\n * @returns The serializer.\n */\nexport function createSerializer(schemaObject: RollupSchema): RollupSerializer {\n const schema = loadSchema(schemaObject);\n\n return {\n serialize(input: unknown, index: number): Uint8Array {\n try {\n return schema.jsonToBorsh(\n index,\n JSON.stringify(input, (_, value) => {\n if (value instanceof Uint8Array) {\n return Array.from(value);\n }\n return value;\n }),\n );\n } catch (err) {\n throw new SovereignError(`Failed to serialize input due to: ${err}`);\n }\n },\n serializeRuntimeCall(input: unknown): Uint8Array {\n return this.serialize(\n input,\n schema.knownTypeIndex(KnownTypeId.RuntimeCall),\n );\n },\n serializeUnsignedTx(input: unknown): Uint8Array {\n return this.serialize(\n input,\n schema.knownTypeIndex(KnownTypeId.UnsignedTransaction),\n );\n },\n serializeTx(input: unknown): Uint8Array {\n return this.serialize(\n input,\n schema.knownTypeIndex(KnownTypeId.Transaction),\n );\n },\n };\n}\n","/**\n * Base error class for Sovereign SDK Web3.\n */\nexport class SovereignError extends Error {\n constructor(message: string) {\n super(message);\n\n this.name = this.constructor.name;\n this.stack = new Error().stack;\n }\n}\n","export function hexToBytes(hex: string) {\n if (hex.length && hex.length % 2 !== 0) {\n throw new Error(`Invalid hex string length: ${hex.length}`);\n }\n\n const matches = hex.match(/[\\da-f]{2}/gi);\n\n if (!matches) {\n throw new Error(\"Invalid hex string: No valid hex digits found\");\n }\n\n return new Uint8Array(matches.map((h) => Number.parseInt(h, 16)));\n}\n\nexport function bytesToHex(arr: Uint8Array): string {\n let hex = \"\";\n for (let i = 0; i < arr.length; i++) {\n let hexValue = arr[i].toString(16);\n if (hexValue.length === 1) {\n hexValue = `0${hexValue}`;\n }\n hex += hexValue;\n }\n return hex;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA,yBAAAA;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,oBAA4B;AAE5B,uBAAuB;;;ACFvB,mCAAoC;;;ACG7B,IAAM,iBAAN,cAA6B,MAAM;AAAA,EACxC,YAAY,SAAiB;AAC3B,UAAM,OAAO;AAEb,SAAK,OAAO,KAAK,YAAY;AAC7B,SAAK,QAAQ,IAAI,MAAM,EAAE;AAAA,EAC3B;AACF;;;ADoDA,SAAS,WAAW,cAAoC;AACtD,MAAI;AACF,WAAO,oCAAO,SAAS,KAAK,UAAU,YAAY,CAAC;AAAA,EACrD,SAAS,KAAK;AACZ,UAAM,IAAI,eAAe,2CAA2C,GAAG,EAAE;AAAA,EAC3E;AACF;AASO,SAAS,iBAAiB,cAA8C;AAC7E,QAAM,SAAS,WAAW,YAAY;AAEtC,SAAO;AAAA,IACL,UAAU,OAAgB,OAA2B;AACnD,UAAI;AACF,eAAO,OAAO;AAAA,UACZ;AAAA,UACA,KAAK,UAAU,OAAO,CAAC,GAAG,UAAU;AAClC,gBAAI,iBAAiB,YAAY;AAC/B,qBAAO,MAAM,KAAK,KAAK;AAAA,YACzB;AACA,mBAAO;AAAA,UACT,CAAC;AAAA,QACH;AAAA,MACF,SAAS,KAAK;AACZ,cAAM,IAAI,eAAe,qCAAqC,GAAG,EAAE;AAAA,MACrE;AAAA,IACF;AAAA,IACA,qBAAqB,OAA4B;AAC/C,aAAO,KAAK;AAAA,QACV;AAAA,QACA,OAAO,eAAe,yCAAY,WAAW;AAAA,MAC/C;AAAA,IACF;AAAA,IACA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AAA;AAAA;AAAA,EAKA,IAAI,YAAuC;AACzC,WAAO,KAAK,QAAQ;AAAA,EACtB;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,SAAiC;AACnC,WAAO,KAAK,QAAQ;AAAA,EACtB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,IAAI,OAAwB;AAC1B,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,IAAI,aAA+B;AACjC,WAAO,KAAK;AAAA,EACd;AACF;;;ADtPA,IAAAC,mBAAiC;AACjC,IAAAC,gCAAuB;","names":["SovereignClient","SovereignClient","SovereignClient","import_universal_wallet_wasm"]}
|
package/dist/index.mjs
CHANGED
|
@@ -113,11 +113,21 @@ function bytesToHex(arr) {
|
|
|
113
113
|
|
|
114
114
|
// src/rollup.ts
|
|
115
115
|
var StandardRollup = class {
|
|
116
|
+
/**
|
|
117
|
+
* Creates a new rollup client.
|
|
118
|
+
*
|
|
119
|
+
* @param config - The configuration for the rollup client.
|
|
120
|
+
*/
|
|
116
121
|
constructor(config) {
|
|
117
122
|
this._client = new SovereignClient({ baseURL: config.url });
|
|
118
123
|
this._serializer = createSerializer(config.schema);
|
|
119
124
|
this._config = config;
|
|
120
125
|
}
|
|
126
|
+
/**
|
|
127
|
+
* Submits a transaction to the rollup.
|
|
128
|
+
*
|
|
129
|
+
* @param transaction - The transaction to submit.
|
|
130
|
+
*/
|
|
121
131
|
submitTransaction(transaction) {
|
|
122
132
|
return __async(this, null, function* () {
|
|
123
133
|
const serializedTx = this.serializer.serializeTx(transaction);
|
|
@@ -126,6 +136,12 @@ var StandardRollup = class {
|
|
|
126
136
|
});
|
|
127
137
|
});
|
|
128
138
|
}
|
|
139
|
+
/**
|
|
140
|
+
* Signs and submits a transaction to the rollup.
|
|
141
|
+
* Utilizes the provided signer to sign the transaction.
|
|
142
|
+
*
|
|
143
|
+
* @param unsignedTx - The unsigned transaction to sign and submit.
|
|
144
|
+
*/
|
|
129
145
|
signAndSubmitTransaction(_0, _1) {
|
|
130
146
|
return __async(this, arguments, function* (unsignedTx, { signer }) {
|
|
131
147
|
const serializedUnsignedTx = this.serializer.serializeUnsignedTx(unsignedTx);
|
|
@@ -143,6 +159,12 @@ var StandardRollup = class {
|
|
|
143
159
|
return { transaction: tx, response: result };
|
|
144
160
|
});
|
|
145
161
|
}
|
|
162
|
+
/**
|
|
163
|
+
* Performs a runtime call transaction.
|
|
164
|
+
* Utilizes the provided signer to sign the transaction.
|
|
165
|
+
*
|
|
166
|
+
* @param runtimeMessage - The runtime message to call.
|
|
167
|
+
*/
|
|
146
168
|
call(_0, _1) {
|
|
147
169
|
return __async(this, arguments, function* (runtimeMessage, { signer, txDetails }) {
|
|
148
170
|
const runtimeCall = this.serializer.serializeRuntimeCall(runtimeMessage);
|
|
@@ -162,6 +184,13 @@ var StandardRollup = class {
|
|
|
162
184
|
);
|
|
163
185
|
});
|
|
164
186
|
}
|
|
187
|
+
/**
|
|
188
|
+
* Simulates a runtime call transaction.
|
|
189
|
+
*
|
|
190
|
+
* This method can be useful to estimate the gas cost of a runtime call transaction.
|
|
191
|
+
*
|
|
192
|
+
* @param runtimeMessage - The runtime message to call.
|
|
193
|
+
*/
|
|
165
194
|
simulate(_0, _1) {
|
|
166
195
|
return __async(this, arguments, function* (runtimeMessage, { signer, txDetails }) {
|
|
167
196
|
const runtimeCall = this.serializer.serializeRuntimeCall(runtimeMessage);
|
|
@@ -179,15 +208,37 @@ var StandardRollup = class {
|
|
|
179
208
|
return response.data;
|
|
180
209
|
});
|
|
181
210
|
}
|
|
211
|
+
/**
|
|
212
|
+
* A ledger client that can be used to query the ledger.
|
|
213
|
+
*/
|
|
182
214
|
get ledger() {
|
|
183
215
|
return this._client.ledger;
|
|
184
216
|
}
|
|
217
|
+
/**
|
|
218
|
+
* A sequencer client that can be used to submit transactions & batches to the rollup.
|
|
219
|
+
*/
|
|
185
220
|
get sequencer() {
|
|
186
221
|
return this._client.sequencer;
|
|
187
222
|
}
|
|
223
|
+
/**
|
|
224
|
+
* A rollup client that can be used to perform operations on the rollup.
|
|
225
|
+
*/
|
|
188
226
|
get rollup() {
|
|
189
227
|
return this._client.rollup;
|
|
190
228
|
}
|
|
229
|
+
/**
|
|
230
|
+
* A client that can be used to perform HTTP requests to the Sovereign API.
|
|
231
|
+
* This can be used as an escape hatch to execute arbitrary requests to the Sovereign API
|
|
232
|
+
* if the operation is not supported by `ledger`, `sequencer`, or `rollup` clients.
|
|
233
|
+
*/
|
|
234
|
+
get http() {
|
|
235
|
+
return this._client;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* The serializer for the rollup.
|
|
239
|
+
* Can be used to serialize transactions, runtime calls, etc.
|
|
240
|
+
* See {@link RollupSerializer} for more information.
|
|
241
|
+
*/
|
|
191
242
|
get serializer() {
|
|
192
243
|
return this._serializer;
|
|
193
244
|
}
|
|
@@ -195,7 +246,9 @@ var StandardRollup = class {
|
|
|
195
246
|
|
|
196
247
|
// src/index.ts
|
|
197
248
|
import * as SovereignClient2 from "@sovereign-sdk/client";
|
|
249
|
+
import { Schema as Schema2 } from "@sovereign-sdk/universal-wallet-wasm";
|
|
198
250
|
export {
|
|
251
|
+
Schema2 as Schema,
|
|
199
252
|
SovereignClient2 as SovereignClient,
|
|
200
253
|
SovereignError,
|
|
201
254
|
StandardRollup,
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/rollup.ts","../src/serialization.ts","../src/errors.ts","../src/utils.ts","../src/index.ts"],"sourcesContent":["import SovereignClient from \"@sovereign-sdk/client\";\nimport type { Signer } from \"@sovereign-sdk/signers\";\nimport { Base64 } from \"js-base64\";\nimport {\n type RollupSchema,\n type RollupSerializer,\n createSerializer,\n} from \"./serialization\";\nimport type { BaseTypeSpec } from \"./type-spec\";\nimport { bytesToHex } from \"./utils\";\n\nexport type TransactionResult<Tx> = {\n response: SovereignClient.Sequencer.TxCreateResponse;\n transaction: Tx;\n};\n\nexport type RollupConfig = {\n url?: string;\n schema: RollupSchema;\n defaultTxDetails: TxDetails;\n};\n\ntype TxDetails = {\n max_priority_fee_bips: number;\n max_fee: number;\n gas_limit?: number[];\n chain_id: number;\n};\n\ntype SignerParams = {\n signer: Signer;\n};\n\ntype CallParams = {\n txDetails?: TxDetails;\n} & SignerParams;\n\ntype SimulateParams = {\n txDetails: TxDetails;\n} & SignerParams;\n\nexport class StandardRollup<T extends BaseTypeSpec = BaseTypeSpec> {\n private readonly _config: RollupConfig;\n private readonly _client: SovereignClient;\n private readonly _serializer: RollupSerializer;\n\n constructor(config: RollupConfig) {\n this._client = new SovereignClient({ baseURL: config.url });\n this._serializer = createSerializer(config.schema);\n this._config = config;\n }\n\n async submitTransaction(\n transaction: T[\"Transaction\"],\n ): Promise<SovereignClient.Sequencer.TxCreateResponse> {\n const serializedTx = this.serializer.serializeTx(transaction);\n\n return this.sequencer.txs.create({\n body: Base64.fromUint8Array(serializedTx),\n });\n }\n\n async signAndSubmitTransaction(\n unsignedTx: T[\"UnsignedTransaction\"],\n { signer }: SignerParams,\n ): Promise<TransactionResult<T[\"Transaction\"]>> {\n const serializedUnsignedTx =\n this.serializer.serializeUnsignedTx(unsignedTx);\n const signature = await signer.sign(serializedUnsignedTx);\n const publicKey = await signer.publicKey();\n const tx = {\n pub_key: {\n pub_key: publicKey,\n },\n signature: {\n msg_sig: signature,\n },\n ...unsignedTx,\n } as T[\"Transaction\"];\n const result = await this.submitTransaction(tx);\n\n return { transaction: tx, response: result };\n }\n\n async call(\n runtimeMessage: T[\"RuntimeCall\"],\n { signer, txDetails }: CallParams,\n ): Promise<TransactionResult<T[\"Transaction\"]>> {\n const runtimeCall = this.serializer.serializeRuntimeCall(runtimeMessage);\n const publicKey = await signer.publicKey();\n const dedup = await this.rollup.addresses.dedup(bytesToHex(publicKey));\n // biome-ignore lint/suspicious/noExplicitAny: fix later\n const nonce = (dedup.data as any).nonce as number;\n const unsignedTx = {\n runtime_msg: runtimeCall,\n nonce,\n details: txDetails ?? this._config.defaultTxDetails,\n };\n\n return this.signAndSubmitTransaction(\n unsignedTx as T[\"UnsignedTransaction\"],\n {\n signer,\n },\n );\n }\n\n async simulate(\n runtimeMessage: T[\"RuntimeCall\"],\n { signer, txDetails }: SimulateParams,\n ): Promise<SovereignClient.Rollup.SimulateExecutionResponse> {\n const runtimeCall = this.serializer.serializeRuntimeCall(runtimeMessage);\n const publicKey = await signer.publicKey();\n const dedup = await this.rollup.addresses.dedup(bytesToHex(publicKey));\n // biome-ignore lint/suspicious/noExplicitAny: fix later\n const nonce = (dedup.data as any).nonce as number;\n const response = await this.rollup.simulate({\n body: {\n details: txDetails,\n encoded_call_message: bytesToHex(runtimeCall),\n nonce,\n sender_pub_key: bytesToHex(publicKey),\n },\n });\n\n // biome-ignore lint/style/noNonNullAssertion: fix later\n return response.data!;\n }\n\n get ledger(): SovereignClient.Ledger {\n return this._client.ledger;\n }\n\n get sequencer(): SovereignClient.Sequencer {\n return this._client.sequencer;\n }\n\n get rollup(): SovereignClient.Rollup {\n return this._client.rollup;\n }\n\n get serializer(): RollupSerializer {\n return this._serializer;\n }\n}\n","import { KnownTypeId, Schema } from \"@sovereign-sdk/universal-wallet-wasm\";\nimport { SovereignError } from \"./errors\";\n\nexport type RollupSchema = object;\n\nexport type RollupSerializer = {\n serialize(input: unknown, index: number): Uint8Array;\n serializeRuntimeCall(input: unknown): Uint8Array;\n serializeUnsignedTx(input: unknown): Uint8Array;\n serializeTx(input: unknown): Uint8Array;\n};\n\nfunction loadSchema(schemaObject: RollupSchema): Schema {\n try {\n return Schema.fromJSON(JSON.stringify(schemaObject));\n } catch (err) {\n throw new SovereignError(`Failed to create runtime schema due to: ${err}`);\n }\n}\n\nexport function createSerializer(schemaObject: RollupSchema): RollupSerializer {\n const schema = loadSchema(schemaObject);\n\n return {\n serialize(input: unknown, index: number): Uint8Array {\n try {\n return schema.jsonToBorsh(\n index,\n JSON.stringify(input, (_, value) => {\n if (value instanceof Uint8Array) {\n return Array.from(value);\n }\n return value;\n }),\n );\n } catch (err) {\n throw new SovereignError(`Failed to serialize input due to: ${err}`);\n }\n },\n serializeRuntimeCall(input: unknown): Uint8Array {\n return this.serialize(\n input,\n schema.knownTypeIndex(KnownTypeId.RuntimeCall),\n );\n },\n serializeUnsignedTx(input: unknown): Uint8Array {\n return this.serialize(\n input,\n schema.knownTypeIndex(KnownTypeId.UnsignedTransaction),\n );\n },\n serializeTx(input: unknown): Uint8Array {\n return this.serialize(\n input,\n schema.knownTypeIndex(KnownTypeId.Transaction),\n );\n },\n };\n}\n","export class SovereignError extends Error {\n constructor(message: string) {\n super(message);\n\n this.name = this.constructor.name;\n this.stack = new Error().stack;\n }\n}\n","export function hexToBytes(hex: string) {\n if (hex.length && hex.length % 2 !== 0) {\n throw new Error(`Invalid hex string length: ${hex.length}`);\n }\n\n const matches = hex.match(/[\\da-f]{2}/gi);\n\n if (!matches) {\n throw new Error(\"Invalid hex string: No valid hex digits found\");\n }\n\n return new Uint8Array(matches.map((h) => Number.parseInt(h, 16)));\n}\n\nexport function bytesToHex(arr: Uint8Array): string {\n let hex = \"\";\n for (let i = 0; i < arr.length; i++) {\n let hexValue = arr[i].toString(16);\n if (hexValue.length === 1) {\n hexValue = `0${hexValue}`;\n }\n hex += hexValue;\n }\n return hex;\n}\n","export * from \"./rollup\";\nexport * from \"./serialization\";\nexport * from \"./errors\";\nexport * from \"./type-spec\";\nexport * as SovereignClient from \"@sovereign-sdk/client\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,OAAO,qBAAqB;AAE5B,SAAS,cAAc;;;ACFvB,SAAS,aAAa,cAAc;;;ACA7B,IAAM,iBAAN,cAA6B,MAAM;AAAA,EACxC,YAAY,SAAiB;AAC3B,UAAM,OAAO;AAEb,SAAK,OAAO,KAAK,YAAY;AAC7B,SAAK,QAAQ,IAAI,MAAM,EAAE;AAAA,EAC3B;AACF;;;ADKA,SAAS,WAAW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uC;AAC3D,YAAM,cAAc,KAAK,WAAW,qBAAqB,cAAc;AACvE,YAAM,YAAY,MAAM,OAAO,UAAU;AACzC,YAAM,QAAQ,MAAM,KAAK,OAAO,UAAU,MAAM,WAAW,SAAS,CAAC;AAErE,YAAM,QAAS,MAAM,KAAa;AAClC,YAAM,WAAW,MAAM,KAAK,OAAO,SAAS;AAAA,QAC1C,MAAM;AAAA,UACJ,SAAS;AAAA,UACT,sBAAsB,WAAW,WAAW;AAAA,UAC5C;AAAA,UACA,gBAAgB,WAAW,SAAS;AAAA,QACtC;AAAA,MACF,CAAC;AAGD,aAAO,SAAS;AAAA,IAClB;AAAA;AAAA,EAEA,IAAI,SAAiC;AACnC,WAAO,KAAK,QAAQ;AAAA,EACtB;AAAA,EAEA,IAAI,YAAuC;AACzC,WAAO,KAAK,QAAQ;AAAA,EACtB;AAAA,EAEA,IAAI,SAAiC;AACnC,WAAO,KAAK,QAAQ;AAAA,EACtB;AAAA,EAEA,IAAI,aAA+B;AACjC,WAAO,KAAK;AAAA,EACd;AACF;;;AI5IA,YAAYA,sBAAqB;","names":["SovereignClient"]}
|
|
1
|
+
{"version":3,"sources":["../src/rollup.ts","../src/serialization.ts","../src/errors.ts","../src/utils.ts","../src/index.ts"],"sourcesContent":["import SovereignClient from \"@sovereign-sdk/client\";\nimport type { Signer } from \"@sovereign-sdk/signers\";\nimport { Base64 } from \"js-base64\";\nimport {\n type RollupSchema,\n type RollupSerializer,\n createSerializer,\n} from \"./serialization\";\nimport type { BaseTypeSpec } from \"./type-spec\";\nimport { bytesToHex } from \"./utils\";\n\n/**\n * The result of signing and submitting a transaction.\n *\n * @template Tx The type of the transaction\n */\nexport type TransactionResult<Tx> = {\n /**\n * The response from the sequencer.\n */\n response: SovereignClient.Sequencer.TxCreateResponse;\n /**\n * The transaction that was submitted.\n */\n transaction: Tx;\n};\n\n/**\n * The configuration for a rollup client.\n */\nexport type RollupConfig = {\n /**\n * The base URL of the rollup full node API.\n */\n url?: string;\n /**\n * The schema of the rollup.\n */\n schema: RollupSchema;\n /**\n * The default transaction details.\n * This will be deprecated soon to remove Sovereign SDK specific types.\n * @deprecated\n * @private\n */\n defaultTxDetails: TxDetails;\n};\n\ntype TxDetails = {\n max_priority_fee_bips: number;\n max_fee: number;\n gas_limit?: number[];\n chain_id: number;\n};\n\n/**\n * The parameters for signing and submitting a transaction.\n */\nexport type SignerParams = {\n /**\n * The signer to use for signing the transaction.\n */\n signer: Signer;\n};\n\n/**\n * The parameters for calling executing a runtime call transaction.\n */\nexport type CallParams = {\n /**\n * The transaction details to use for the call.\n * @deprecated This will be removed soon to remove Sovereign SDK specific types.\n * In favor of a looser type specification.\n */\n txDetails?: TxDetails;\n} & SignerParams;\n\n/**\n * The parameters for simulating a runtime call transaction.\n */\nexport type SimulateParams = {\n /**\n * The transaction details to use for the simulation.\n * @deprecated This will be removed soon to remove Sovereign SDK specific types.\n * In favor of a looser type specification.\n */\n txDetails: TxDetails;\n} & SignerParams;\n\n/**\n * The rollup client.\n *\n * @template T - The type specification for the rollup.\n * If not provided, the base type specification will be used which uses `any` for all types.\n */\nexport class StandardRollup<T extends BaseTypeSpec = BaseTypeSpec> {\n private readonly _config: RollupConfig;\n private readonly _client: SovereignClient;\n private readonly _serializer: RollupSerializer;\n\n /**\n * Creates a new rollup client.\n *\n * @param config - The configuration for the rollup client.\n */\n constructor(config: RollupConfig) {\n this._client = new SovereignClient({ baseURL: config.url });\n this._serializer = createSerializer(config.schema);\n this._config = config;\n }\n\n /**\n * Submits a transaction to the rollup.\n *\n * @param transaction - The transaction to submit.\n */\n async submitTransaction(\n transaction: T[\"Transaction\"],\n ): Promise<SovereignClient.Sequencer.TxCreateResponse> {\n const serializedTx = this.serializer.serializeTx(transaction);\n\n return this.sequencer.txs.create({\n body: Base64.fromUint8Array(serializedTx),\n });\n }\n\n /**\n * Signs and submits a transaction to the rollup.\n * Utilizes the provided signer to sign the transaction.\n *\n * @param unsignedTx - The unsigned transaction to sign and submit.\n */\n async signAndSubmitTransaction(\n unsignedTx: T[\"UnsignedTransaction\"],\n { signer }: SignerParams,\n ): Promise<TransactionResult<T[\"Transaction\"]>> {\n const serializedUnsignedTx =\n this.serializer.serializeUnsignedTx(unsignedTx);\n const signature = await signer.sign(serializedUnsignedTx);\n const publicKey = await signer.publicKey();\n const tx = {\n pub_key: {\n pub_key: publicKey,\n },\n signature: {\n msg_sig: signature,\n },\n ...unsignedTx,\n } as T[\"Transaction\"];\n const result = await this.submitTransaction(tx);\n\n return { transaction: tx, response: result };\n }\n\n /**\n * Performs a runtime call transaction.\n * Utilizes the provided signer to sign the transaction.\n *\n * @param runtimeMessage - The runtime message to call.\n */\n async call(\n runtimeMessage: T[\"RuntimeCall\"],\n { signer, txDetails }: CallParams,\n ): Promise<TransactionResult<T[\"Transaction\"]>> {\n const runtimeCall = this.serializer.serializeRuntimeCall(runtimeMessage);\n const publicKey = await signer.publicKey();\n const dedup = await this.rollup.addresses.dedup(bytesToHex(publicKey));\n // biome-ignore lint/suspicious/noExplicitAny: fix later\n const nonce = (dedup.data as any).nonce as number;\n const unsignedTx = {\n runtime_msg: runtimeCall,\n nonce,\n details: txDetails ?? this._config.defaultTxDetails,\n };\n\n return this.signAndSubmitTransaction(\n unsignedTx as T[\"UnsignedTransaction\"],\n {\n signer,\n },\n );\n }\n\n /**\n * Simulates a runtime call transaction.\n *\n * This method can be useful to estimate the gas cost of a runtime call transaction.\n *\n * @param runtimeMessage - The runtime message to call.\n */\n async simulate(\n runtimeMessage: T[\"RuntimeCall\"],\n { signer, txDetails }: SimulateParams,\n ): Promise<SovereignClient.Rollup.SimulateExecutionResponse> {\n const runtimeCall = this.serializer.serializeRuntimeCall(runtimeMessage);\n const publicKey = await signer.publicKey();\n const dedup = await this.rollup.addresses.dedup(bytesToHex(publicKey));\n // biome-ignore lint/suspicious/noExplicitAny: fix later\n const nonce = (dedup.data as any).nonce as number;\n const response = await this.rollup.simulate({\n body: {\n details: txDetails,\n encoded_call_message: bytesToHex(runtimeCall),\n nonce,\n sender_pub_key: bytesToHex(publicKey),\n },\n });\n\n // biome-ignore lint/style/noNonNullAssertion: fix later\n return response.data!;\n }\n\n /**\n * A ledger client that can be used to query the ledger.\n */\n get ledger(): SovereignClient.Ledger {\n return this._client.ledger;\n }\n\n /**\n * A sequencer client that can be used to submit transactions & batches to the rollup.\n */\n get sequencer(): SovereignClient.Sequencer {\n return this._client.sequencer;\n }\n\n /**\n * A rollup client that can be used to perform operations on the rollup.\n */\n get rollup(): SovereignClient.Rollup {\n return this._client.rollup;\n }\n\n /**\n * A client that can be used to perform HTTP requests to the Sovereign API.\n * This can be used as an escape hatch to execute arbitrary requests to the Sovereign API\n * if the operation is not supported by `ledger`, `sequencer`, or `rollup` clients.\n */\n get http(): SovereignClient {\n return this._client;\n }\n\n /**\n * The serializer for the rollup.\n * Can be used to serialize transactions, runtime calls, etc.\n * See {@link RollupSerializer} for more information.\n */\n get serializer(): RollupSerializer {\n return this._serializer;\n }\n}\n","import { KnownTypeId, Schema } from \"@sovereign-sdk/universal-wallet-wasm\";\nimport { SovereignError } from \"./errors\";\n\n/**\n * A rollup schema is a description of the types that are utilized in the rollup.\n * It is used to serialize and deserialize the types.\n * As well as display them in a human-readable way that is verified by the rollup.\n */\nexport type RollupSchema = object;\n\n/**\n * A serializer is used to serialize rollup types to Borsh bytes.\n *\n * @example\n * ```typescript\n * const serializer = createSerializer(yourSchema);\n * const runtimeCall = {\n * value_setter: {\n * set_value: 100,\n * },\n * };\n * const borshBytes = serializer.serializeRuntimeCall(runtimeCall);\n * ```\n */\nexport type RollupSerializer = {\n /**\n * Serialize an input to Borsh bytes.\n *\n * Treats `Uint8Array` as a plain array of numbers as this is the format used by universal-wallet-wasm\n * to represent Borsh bytes.\n *\n * @param input - The input to serialize.\n * @param index - The index of the type in the schema to serialize.\n * @returns The serialized Borsh bytes.\n */\n serialize(input: unknown, index: number): Uint8Array;\n\n /**\n * Serialize a runtime call to Borsh bytes.\n *\n * @param input - The runtime call to serialize.\n * @returns The serialized Borsh bytes.\n */\n serializeRuntimeCall(input: unknown): Uint8Array;\n\n /**\n * Serialize an unsigned transaction to Borsh bytes.\n *\n * @param input - The unsigned transaction to serialize.\n * @returns The serialized Borsh bytes.\n */\n serializeUnsignedTx(input: unknown): Uint8Array;\n\n /**\n * Serialize a transaction to Borsh bytes.\n *\n * @param input - The transaction to serialize.\n * @returns The serialized Borsh bytes.\n */\n serializeTx(input: unknown): Uint8Array;\n};\n\nfunction loadSchema(schemaObject: RollupSchema): Schema {\n try {\n return Schema.fromJSON(JSON.stringify(schemaObject));\n } catch (err) {\n throw new SovereignError(`Failed to create runtime schema due to: ${err}`);\n }\n}\n\n/**\n * Create a serializer for a rollup schema.\n *\n * @throws {@link SovereignError} - If the schema is invalid.\n * @param schemaObject - The rollup schema.\n * @returns The serializer.\n */\nexport function createSerializer(schemaObject: RollupSchema): RollupSerializer {\n const schema = loadSchema(schemaObject);\n\n return {\n serialize(input: unknown, index: number): Uint8Array {\n try {\n return schema.jsonToBorsh(\n index,\n JSON.stringify(input, (_, value) => {\n if (value instanceof Uint8Array) {\n return Array.from(value);\n }\n return value;\n }),\n );\n } catch (err) {\n throw new SovereignError(`Failed to serialize input due to: ${err}`);\n }\n },\n serializeRuntimeCall(input: unknown): Uint8Array {\n return this.serialize(\n input,\n schema.knownTypeIndex(KnownTypeId.RuntimeCall),\n );\n },\n serializeUnsignedTx(input: unknown): Uint8Array {\n return this.serialize(\n input,\n schema.knownTypeIndex(KnownTypeId.UnsignedTransaction),\n );\n },\n serializeTx(input: unknown): Uint8Array {\n return this.serialize(\n input,\n schema.knownTypeIndex(KnownTypeId.Transaction),\n );\n },\n };\n}\n","/**\n * Base error class for Sovereign SDK Web3.\n */\nexport class SovereignError extends Error {\n constructor(message: string) {\n super(message);\n\n this.name = this.constructor.name;\n this.stack = new Error().stack;\n }\n}\n","export function hexToBytes(hex: string) {\n if (hex.length && hex.length % 2 !== 0) {\n throw new Error(`Invalid hex string length: ${hex.length}`);\n }\n\n const matches = hex.match(/[\\da-f]{2}/gi);\n\n if (!matches) {\n throw new Error(\"Invalid hex string: No valid hex digits found\");\n }\n\n return new Uint8Array(matches.map((h) => Number.parseInt(h, 16)));\n}\n\nexport function bytesToHex(arr: Uint8Array): string {\n let hex = \"\";\n for (let i = 0; i < arr.length; i++) {\n let hexValue = arr[i].toString(16);\n if (hexValue.length === 1) {\n hexValue = `0${hexValue}`;\n }\n hex += hexValue;\n }\n return hex;\n}\n","export * from \"./rollup\";\nexport * from \"./serialization\";\nexport * from \"./errors\";\nexport * from \"./type-spec\";\nexport * as SovereignClient from \"@sovereign-sdk/client\";\nexport { Schema } from \"@sovereign-sdk/universal-wallet-wasm\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,OAAO,qBAAqB;AAE5B,SAAS,cAAc;;;ACFvB,SAAS,aAAa,cAAc;;;ACG7B,IAAM,iBAAN,cAA6B,MAAM;AAAA,EACxC,YAAY,SAAiB;AAC3B,UAAM,OAAO;AAEb,SAAK,OAAO,KAAK,YAAY;AAC7B,SAAK,QAAQ,IAAI,MAAM,EAAE;AAAA,EAC3B;AACF;;;ADoDA,SAAS,WAAW,cAAoC;AACtD,MAAI;AACF,WAAO,OAAO,SAAS,KAAK,UAAU,YAAY,CAAC;AAAA,EACrD,SAAS,KAAK;AACZ,UAAM,IAAI,eAAe,2CAA2C,GAAG,EAAE;AAAA,EAC3E;AACF;AASO,SAAS,iBAAiB,cAA8C;AAC7E,QAAM,SAAS,WAAW,YAAY;AAEtC,SAAO;AAAA,IACL,UAAU,OAAgB,OAA2B;AACnD,UAAI;AACF,eAAO,OAAO;AAAA,UACZ;AAAA,UACA,KAAK,UAAU,OAAO,CAAC,GAAG,UAAU;AAClC,gBAAI,iBAAiB,YAAY;AAC/B,qBAAO,MAAM,KAAK,KAAK;AAAA,YACzB;AACA,mBAAO;AAAA,UACT,CAAC;AAAA,QACH;AAAA,MACF,SAAS,KAAK;AACZ,cAAM,IA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gB,WAAW,SAAS;AAAA,QACtC;AAAA,MACF,CAAC;AAGD,aAAO,SAAS;AAAA,IAClB;AAAA;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,SAAiC;AACnC,WAAO,KAAK,QAAQ;AAAA,EACtB;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,YAAuC;AACzC,WAAO,KAAK,QAAQ;AAAA,EACtB;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,SAAiC;AACnC,WAAO,KAAK,QAAQ;AAAA,EACtB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,IAAI,OAAwB;AAC1B,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,IAAI,aAA+B;AACjC,WAAO,KAAK;AAAA,EACd;AACF;;;AItPA,YAAYA,sBAAqB;AACjC,SAAS,UAAAC,eAAc;","names":["SovereignClient","Schema"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sovereign-sdk/web3",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.13",
|
|
4
4
|
"files": [
|
|
5
5
|
"dist",
|
|
6
6
|
"package.json"
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"@sovereign-sdk/client": "0.1.0-alpha.13",
|
|
28
28
|
"js-base64": "^3.7.7",
|
|
29
29
|
"@sovereign-sdk/signers": "^0.0.2",
|
|
30
|
-
"@sovereign-sdk/universal-wallet-wasm": "^0.1.
|
|
30
|
+
"@sovereign-sdk/universal-wallet-wasm": "^0.1.4"
|
|
31
31
|
},
|
|
32
32
|
"publishConfig": {
|
|
33
33
|
"access": "public"
|