weavedb-node-client 0.4.3

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.
Files changed (3) hide show
  1. package/index.js +70 -0
  2. package/package.json +15 -0
  3. package/weavedb.proto +18 -0
package/index.js ADDED
@@ -0,0 +1,70 @@
1
+ const { all, complement, init, is, last, isNil } = require("ramda")
2
+ const Base = require("weavedb-base")
3
+ const PROTO_PATH = __dirname + "/weavedb.proto"
4
+ const grpc = require("@grpc/grpc-js")
5
+ const protoLoader = require("@grpc/proto-loader")
6
+ const packageDefinition = protoLoader.loadSync(PROTO_PATH, {
7
+ keepCase: true,
8
+ longs: String,
9
+ enums: String,
10
+ defaults: true,
11
+ oneofs: true,
12
+ })
13
+ const weavedb_proto = grpc.loadPackageDefinition(packageDefinition).weavedb
14
+
15
+ class SDK extends Base {
16
+ constructor({ rpc, contractTxId, wallet, name, version, EthWallet, web3 }) {
17
+ super()
18
+ this.client = new weavedb_proto.DB(rpc, grpc.credentials.createInsecure())
19
+ if (typeof window === "object") {
20
+ require("@metamask/legacy-web3")
21
+ this.web3 = window.web3
22
+ }
23
+ if (all(complement(isNil))([contractTxId, name, version])) {
24
+ this.initialize({ contractTxId, name, version, EthWallet })
25
+ }
26
+ }
27
+
28
+ initialize({ contractTxId, name, version, EthWallet }) {
29
+ this.domain = { name, version, verifyingContract: contractTxId }
30
+ if (!isNil(EthWallet)) this.setEthWallet(EthWallet)
31
+ }
32
+
33
+ async _request(func, query) {
34
+ const request = {
35
+ method: func,
36
+ query: JSON.stringify(query),
37
+ }
38
+ const _query = () =>
39
+ new Promise(ret => {
40
+ this.client.query(request, (err, response) => {
41
+ if (!isNil(err)) {
42
+ ret({ result: null, err })
43
+ } else {
44
+ if (response.err === "") {
45
+ const result = JSON.parse(response.result)
46
+ ret({ result, err: null })
47
+ } else {
48
+ ret({ result: null, err: response.err })
49
+ }
50
+ }
51
+ })
52
+ })
53
+ let q = await _query()
54
+ return q.result
55
+ }
56
+
57
+ async request(func, ...query) {
58
+ return await this._request(func, query)
59
+ }
60
+
61
+ async getNonce(addr) {
62
+ return this.request("getNonce", addr, true)
63
+ }
64
+
65
+ async getIds(tx) {
66
+ return this.request("getIds", tx)
67
+ }
68
+ }
69
+
70
+ module.exports = SDK
package/package.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "weavedb-node-client",
3
+ "version": "0.4.3",
4
+ "main": "index.js",
5
+ "license": "MIT",
6
+ "engines": {
7
+ "node": "16"
8
+ },
9
+ "dependencies": {
10
+ "@grpc/grpc-js": "^1.1.0",
11
+ "@grpc/proto-loader": "^0.5.0",
12
+ "ramda": "^0.28.0",
13
+ "weavedb-base": "^0.4.3"
14
+ }
15
+ }
package/weavedb.proto ADDED
@@ -0,0 +1,18 @@
1
+ syntax = "proto3";
2
+
3
+ package weavedb;
4
+
5
+ service DB {
6
+ rpc query (WeaveDBRequest) returns (WeaveDBReply) {}
7
+ }
8
+
9
+ message WeaveDBRequest {
10
+ string method = 1;
11
+ string query = 2;
12
+ bool nocache = 3;
13
+ }
14
+
15
+ message WeaveDBReply {
16
+ string result = 1;
17
+ string err = 2;
18
+ }