postchain-client 0.10.5 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +103 -69
- package/built/index.d.ts +10 -0
- package/built/index.js +3 -0
- package/built/index.js.LICENSE.txt +649 -0
- package/built/src/chromia/chromiaClientProvider.d.ts +3 -0
- package/built/src/chromia/errors.d.ts +3 -0
- package/built/src/chromia/interfaces.d.ts +4 -0
- package/built/src/encryption/encryption.d.ts +54 -0
- package/built/src/encryption/errors.d.ts +7 -0
- package/built/src/encryption/types.d.ts +5 -0
- package/built/src/formatter.d.ts +19 -0
- package/built/src/gtv/definition.d.ts +3 -0
- package/built/src/gtv/index.d.ts +6 -0
- package/built/src/gtv/types.d.ts +6 -0
- package/built/src/gtx/errors.d.ts +15 -0
- package/built/src/gtx/gtx.d.ts +34 -0
- package/built/src/gtx/gtxclient.d.ts +3 -0
- package/built/src/gtx/interfaces.d.ts +21 -0
- package/built/src/gtx/serialization.d.ts +6 -0
- package/built/src/gtx/types.d.ts +28 -0
- package/built/src/logger.d.ts +6 -0
- package/built/src/merkle/merkleHelper.d.ts +17 -0
- package/built/src/merkle/types.d.ts +11 -0
- package/built/src/restclient/errors.d.ts +32 -0
- package/built/src/restclient/interfaces.d.ts +13 -0
- package/built/src/restclient/restclient.d.ts +3 -0
- package/built/src/restclient/restclientutil.d.ts +3 -0
- package/built/src/restclient/types.d.ts +66 -0
- package/package.json +36 -8
- package/dist/postchain-client.js +0 -2
- package/dist/postchain-client.js.LICENSE.txt +0 -17
- package/index.js +0 -9
- package/postchain-client.js +0 -55336
- package/src/gtv/index.js +0 -10
- package/src/gtv/merkle/binarytree.js +0 -160
- package/src/gtv/merkle/binarytreefactory.js +0 -256
- package/src/gtv/merkle/merklehashcalculator.js +0 -77
- package/src/gtv/merkle/path.js +0 -322
- package/src/gtv/merkle/proof/merklehashcarrier.js +0 -25
- package/src/gtv/merkle/proof/merklehashsummaryfactory.js +0 -84
- package/src/gtv/merkle/proof/merkleproof.js +0 -65
- package/src/gtv/merkle/proof/merkleprooftree.js +0 -125
- package/src/gtv/merkle/proof/merkleprooftreefactory.js +0 -113
- package/src/gtv/serialization.js +0 -113
- package/src/gtx/gtx.js +0 -135
- package/src/gtx/gtxclient.js +0 -117
- package/src/restclient.js +0 -278
- package/src/util.js +0 -298
- package/test/gtv/merkle/merklerootcalculatortest.js +0 -24
- package/test/gtv/merkle/treehelper.js +0 -24
- package/test/gtv/merkle/treeprinter.js +0 -349
- package/test/gtv/stringbuilder.js +0 -25
- package/test/gtx/checkGTXSignaturetest.js +0 -81
- package/test/gtx/gtxclienttest.js +0 -104
- package/test/gtx/serializationtest.js +0 -133
- package/test/merkle/hash/arraytomerkleroottest.js +0 -47
- package/test/merkle/hash/dicttomerkleroottest.js +0 -33
- package/test/merkle/hash/mixtomerkleroottest.js +0 -17
- package/test/merkle/merklehashcalculatordummy.js +0 -62
- package/test/merkle/pathtest.js +0 -101
- package/test/merkle/proof/arraytomerkletreeprooftest.js +0 -270
- package/test/merkle/proof/dicttomerkletreeprooftest.js +0 -134
- package/test/merkle/proof/mixtomerkletreeprooftest.js +0 -74
- package/test/merkle/tree/arraytobinarytest.js +0 -354
- package/test/merkle/tree/dicttobinarytest.js +0 -175
- package/test/merkle/tree/mixarraydictbinarytest.js +0 -71
- package/test/restclientintegrationtestmanual.js +0 -171
- package/test/restclienttest.js +0 -210
- package/test/signatures.js +0 -31
- package/test/utiltest.js +0 -159
- package/webpack.config.js +0 -10
package/README.md
CHANGED
|
@@ -3,44 +3,46 @@
|
|
|
3
3
|
## Example:
|
|
4
4
|
|
|
5
5
|
```javascript
|
|
6
|
-
let crypto = require(
|
|
7
|
-
let secp256k1 = require(
|
|
6
|
+
let crypto = require("crypto");
|
|
7
|
+
let secp256k1 = require("secp256k1");
|
|
8
8
|
|
|
9
9
|
// Create some dummy keys
|
|
10
|
-
let signerPrivKeyA = Buffer.alloc(32,
|
|
10
|
+
let signerPrivKeyA = Buffer.alloc(32, "a");
|
|
11
11
|
let signerPubKeyA = secp256k1.publicKeyCreate(signerPrivKeyA);
|
|
12
|
-
let signerPrivKeyB = Buffer.alloc(32,
|
|
12
|
+
let signerPrivKeyB = Buffer.alloc(32, "b");
|
|
13
13
|
let signerPubKeyB = secp256k1.publicKeyCreate(signerPrivKeyB);
|
|
14
14
|
|
|
15
15
|
// The lower-level client that can be used for any
|
|
16
16
|
// postchain client messages. It only handles binary data.
|
|
17
|
-
let restClient = require(
|
|
17
|
+
let restClient = require("postchain-client").restClient;
|
|
18
18
|
|
|
19
19
|
// The higher-level client that is used for generic transactions, GTX.
|
|
20
20
|
// This utilizes the GTX format described below to pass function calls
|
|
21
21
|
// from the client to the postchain backend implementation.
|
|
22
|
-
let gtxClient = require(
|
|
23
|
-
|
|
24
|
-
// Create an instance of the rest client and configure it for a specific
|
|
25
|
-
// base url. You may set an optional pool size for the connection pool.
|
|
26
|
-
// Default pool size is 10. Applications that do hundreds of requests
|
|
27
|
-
// per second may consider setting this a bit higher, for example 100.
|
|
28
|
-
// It *may* speed things up a bit.
|
|
29
|
-
let rest = restClient.createRestClient(`http://localhost:7741`, 5);
|
|
22
|
+
let gtxClient = require("postchain-client").gtxClient;
|
|
30
23
|
|
|
31
24
|
// Each blockchain has a blockchainRID, that identifies the blockchain
|
|
32
25
|
// we want to work with. This blockchainRID must match the blockchain RID
|
|
33
|
-
// encoded into the first block of the blockchain. How the blockchainRID
|
|
34
|
-
// is constructed is up to the creator of the blockchain. In this example
|
|
26
|
+
// encoded into the first block of the blockchain. How the blockchainRID
|
|
27
|
+
// is constructed is up to the creator of the blockchain. In this example
|
|
35
28
|
// we use the linux command:
|
|
36
29
|
// echo "A blockchain example"| sha256sum
|
|
37
|
-
let blockchainRID =
|
|
30
|
+
let blockchainRID = "7d565d92fd15bd1cdac2dc276cbcbc5581349d05a9e94ba919e1155ef4daf8f9";
|
|
31
|
+
|
|
32
|
+
// Create an instance of the rest client and configure it for a specific set of
|
|
33
|
+
// base urls and a blockchinRID. You may set an optional pool size for the connection pool,
|
|
34
|
+
// default pool size is 10. Applications that do hundreds of requests
|
|
35
|
+
// per second may consider setting this a bit higher, for example 100.
|
|
36
|
+
// It *may* speed things up a bit. You may also set an opitional
|
|
37
|
+
// poolinginterval in milliseconds, default is set to 500, a fail over
|
|
38
|
+
// config wich include attemps per endpoint and attempt interval, default is 3 and 500.
|
|
39
|
+
let rest = restClient.createRestClient([`http://localhost:7741`], blockchainRID, 5, 1000);
|
|
38
40
|
|
|
39
41
|
// Create an instance of the higher-level gtx client. It will
|
|
40
42
|
// use the rest client instance and it will allow calls to functions
|
|
41
43
|
// fun1 and fun2. The backend implementation in Postchain must
|
|
42
44
|
// provide those functions.
|
|
43
|
-
let gtx = gtxClient.createClient(rest, blockchainRID, [
|
|
45
|
+
let gtx = gtxClient.createClient(rest, blockchainRID, ["fun1", "fun2"]);
|
|
44
46
|
|
|
45
47
|
// Start a new request. A request instance is created.
|
|
46
48
|
// The public keys are the keys that must sign the request
|
|
@@ -48,9 +50,9 @@ let gtx = gtxClient.createClient(rest, blockchainRID, ['fun1', 'fun2']);
|
|
|
48
50
|
let req = gtx.newTransaction([signerPubKeyA, signerPubKeyB]);
|
|
49
51
|
|
|
50
52
|
// call fun1 with three arguments: a string, an array and a Buffer
|
|
51
|
-
req.fun1(
|
|
53
|
+
req.fun1("arg1", ["arg2", [1, 2]], Buffer.from("hello"));
|
|
52
54
|
// call the same function with only one argument
|
|
53
|
-
req.fun1(
|
|
55
|
+
req.fun1("arg1");
|
|
54
56
|
// call fun2
|
|
55
57
|
req.fun2(1, 2);
|
|
56
58
|
|
|
@@ -69,51 +71,51 @@ req.addSignature(signerPubKeyB, signatureFromB);
|
|
|
69
71
|
|
|
70
72
|
// Finally send the request and supply an error callback
|
|
71
73
|
req.send((error) => {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
74
|
+
if (error) {
|
|
75
|
+
console.log(error);
|
|
76
|
+
}
|
|
75
77
|
});
|
|
76
78
|
|
|
77
79
|
// Now we can query Postchain. The backend must have a method
|
|
78
80
|
// query method named "findStuff" (readOnlyConn, queryObject) that can
|
|
79
|
-
// understand the query object and typically perform a search using
|
|
80
|
-
// the database connection readOnlyConn. The backend query function
|
|
81
|
+
// understand the query object and typically perform a search using
|
|
82
|
+
// the database connection readOnlyConn. The backend query function
|
|
81
83
|
// can return any serializable result object you chose
|
|
82
|
-
let queryObject = {type: "findStuff", text:
|
|
84
|
+
let queryObject = { type: "findStuff", text: "arg1" };
|
|
83
85
|
let resultHandler = (error, result) => {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}
|
|
86
|
+
if (error) {
|
|
87
|
+
console.error(error);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
if (result.hits == 0) {
|
|
91
|
+
// Poll every 2 seconds
|
|
92
|
+
setTimeout(gtx.query(queryObject, resultHandler), 2000);
|
|
93
|
+
}
|
|
94
|
+
console.log(JSON.stringify(result));
|
|
95
|
+
};
|
|
94
96
|
gtx.query(queryObject, resultHandler);
|
|
95
97
|
|
|
96
98
|
// This will make a request with a single operation
|
|
97
99
|
// and a single signature.
|
|
98
100
|
req = gtx.newTransaction(blockchainRID, [signerPubKeyA]);
|
|
99
|
-
req.fun1(
|
|
101
|
+
req.fun1("arg1");
|
|
100
102
|
req.sign(signerPrivKeyA);
|
|
101
103
|
req.send((error) => {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
104
|
+
if (!error) {
|
|
105
|
+
done();
|
|
106
|
+
}
|
|
105
107
|
});
|
|
106
108
|
|
|
107
109
|
function sha256(buffer) {
|
|
108
|
-
|
|
110
|
+
return crypto.createHash("sha256").update(buffer).digest();
|
|
109
111
|
}
|
|
110
112
|
|
|
111
113
|
// This is to demonstrate that you can use external signing
|
|
112
114
|
// mechanisms.
|
|
113
115
|
function askUserBToSign(buffer) {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
116
|
+
// The signed digest is a double sha-256
|
|
117
|
+
var digest = sha256(sha256(buffer));
|
|
118
|
+
return secp256k1.sign(digest, signerPrivKeyB).signature;
|
|
117
119
|
}
|
|
118
120
|
```
|
|
119
121
|
|
|
@@ -121,40 +123,49 @@ A very simple backend for the above client might look like this:
|
|
|
121
123
|
|
|
122
124
|
```javascript
|
|
123
125
|
module.exports.createSchema = async function (conn) {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
126
|
+
console.log("Creating schema in backend");
|
|
127
|
+
await conn.query(
|
|
128
|
+
"CREATE TABLE IF NOT EXISTS example " +
|
|
129
|
+
"(id SERIAL PRIMARY KEY, stuff TEXT NOT NULL)"
|
|
130
|
+
);
|
|
131
|
+
};
|
|
128
132
|
|
|
129
133
|
// Example backend implementation that doesn't do anything but log the function calls
|
|
130
134
|
module.exports.backendFunctions = {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
135
|
+
fun1: async function (
|
|
136
|
+
conn,
|
|
137
|
+
tx_iid,
|
|
138
|
+
call_index,
|
|
139
|
+
signers,
|
|
140
|
+
stringArg,
|
|
141
|
+
arrayArg,
|
|
142
|
+
bufferArg
|
|
143
|
+
) {
|
|
144
|
+
console.log("fun1 called in backend");
|
|
145
|
+
},
|
|
146
|
+
fun2: async function (conn, tx_iid, call_index, signers, intArg1, intArg2) {
|
|
147
|
+
console.log("fun2 called in backend");
|
|
148
|
+
},
|
|
149
|
+
};
|
|
139
150
|
|
|
140
151
|
module.exports.backendQueries = {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
}
|
|
146
|
-
return {hits: 0};
|
|
152
|
+
findStuff: async function (readOnlyConn, queryObject) {
|
|
153
|
+
console.log("Search for " + queryObject.text);
|
|
154
|
+
if (queryObject.text === "giveMeHits") {
|
|
155
|
+
return { hits: 4 };
|
|
147
156
|
}
|
|
148
|
-
}
|
|
157
|
+
return { hits: 0 };
|
|
158
|
+
},
|
|
159
|
+
};
|
|
149
160
|
```
|
|
150
161
|
|
|
151
162
|
## GTX architecture
|
|
152
163
|
|
|
153
|
-
Generic transactions were developed to make it easier to make user implementations of Postchain.
|
|
164
|
+
Generic transactions were developed to make it easier to make user implementations of Postchain.
|
|
154
165
|
The user doesn't have to invent a binary format for it's transactions. With GTX you specify a
|
|
155
166
|
set of functions that you will call from the client, and the GTX client will serialize the
|
|
156
167
|
function calls, sign them and send to Postchain.
|
|
157
|
-
|
|
168
|
+
|
|
158
169
|
```
|
|
159
170
|
User
|
|
160
171
|
|
|
|
@@ -180,10 +191,33 @@ Postchain
|
|
|
180
191
|
v
|
|
181
192
|
Backend
|
|
182
193
|
```
|
|
194
|
+
|
|
183
195
|
The first four arguments to backend.fun1 are
|
|
184
196
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
the GTX framework when the backend function is called.
|
|
197
|
+
- `conn` is a database connection that the backend function can use to query/update the database
|
|
198
|
+
- `tx_iid` is the primary key of this postchain transaction.
|
|
199
|
+
- `call_index`, 0 in this example. It's the index within the GTX of the current call
|
|
200
|
+
- `signers`, all signers of this GTX. The signatures from these signers are already verified by
|
|
201
|
+
the GTX framework when the backend function is called.
|
|
202
|
+
|
|
203
|
+
## Release notes
|
|
204
|
+
### 1.0.0
|
|
205
|
+
* New logger accessible in index.ts.
|
|
206
|
+
* Enable the client to connect to a blockchain through multiple nodes running the blockchain.
|
|
207
|
+
* Load balancing by randomly distributing transactions and queries between nodes.
|
|
208
|
+
* Retry policy added for HTTP request to the blockchain.
|
|
209
|
+
* Enables you to discover the nodes running your blockchain by querying D1 with your dapp´s blockchain RID. Read more in the Chromia client providers [Readme](./src/chromia/README.md).
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
Breaking changes:
|
|
213
|
+
* Previously a rest client was initialized with one string containing the address of the node running your blockchain. Now an instance of the rest client is initiated with a list of strings representing the addresses of the nodes where your dapp is running.
|
|
214
|
+
* Previously a rest client query was called with two parameters; queryName and queryObject. Now this call only takes one parameter called queryObject, which is defined as:
|
|
215
|
+
{ type: string;
|
|
216
|
+
[arg: string]: RawGtv;
|
|
217
|
+
}
|
|
218
|
+
where type is what previously was called query name.
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
### 0.\*.*
|
|
223
|
+
Early version of the postchain-client written in javascript.
|
package/built/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * as gtv from "./src/gtv/index";
|
|
2
|
+
export * as formatter from "./src/formatter";
|
|
3
|
+
export * as gtx from "./src/gtx/gtx";
|
|
4
|
+
export * as gtxClient from "./src/gtx/gtxclient";
|
|
5
|
+
export * as restClient from "./src/restclient/restclient";
|
|
6
|
+
export * as logger from "./src/logger";
|
|
7
|
+
export * as merkle from "./src/merkle/merkleHelper";
|
|
8
|
+
export * as encryption from "./src/encryption/encryption";
|
|
9
|
+
export * as restClientutil from "./src/restclient/restclientutil";
|
|
10
|
+
export * as chromiaClientProvider from "./src/chromia/chromiaClientProvider";
|