postchain-client 1.7.0 → 1.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +78 -36
- package/built/cjs/index.js +938 -907
- package/built/cjs/index.js.map +1 -1
- package/built/esm/index.js +4025 -1727
- package/built/esm/index.js.map +1 -1
- package/built/index.d.ts +1 -1
- package/built/index.js.map +1 -1
- package/built/src/blockchainClient/blockchainClient.js +12 -23
- package/built/src/blockchainClient/blockchainClient.js.map +1 -1
- package/built/src/blockchainClient/errors.d.ts +1 -1
- package/built/src/blockchainClient/errors.js +1 -1
- package/built/src/blockchainClient/errors.js.map +1 -1
- package/built/src/blockchainClient/interface.d.ts +7 -7
- package/built/src/blockchainClient/types.d.ts +3 -4
- package/built/src/blockchainClient/types.js.map +1 -1
- package/built/src/blockchainClient/utils.d.ts +6 -3
- package/built/src/blockchainClient/utils.js +65 -31
- package/built/src/blockchainClient/utils.js.map +1 -1
- package/built/src/formatter.d.ts +3 -2
- package/built/src/formatter.js +1 -1
- package/built/src/formatter.js.map +1 -1
- package/built/src/gtv/types.d.ts +1 -1
- package/built/src/gtx/gtxclient.js.map +1 -1
- package/built/src/gtx/interfaces.d.ts +3 -3
- package/built/src/restclient/errors.d.ts +6 -6
- package/built/src/restclient/errors.js +2 -2
- package/built/src/restclient/httpUtil.js +8 -1
- package/built/src/restclient/httpUtil.js.map +1 -1
- package/built/src/restclient/interfaces.d.ts +4 -3
- package/built/src/restclient/restclient.js +4 -4
- package/built/src/restclient/restclient.js.map +1 -1
- package/built/src/restclient/restclientutil.d.ts +2 -2
- package/built/src/restclient/restclientutil.js +22 -20
- package/built/src/restclient/restclientutil.js.map +1 -1
- package/built/src/restclient/types.d.ts +0 -3
- package/built/src/restclient/types.js.map +1 -1
- package/built/umd/index.js +660 -629
- package/built/umd/index.js.map +1 -1
- package/changelog.md +86 -34
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -13,7 +13,11 @@ Firstly, import the required libraries.
|
|
|
13
13
|
```typescript
|
|
14
14
|
import crypto from "crypto-browserify";
|
|
15
15
|
import secp256k1 from "secp256k1";
|
|
16
|
-
import {
|
|
16
|
+
import {
|
|
17
|
+
encryption,
|
|
18
|
+
createClient,
|
|
19
|
+
newSignatureProvider,
|
|
20
|
+
} from "postchain-client";
|
|
17
21
|
```
|
|
18
22
|
|
|
19
23
|
Then, create some dummy keys.
|
|
@@ -41,7 +45,7 @@ const chromiaClient = await createClient({
|
|
|
41
45
|
});
|
|
42
46
|
```
|
|
43
47
|
|
|
44
|
-
Connecting to a network is achieved through the Directory System Chain. The `
|
|
48
|
+
Connecting to a network is achieved through the Directory System Chain. The `directoryNodeUrlPool` property accepts an array of URLs to system nodes running the directory chain. This directory chain is automatically queried to determine the URLs of the nodes in the network running the target dapp.
|
|
45
49
|
|
|
46
50
|
```typescript
|
|
47
51
|
const chromiaClient = await createClient({
|
|
@@ -49,11 +53,15 @@ const chromiaClient = await createClient({
|
|
|
49
53
|
blockchainRid,
|
|
50
54
|
});
|
|
51
55
|
```
|
|
56
|
+
|
|
52
57
|
### Failover strategies
|
|
58
|
+
|
|
53
59
|
When initializing a client, you have the option to configure the failover strategy for the client. Additionally, you can modify certain parameters within the failover configuration, such as the number of attempts per endpoint and the interval between attempts.
|
|
54
60
|
|
|
55
61
|
The Postchain client offers three failover strategies:
|
|
62
|
+
|
|
56
63
|
#### Abort On Error
|
|
64
|
+
|
|
57
65
|
The request strategy will abort on client error and retry
|
|
58
66
|
on server error. This means that if a client error occurs, such as an
|
|
59
67
|
invalid query parameter, the request strategy will not retry the query.
|
|
@@ -61,12 +69,14 @@ However, if a server error occurs, such as a timeout or internal server
|
|
|
61
69
|
error, the request strategy will retry the query on another node.
|
|
62
70
|
|
|
63
71
|
#### Try Next On Error
|
|
72
|
+
|
|
64
73
|
The Try Next On Error request strategy is similar to Abort On Error, but
|
|
65
74
|
will also retry on client error. This means that if a client error
|
|
66
75
|
occurs, the request strategy will retry the query on another node, as
|
|
67
76
|
well as retrying on server error.
|
|
68
77
|
|
|
69
78
|
#### Single Endpoint
|
|
79
|
+
|
|
70
80
|
The Single Endpoint request strategy will not retry on another node.
|
|
71
81
|
|
|
72
82
|
## Queries
|
|
@@ -110,7 +120,7 @@ type ReturnType = {
|
|
|
110
120
|
foobar: string;
|
|
111
121
|
};
|
|
112
122
|
|
|
113
|
-
const result = await chromiaClient.query<
|
|
123
|
+
const result = await chromiaClient.query<ReturnType, ArgumentsType>(
|
|
114
124
|
"get_foobar",
|
|
115
125
|
{
|
|
116
126
|
foo: 1,
|
|
@@ -119,6 +129,22 @@ const result = await chromiaClient.query<ArgumentsType, ReturnType>(
|
|
|
119
129
|
);
|
|
120
130
|
```
|
|
121
131
|
|
|
132
|
+
### Typed query 2
|
|
133
|
+
|
|
134
|
+
Alternatively, you can specify the types in a `QueryObject` to achieve type safety
|
|
135
|
+
|
|
136
|
+
```typescript
|
|
137
|
+
type ReturnType = {
|
|
138
|
+
foobar: string;
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
const myQuery: QueryObject<ReturnType> = {
|
|
142
|
+
name: "get_fobar",
|
|
143
|
+
args: { foo: "bar" },
|
|
144
|
+
};
|
|
145
|
+
const result = await chromiaClient.query(myQuery); // result has type ReturnType
|
|
146
|
+
```
|
|
147
|
+
|
|
122
148
|
## Transactions
|
|
123
149
|
|
|
124
150
|
To send transactions, begin by creating a simple signature provider. The signature provider is used to sign transactions. More details on usage are provided further below.
|
|
@@ -129,31 +155,35 @@ const signatureProviderA = newSignatureProvider({ privKey: signerPrivKeyA });
|
|
|
129
155
|
|
|
130
156
|
### Simple Transaction
|
|
131
157
|
|
|
132
|
-
The `signAndSendUniqueTransaction` function streamlines the process of sending a transaction in three steps. It adds a "nop" (no operation) with a random number that ensures the transaction is unique, signs it with a signature provider or private key, and sends it. The function generates a receipt that includes a status code, status, and transactionRID. The status code indicates whether the server successfully processed the transaction. The status represents the current stage of the transaction on the blockchain, which can be one of the following: `Waiting`, `Rejected`, `Confirmed`, or `Unknown`.
|
|
158
|
+
The `signAndSendUniqueTransaction` function streamlines the process of sending a transaction in three steps. It adds a "nop" (no operation) with a random number that ensures the transaction is unique, signs it with a signature provider or private key, and sends it. The function generates a receipt that includes a status code, status, and transactionRID. The status code indicates whether the server successfully processed the transaction. The status represents the current stage of the transaction on the blockchain, which can be one of the following: `Waiting`, `Rejected`, `Confirmed`, or `Unknown`.
|
|
133
159
|
|
|
134
160
|
```typescript
|
|
135
|
-
const { status, statusCode, transactionRID } =
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
161
|
+
const { status, statusCode, transactionRID } =
|
|
162
|
+
await chromiaClient.signAndSendUniqueTransaction(
|
|
163
|
+
{
|
|
164
|
+
operations: [
|
|
165
|
+
{
|
|
166
|
+
name: "my_operation",
|
|
167
|
+
args: ["arg1", "arg2"],
|
|
168
|
+
},
|
|
169
|
+
],
|
|
170
|
+
signers: [signatureProviderA.pubkey],
|
|
171
|
+
},
|
|
172
|
+
signatureProviderA
|
|
173
|
+
);
|
|
147
174
|
```
|
|
175
|
+
|
|
148
176
|
It is also possible to pass a single operation.
|
|
177
|
+
|
|
149
178
|
```typescript
|
|
150
|
-
const { status, statusCode, transactionRID } =
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
179
|
+
const { status, statusCode, transactionRID } =
|
|
180
|
+
await chromiaClient.signAndSendUniqueTransaction(
|
|
181
|
+
{
|
|
182
|
+
name: "my_operation",
|
|
183
|
+
args: ["arg1", "arg2"],
|
|
184
|
+
},
|
|
185
|
+
signatureProviderA
|
|
186
|
+
);
|
|
157
187
|
```
|
|
158
188
|
|
|
159
189
|
### Signing a Transaction
|
|
@@ -183,6 +213,7 @@ const receipt = await chromiaClient.sendTransaction({
|
|
|
183
213
|
args: ["arg1", "arg2"],
|
|
184
214
|
});
|
|
185
215
|
```
|
|
216
|
+
|
|
186
217
|
### Sending a Signed Transaction
|
|
187
218
|
|
|
188
219
|
```typescript
|
|
@@ -236,25 +267,30 @@ const signedTx = await chromiaClient.signTransaction(
|
|
|
236
267
|
|
|
237
268
|
const receipt = await chromiaClient.sendTransaction(signedTx);
|
|
238
269
|
```
|
|
270
|
+
|
|
239
271
|
## PromiEvent
|
|
272
|
+
|
|
240
273
|
When using functions that involve sending a transaction, you have the option to either wait for a promise or act on an event. The return value in this case is a "PromiEvent," which combines the functionalities of both a "Promise" and an "Event." This combination allows you to handle asynchronous operations. You can treat it as a Promise by utilizing the .then() and .catch() methods to handle the result or any potential errors. Moreover, it emits an event when a transaction is sent, providing you with the ability to listen for the event and execute custom logic based on your specific needs.
|
|
241
274
|
|
|
242
275
|
```typescript
|
|
243
|
-
chromiaClient
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
276
|
+
chromiaClient
|
|
277
|
+
.sendTransaction({
|
|
278
|
+
name: "my_operation",
|
|
279
|
+
args: ["arg1", "arg2"],
|
|
280
|
+
})
|
|
281
|
+
.on("sent", (receipt: TransactionReceipt) => {
|
|
282
|
+
console.log("The transaction is sent");
|
|
283
|
+
});
|
|
249
284
|
```
|
|
285
|
+
|
|
250
286
|
## External Signing Example
|
|
251
287
|
|
|
252
288
|
This example demonstrates that you can use external signing mechanisms. It could involve a complex function requiring you to sign from your phone, another device, or a different method.
|
|
253
289
|
|
|
254
290
|
```typescript
|
|
255
291
|
function askUserBToSign(buffer) {
|
|
256
|
-
// The signed digest is a
|
|
257
|
-
var digest = sha256(
|
|
292
|
+
// The signed digest is a sha-256
|
|
293
|
+
var digest = sha256(buffer);
|
|
258
294
|
return secp256k1.sign(digest, signerPrivKeyB).signature;
|
|
259
295
|
}
|
|
260
296
|
```
|
|
@@ -267,7 +303,9 @@ const signatureProviderB = {
|
|
|
267
303
|
sign: askUserBToSign,
|
|
268
304
|
};
|
|
269
305
|
```
|
|
306
|
+
|
|
270
307
|
## ICCF
|
|
308
|
+
|
|
271
309
|
Creates an [ICCF](https://docs.chromia.com/overview/cross-chain-communication) (Inter-Chain Communication Framework) proof transaction. This function generates a proof that a specific transaction has occurred on the source blockchain. The function returns a transaction object with an operation called iccf_proof and the operation that should be accompanied by the proof should be added to this transaction object. The transaction can then be signed and posted to the target blockchain.
|
|
272
310
|
|
|
273
311
|
```typescript
|
|
@@ -290,14 +328,17 @@ const { iccfTx, verifiedTx } = createIccfProofTx(chromiaClient, txToProveRID,txT
|
|
|
290
328
|
`iccfTx` is a transaction object with an operation called `iccf_proof` with argument containing the composed proof. To this transaction object you can now add the operation that will need the proof. Finally, the transaction object is ready to be signed and sent.
|
|
291
329
|
|
|
292
330
|
If necessary, it is possible to solely verify whether a specific transaction has been included in the anchoring blockchain:
|
|
331
|
+
|
|
293
332
|
```typescript
|
|
294
333
|
isBlockAnchored(sourceClient, anchoringClient, txRid);
|
|
295
334
|
```
|
|
296
335
|
|
|
297
336
|
To create an anchoring client there is an utility function:
|
|
337
|
+
|
|
298
338
|
```typescript
|
|
299
|
-
const anchoringClient = getAnchoringClient()
|
|
339
|
+
const anchoringClient = getAnchoringClient();
|
|
300
340
|
```
|
|
341
|
+
|
|
301
342
|
## Architecture
|
|
302
343
|
|
|
303
344
|
In the Postchain client, Generic Transactions (GTX) are used to simplify user implementations of Postchain. Users do not need to invent a binary format for their transactions. The client will serialize the function calls, sign them, and send them to Postchain. Read more about GTX in the [docs](https://docs.chromia.com/category/gtx).
|
|
@@ -333,19 +374,20 @@ Backend
|
|
|
333
374
|
## Contributing to the Project
|
|
334
375
|
|
|
335
376
|
### Run tests
|
|
377
|
+
|
|
336
378
|
Unit tests:
|
|
337
379
|
|
|
338
|
-
|
|
380
|
+
`npm run test:unit`
|
|
339
381
|
|
|
340
382
|
Integration tests:
|
|
341
383
|
|
|
342
384
|
1. Make sure a postgres database is running. Read more [here](https://docs.chromia.com/getting-started/dev-setup/database-setup).
|
|
343
385
|
2. Start blockchain
|
|
344
386
|
|
|
345
|
-
|
|
387
|
+
`cd resources`
|
|
346
388
|
|
|
347
|
-
|
|
389
|
+
`chr node start --wipe`
|
|
348
390
|
|
|
349
391
|
3. Run tests
|
|
350
392
|
|
|
351
|
-
|
|
393
|
+
`npm run test:integration`
|