dkg.js 6.0.0 → 6.0.2
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/.eslintrc.js +2 -0
- package/README.md +1 -3
- package/constants.js +74 -9
- package/dist/dkg.min.js +1 -1
- package/dist/dkg.min.js.LICENSE.txt +93 -1111
- package/{demo.js → examples/demo-private.js} +24 -51
- package/{demo.html → examples/demo.html} +2 -4
- package/examples/demo.js +184 -0
- package/{socket-front-demo.html → examples/socket-front-demo.html} +1 -1
- package/{sockets-demo.js → examples/sockets-demo.js} +2 -2
- package/managers/asset-operations-manager.js +832 -150
- package/managers/graph-operations-manager.js +55 -6
- package/managers/node-operations-manager.js +16 -2
- package/package.json +11 -13
- package/services/base-service-manager.js +3 -0
- package/services/blockchain-service/blockchain-service-base.js +262 -91
- package/services/blockchain-service/implementations/browser-blockchain-service.js +5 -21
- package/services/blockchain-service/implementations/node-blockchain-service.js +6 -21
- package/services/input-service.js +167 -0
- package/services/node-api-service/implementations/http-service.js +93 -74
- package/services/utilities.js +62 -24
- package/services/validation-service.js +289 -52
- package/webpack.config.js +8 -2
|
@@ -1,25 +1,13 @@
|
|
|
1
1
|
const Web3 = require('web3');
|
|
2
2
|
const BlockchainServiceBase = require('../blockchain-service-base.js');
|
|
3
|
-
const { WEBSOCKET_PROVIDER_OPTIONS
|
|
3
|
+
const { WEBSOCKET_PROVIDER_OPTIONS } = require('../../../constants.js');
|
|
4
4
|
|
|
5
5
|
class BrowserBlockchainService extends BlockchainServiceBase {
|
|
6
|
-
constructor(config) {
|
|
6
|
+
constructor(config = {}) {
|
|
7
7
|
super(config);
|
|
8
8
|
this.config = config;
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
getBlockchain(options) {
|
|
12
|
-
return {
|
|
13
|
-
name: options.blockchain.name,
|
|
14
|
-
hubContract:
|
|
15
|
-
options.blockchain.hubContract ?? BLOCKCHAINS[options.blockchain.name].hubContract,
|
|
16
|
-
assetContract:
|
|
17
|
-
options.blockchain.assetContract ??
|
|
18
|
-
BLOCKCHAINS[options.blockchain.name].assetContract,
|
|
19
|
-
rpc: options.blockchain.rpc ?? BLOCKCHAINS[options.blockchain.name].rpc,
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
|
|
23
11
|
initializeWeb3(blockchainName, blockchainRpc) {
|
|
24
12
|
if (typeof window.Web3 === 'undefined' || !window.Web3) {
|
|
25
13
|
this.logger.error(
|
|
@@ -41,11 +29,7 @@ class BrowserBlockchainService extends BlockchainServiceBase {
|
|
|
41
29
|
}
|
|
42
30
|
|
|
43
31
|
async executeContractFunction(contractName, functionName, args, blockchain) {
|
|
44
|
-
const contractInstance = await this.getContractInstance(
|
|
45
|
-
blockchain.name,
|
|
46
|
-
contractName,
|
|
47
|
-
blockchain.rpc,
|
|
48
|
-
);
|
|
32
|
+
const contractInstance = await this.getContractInstance(contractName, blockchain);
|
|
49
33
|
const tx = await this.prepareTransaction(contractInstance, functionName, args, {
|
|
50
34
|
name: blockchain.name,
|
|
51
35
|
publicKey: await this.getAccount(),
|
|
@@ -74,12 +58,12 @@ class BrowserBlockchainService extends BlockchainServiceBase {
|
|
|
74
58
|
return receipt.events[eventName].returnValues;
|
|
75
59
|
}
|
|
76
60
|
|
|
77
|
-
async transferAsset(tokenId, to,
|
|
61
|
+
async transferAsset(tokenId, to, blockchain) {
|
|
78
62
|
return this.executeContractFunction(
|
|
79
63
|
'ContentAssetStorage',
|
|
80
64
|
'transferFrom',
|
|
81
65
|
[await this.getAccount(), to, tokenId],
|
|
82
|
-
|
|
66
|
+
blockchain,
|
|
83
67
|
);
|
|
84
68
|
}
|
|
85
69
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
const Web3 = require('web3');
|
|
2
|
-
const {
|
|
2
|
+
const { WEBSOCKET_PROVIDER_OPTIONS } = require('../../../constants.js');
|
|
3
3
|
const BlockchainServiceBase = require('../blockchain-service-base.js');
|
|
4
4
|
|
|
5
5
|
class NodeBlockchainService extends BlockchainServiceBase {
|
|
6
|
-
constructor(config) {
|
|
6
|
+
constructor(config = {}) {
|
|
7
7
|
super(config);
|
|
8
8
|
this.config = config;
|
|
9
9
|
this.events = {};
|
|
@@ -31,23 +31,10 @@ class NodeBlockchainService extends BlockchainServiceBase {
|
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
getBlockchain(options) {
|
|
35
|
-
return {
|
|
36
|
-
name: options.blockchain.name,
|
|
37
|
-
rpc: options.blockchain.rpc ?? BLOCKCHAINS[options.blockchain.name].rpc,
|
|
38
|
-
publicKey: this.config.blockchain?.publicKey ?? options.blockchain.publicKey,
|
|
39
|
-
privateKey: this.config.blockchain?.privateKey ?? options.blockchain.privateKey,
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
|
|
43
34
|
async executeContractFunction(contractName, functionName, args, blockchain) {
|
|
44
|
-
const web3Instance = await this.getWeb3Instance(blockchain
|
|
35
|
+
const web3Instance = await this.getWeb3Instance(blockchain);
|
|
45
36
|
|
|
46
|
-
const contractInstance = await this.getContractInstance(
|
|
47
|
-
blockchain.name,
|
|
48
|
-
contractName,
|
|
49
|
-
blockchain.rpc,
|
|
50
|
-
);
|
|
37
|
+
const contractInstance = await this.getContractInstance(contractName, blockchain);
|
|
51
38
|
const tx = await this.prepareTransaction(contractInstance, functionName, args, blockchain);
|
|
52
39
|
const createdTransaction = await web3Instance.eth.accounts.signTransaction(
|
|
53
40
|
tx,
|
|
@@ -58,7 +45,7 @@ class NodeBlockchainService extends BlockchainServiceBase {
|
|
|
58
45
|
}
|
|
59
46
|
|
|
60
47
|
async decodeEventLogs(receipt, eventName, blockchain) {
|
|
61
|
-
const web3Instance = await this.getWeb3Instance(blockchain
|
|
48
|
+
const web3Instance = await this.getWeb3Instance(blockchain);
|
|
62
49
|
let result;
|
|
63
50
|
const { hash, inputs } = this.events[eventName];
|
|
64
51
|
receipt.logs.forEach((row) => {
|
|
@@ -68,9 +55,7 @@ class NodeBlockchainService extends BlockchainServiceBase {
|
|
|
68
55
|
return result;
|
|
69
56
|
}
|
|
70
57
|
|
|
71
|
-
async transferAsset(tokenId, to,
|
|
72
|
-
const blockchain = this.getBlockchain(options);
|
|
73
|
-
|
|
58
|
+
async transferAsset(tokenId, to, blockchain) {
|
|
74
59
|
return this.executeContractFunction(
|
|
75
60
|
'ContentAssetStorage',
|
|
76
61
|
'transferFrom',
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
const { DEFAULT_PARAMETERS, BLOCKCHAINS } = require('../constants');
|
|
2
|
+
|
|
3
|
+
class InputService {
|
|
4
|
+
constructor(config = {}) {
|
|
5
|
+
this.config = config;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
getAssetCreateArguments(options) {
|
|
9
|
+
return {
|
|
10
|
+
blockchain: this.getBlockchain(options),
|
|
11
|
+
endpoint: this.getEndpoint(options),
|
|
12
|
+
port: this.getPort(options),
|
|
13
|
+
maxNumberOfRetries: this.getMaxNumberOfRetries(options),
|
|
14
|
+
frequency: this.getFrequency(options),
|
|
15
|
+
epochsNum: this.getEpochsNum(options),
|
|
16
|
+
hashFunctionId: this.getHashFunctionId(options),
|
|
17
|
+
scoreFunctionId: this.getScoreFunctionId(options),
|
|
18
|
+
immutable: this.getImmutable(options),
|
|
19
|
+
tokenAmount: this.getTokenAmount(options),
|
|
20
|
+
authToken: this.getAuthToken(options),
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
getAssetGetArguments(options) {
|
|
25
|
+
return {
|
|
26
|
+
blockchain: this.getBlockchain(options),
|
|
27
|
+
endpoint: this.getEndpoint(options),
|
|
28
|
+
port: this.getPort(options),
|
|
29
|
+
maxNumberOfRetries: this.getMaxNumberOfRetries(options),
|
|
30
|
+
frequency: this.getFrequency(options),
|
|
31
|
+
state: this.getState(options),
|
|
32
|
+
contentType: this.getContentType(options),
|
|
33
|
+
validate: this.getValidate(options),
|
|
34
|
+
outputFormat: this.getOutputFormat(options),
|
|
35
|
+
authToken: this.getAuthToken(options),
|
|
36
|
+
hashFunctionId: this.getHashFunctionId(options),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
getAssetUpdateArguments(options) {
|
|
41
|
+
return {
|
|
42
|
+
blockchain: this.getBlockchain(options),
|
|
43
|
+
endpoint: this.getEndpoint(options),
|
|
44
|
+
port: this.getPort(options),
|
|
45
|
+
maxNumberOfRetries: this.getMaxNumberOfRetries(options),
|
|
46
|
+
frequency: this.getFrequency(options),
|
|
47
|
+
hashFunctionId: this.getHashFunctionId(options),
|
|
48
|
+
scoreFunctionId: this.getScoreFunctionId(options),
|
|
49
|
+
tokenAmount: this.getTokenAmount(options),
|
|
50
|
+
authToken: this.getAuthToken(options),
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
getQueryArguments(options) {
|
|
55
|
+
return {
|
|
56
|
+
graphLocation: this.getGraphLocation(options),
|
|
57
|
+
graphState: this.getGraphState(options),
|
|
58
|
+
endpoint: this.getEndpoint(options),
|
|
59
|
+
port: this.getPort(options),
|
|
60
|
+
maxNumberOfRetries: this.getMaxNumberOfRetries(options),
|
|
61
|
+
frequency: this.getFrequency(options),
|
|
62
|
+
authToken: this.getAuthToken(options),
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
getBlockchain(options) {
|
|
67
|
+
const name = options.blockchain?.name ?? this.config.blockchain?.name ?? null;
|
|
68
|
+
const rpc =
|
|
69
|
+
options.blockchain?.rpc ?? this.config.blockchain?.rpc ?? BLOCKCHAINS[name]?.rpc;
|
|
70
|
+
const hubContract =
|
|
71
|
+
options.blockchain?.hubContract ??
|
|
72
|
+
this.config.blockchain?.hubContract ??
|
|
73
|
+
BLOCKCHAINS[name]?.hubContract;
|
|
74
|
+
const publicKey =
|
|
75
|
+
options.blockchain?.publicKey ?? this.config.blockchain?.publicKey ?? null;
|
|
76
|
+
const privateKey =
|
|
77
|
+
options.blockchain?.privateKey ?? this.config.blockchain?.privateKey ?? null;
|
|
78
|
+
|
|
79
|
+
return {
|
|
80
|
+
name,
|
|
81
|
+
rpc,
|
|
82
|
+
hubContract,
|
|
83
|
+
publicKey,
|
|
84
|
+
privateKey,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
getGraphLocation(options) {
|
|
89
|
+
return (
|
|
90
|
+
options.graphLocation ?? this.config.graphLocation ?? DEFAULT_PARAMETERS.GRAPH_LOCATION
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
getGraphState(options) {
|
|
95
|
+
return options.graphState ?? this.config.graphState ?? DEFAULT_PARAMETERS.GRAPH_STATE;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
getEndpoint(options) {
|
|
99
|
+
return options.endpoint ?? this.config.endpoint ?? null;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
getPort(options) {
|
|
103
|
+
return options.port ?? this.config.port ?? DEFAULT_PARAMETERS.PORT;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
getFrequency(options) {
|
|
107
|
+
return options.frequency ?? this.config.frequency ?? DEFAULT_PARAMETERS.FREQUENCY;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
getHashFunctionId(options) {
|
|
111
|
+
return (
|
|
112
|
+
options.hashFunctionId ??
|
|
113
|
+
this.config.hashFunctionId ??
|
|
114
|
+
DEFAULT_PARAMETERS.HASH_FUNCTION_ID
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
getScoreFunctionId(options) {
|
|
119
|
+
return (
|
|
120
|
+
options.scoreFunctionId ??
|
|
121
|
+
this.config.scoreFunctionId ??
|
|
122
|
+
DEFAULT_PARAMETERS.SCORE_FUNCTION_ID
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
getEpochsNum(options) {
|
|
127
|
+
return options.epochsNum ?? this.config.epochsNum ?? null;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
getImmutable(options) {
|
|
131
|
+
return options.immutable ?? this.config.immutable ?? DEFAULT_PARAMETERS.IMMUTABLE;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
getTokenAmount(options) {
|
|
135
|
+
return options.tokenAmount ?? this.config.tokenAmount ?? null;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
getState(options) {
|
|
139
|
+
return options.state ?? this.config.state ?? DEFAULT_PARAMETERS.STATE;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
getContentType(options) {
|
|
143
|
+
return options.contentType ?? this.config.contentType ?? DEFAULT_PARAMETERS.CONTENT_TYPE;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
getValidate(options) {
|
|
147
|
+
return options.validate ?? this.config.validate ?? DEFAULT_PARAMETERS.VALIDATE;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
getOutputFormat(options) {
|
|
151
|
+
return options.outputFormat ?? this.config.outputFormat ?? DEFAULT_PARAMETERS.OUTPUT_FORMAT;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
getMaxNumberOfRetries(options) {
|
|
155
|
+
return (
|
|
156
|
+
options.maxNumberOfRetries ??
|
|
157
|
+
this.config.maxNumberOfRetries ??
|
|
158
|
+
DEFAULT_PARAMETERS.MAX_NUMBER_OF_RETRIES
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
getAuthToken(options) {
|
|
163
|
+
return options.auth?.token ?? this.config?.auth?.token ?? null;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
module.exports = InputService;
|
|
@@ -1,22 +1,18 @@
|
|
|
1
1
|
const axios = require('axios');
|
|
2
|
-
const {
|
|
3
|
-
|
|
4
|
-
DEFAULT_GET_OPERATION_RESULT_FREQUENCY,
|
|
5
|
-
DEFAULT_GET_OPERATION_RESULT_MAX_NUM_RETRIES,
|
|
6
|
-
} = require('../../../constants.js');
|
|
7
|
-
const { sleepForMilliseconds, resolveUAL } = require('../../utilities.js');
|
|
2
|
+
const { OPERATION_STATUSES } = require('../../../constants.js');
|
|
3
|
+
const { sleepForMilliseconds } = require('../../utilities.js');
|
|
8
4
|
|
|
9
5
|
class HttpService {
|
|
10
|
-
constructor(config) {
|
|
6
|
+
constructor(config = {}) {
|
|
11
7
|
this.config = config;
|
|
12
8
|
}
|
|
13
9
|
|
|
14
|
-
async info() {
|
|
10
|
+
async info(endpoint, port, authToken) {
|
|
15
11
|
try {
|
|
16
12
|
const response = await axios({
|
|
17
13
|
method: 'get',
|
|
18
|
-
url: `${
|
|
19
|
-
headers: this.prepareRequestConfig(),
|
|
14
|
+
url: `${endpoint}:${port}/info`,
|
|
15
|
+
headers: this.prepareRequestConfig(authToken),
|
|
20
16
|
});
|
|
21
17
|
|
|
22
18
|
return response;
|
|
@@ -26,19 +22,20 @@ class HttpService {
|
|
|
26
22
|
}
|
|
27
23
|
|
|
28
24
|
async getBidSuggestion(
|
|
25
|
+
endpoint,
|
|
26
|
+
port,
|
|
27
|
+
authToken,
|
|
29
28
|
blockchain,
|
|
30
29
|
epochsNumber,
|
|
31
30
|
assertionSize,
|
|
32
31
|
contentAssetStorageAddress,
|
|
33
32
|
firstAssertionId,
|
|
34
33
|
hashFunctionId,
|
|
35
|
-
options,
|
|
36
34
|
) {
|
|
37
|
-
const endpoint = options.endpoint ?? this.config.endpoint;
|
|
38
35
|
try {
|
|
39
36
|
const response = await axios({
|
|
40
37
|
method: 'get',
|
|
41
|
-
url: `${endpoint}:${
|
|
38
|
+
url: `${endpoint}:${port}/bid-suggestion`,
|
|
42
39
|
params: {
|
|
43
40
|
blockchain,
|
|
44
41
|
epochsNumber,
|
|
@@ -47,7 +44,7 @@ class HttpService {
|
|
|
47
44
|
firstAssertionId,
|
|
48
45
|
hashFunctionId,
|
|
49
46
|
},
|
|
50
|
-
headers: this.prepareRequestConfig(),
|
|
47
|
+
headers: this.prepareRequestConfig(authToken),
|
|
51
48
|
});
|
|
52
49
|
|
|
53
50
|
return response.data.bidSuggestion;
|
|
@@ -56,15 +53,13 @@ class HttpService {
|
|
|
56
53
|
}
|
|
57
54
|
}
|
|
58
55
|
|
|
59
|
-
async localStore(
|
|
60
|
-
const endpoint = options.endpoint ?? this.config.endpoint;
|
|
61
|
-
|
|
56
|
+
async localStore(endpoint, port, authToken, assertions) {
|
|
62
57
|
try {
|
|
63
58
|
const response = await axios({
|
|
64
59
|
method: 'post',
|
|
65
|
-
url: `${endpoint}:${
|
|
60
|
+
url: `${endpoint}:${port}/local-store`,
|
|
66
61
|
data: assertions,
|
|
67
|
-
headers: this.prepareRequestConfig(),
|
|
62
|
+
headers: this.prepareRequestConfig(authToken),
|
|
68
63
|
});
|
|
69
64
|
|
|
70
65
|
return response.data.operationId;
|
|
@@ -73,21 +68,30 @@ class HttpService {
|
|
|
73
68
|
}
|
|
74
69
|
}
|
|
75
70
|
|
|
76
|
-
async publish(
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
71
|
+
async publish(
|
|
72
|
+
endpoint,
|
|
73
|
+
port,
|
|
74
|
+
authToken,
|
|
75
|
+
assertionId,
|
|
76
|
+
assertion,
|
|
77
|
+
blockchain,
|
|
78
|
+
contract,
|
|
79
|
+
tokenId,
|
|
80
|
+
hashFunctionId,
|
|
81
|
+
) {
|
|
85
82
|
try {
|
|
86
83
|
const response = await axios({
|
|
87
84
|
method: 'post',
|
|
88
|
-
url: `${endpoint}:${
|
|
89
|
-
data:
|
|
90
|
-
|
|
85
|
+
url: `${endpoint}:${port}/publish`,
|
|
86
|
+
data: {
|
|
87
|
+
assertionId,
|
|
88
|
+
assertion,
|
|
89
|
+
blockchain,
|
|
90
|
+
contract,
|
|
91
|
+
tokenId,
|
|
92
|
+
hashFunctionId,
|
|
93
|
+
},
|
|
94
|
+
headers: this.prepareRequestConfig(authToken),
|
|
91
95
|
});
|
|
92
96
|
|
|
93
97
|
return response.data.operationId;
|
|
@@ -96,15 +100,17 @@ class HttpService {
|
|
|
96
100
|
}
|
|
97
101
|
}
|
|
98
102
|
|
|
99
|
-
async get(UAL,
|
|
100
|
-
const requestBody = this.prepareGetAssertionRequest(UAL, 1);
|
|
101
|
-
const endpoint = options.endpoint ?? this.config.endpoint;
|
|
103
|
+
async get(endpoint, port, authToken, UAL, state, hashFunctionId) {
|
|
102
104
|
try {
|
|
103
105
|
const response = await axios({
|
|
104
106
|
method: 'post',
|
|
105
|
-
url: `${endpoint}:${
|
|
106
|
-
data:
|
|
107
|
-
|
|
107
|
+
url: `${endpoint}:${port}/get`,
|
|
108
|
+
data: {
|
|
109
|
+
id: UAL,
|
|
110
|
+
state,
|
|
111
|
+
hashFunctionId,
|
|
112
|
+
},
|
|
113
|
+
headers: this.prepareRequestConfig(authToken),
|
|
108
114
|
});
|
|
109
115
|
|
|
110
116
|
return response.data.operationId;
|
|
@@ -113,14 +119,45 @@ class HttpService {
|
|
|
113
119
|
}
|
|
114
120
|
}
|
|
115
121
|
|
|
116
|
-
async
|
|
117
|
-
|
|
122
|
+
async update(
|
|
123
|
+
endpoint,
|
|
124
|
+
port,
|
|
125
|
+
authToken,
|
|
126
|
+
assertionId,
|
|
127
|
+
assertion,
|
|
128
|
+
blockchain,
|
|
129
|
+
contract,
|
|
130
|
+
tokenId,
|
|
131
|
+
hashFunctionId,
|
|
132
|
+
) {
|
|
118
133
|
try {
|
|
119
134
|
const response = await axios({
|
|
120
135
|
method: 'post',
|
|
121
|
-
url: `${endpoint}:${
|
|
122
|
-
data: {
|
|
123
|
-
|
|
136
|
+
url: `${endpoint}:${port}/update`,
|
|
137
|
+
data: {
|
|
138
|
+
assertionId,
|
|
139
|
+
assertion,
|
|
140
|
+
blockchain,
|
|
141
|
+
contract,
|
|
142
|
+
tokenId,
|
|
143
|
+
hashFunctionId,
|
|
144
|
+
},
|
|
145
|
+
headers: this.prepareRequestConfig(authToken),
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
return response.data.operationId;
|
|
149
|
+
} catch (error) {
|
|
150
|
+
throw Error(`Unable to update: ${error.message}`);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
async query(endpoint, port, authToken, query, type, repository) {
|
|
155
|
+
try {
|
|
156
|
+
const response = await axios({
|
|
157
|
+
method: 'post',
|
|
158
|
+
url: `${endpoint}:${port}/query`,
|
|
159
|
+
data: { query, type, repository },
|
|
160
|
+
headers: this.prepareRequestConfig(authToken),
|
|
124
161
|
});
|
|
125
162
|
return response.data.operationId;
|
|
126
163
|
} catch (error) {
|
|
@@ -128,24 +165,25 @@ class HttpService {
|
|
|
128
165
|
}
|
|
129
166
|
}
|
|
130
167
|
|
|
131
|
-
async getOperationResult(
|
|
168
|
+
async getOperationResult(
|
|
169
|
+
endpoint,
|
|
170
|
+
port,
|
|
171
|
+
authToken,
|
|
172
|
+
operation,
|
|
173
|
+
maxNumberOfRetries,
|
|
174
|
+
frequency,
|
|
175
|
+
operationId,
|
|
176
|
+
) {
|
|
132
177
|
await sleepForMilliseconds(500);
|
|
133
|
-
if (!operationId) {
|
|
134
|
-
throw Error('Operation ID is missing, unable to fetch the operation results.');
|
|
135
|
-
}
|
|
136
178
|
let response = {
|
|
137
179
|
status: OPERATION_STATUSES.PENDING,
|
|
138
180
|
};
|
|
139
181
|
let retries = 0;
|
|
140
|
-
const maxNumberOfRetries =
|
|
141
|
-
options.maxNumberOfRetries ?? DEFAULT_GET_OPERATION_RESULT_MAX_NUM_RETRIES;
|
|
142
|
-
const frequency = options.frequency ?? DEFAULT_GET_OPERATION_RESULT_FREQUENCY;
|
|
143
182
|
|
|
144
|
-
const endpoint = options.endpoint ?? this.config.endpoint;
|
|
145
183
|
const axios_config = {
|
|
146
184
|
method: 'get',
|
|
147
|
-
url: `${endpoint}:${
|
|
148
|
-
headers: this.prepareRequestConfig(),
|
|
185
|
+
url: `${endpoint}:${port}/${operation}/${operationId}`,
|
|
186
|
+
headers: this.prepareRequestConfig(authToken),
|
|
149
187
|
};
|
|
150
188
|
do {
|
|
151
189
|
if (retries > maxNumberOfRetries) {
|
|
@@ -170,28 +208,9 @@ class HttpService {
|
|
|
170
208
|
return response.data;
|
|
171
209
|
}
|
|
172
210
|
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
assertionId,
|
|
177
|
-
assertion,
|
|
178
|
-
blockchain,
|
|
179
|
-
contract,
|
|
180
|
-
tokenId: parseInt(tokenId, 10),
|
|
181
|
-
hashFunctionId,
|
|
182
|
-
};
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
prepareGetAssertionRequest(UAL, hashFunctionId = 1) {
|
|
186
|
-
return {
|
|
187
|
-
id: UAL,
|
|
188
|
-
hashFunctionId,
|
|
189
|
-
};
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
prepareRequestConfig() {
|
|
193
|
-
if (this.config?.auth?.token) {
|
|
194
|
-
return { Authorization: `Bearer ${this.config.auth.token}` };
|
|
211
|
+
prepareRequestConfig(authToken) {
|
|
212
|
+
if (authToken) {
|
|
213
|
+
return { Authorization: `Bearer ${authToken}` };
|
|
195
214
|
}
|
|
196
215
|
|
|
197
216
|
return {};
|
package/services/utilities.js
CHANGED
|
@@ -1,27 +1,29 @@
|
|
|
1
|
+
const jsonld = require('jsonld');
|
|
2
|
+
const { GRAPH_LOCATIONS, GRAPH_STATES, OT_NODE_TRIPLE_STORE_REPOSITORIES } = require('../constants.js');
|
|
3
|
+
|
|
1
4
|
module.exports = {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const args = argsString.split("/");
|
|
5
|
+
nodeSupported() {
|
|
6
|
+
return typeof window === 'undefined';
|
|
7
|
+
},
|
|
8
|
+
isEmptyObject(object) {
|
|
9
|
+
// eslint-disable-next-line no-unreachable-loop
|
|
10
|
+
for (const key in object) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
return true;
|
|
14
|
+
},
|
|
15
|
+
toNumber(hex) {
|
|
16
|
+
return parseInt(hex.slice(2), 16);
|
|
17
|
+
},
|
|
18
|
+
deriveUAL(blockchain, contract, tokenId) {
|
|
19
|
+
return `did:dkg:${
|
|
20
|
+
blockchain.startsWith('otp') ? 'otp' : blockchain.toLowerCase()
|
|
21
|
+
}/${contract.toLowerCase()}/${tokenId}`;
|
|
22
|
+
},
|
|
23
|
+
resolveUAL(ual) {
|
|
24
|
+
const segments = ual.split(':');
|
|
25
|
+
const argsString = segments.length === 3 ? segments[2] : segments[2] + segments[3];
|
|
26
|
+
const args = argsString.split('/');
|
|
25
27
|
|
|
26
28
|
if (args.length !== 3) {
|
|
27
29
|
throw new Error(`UAL doesn't have correct format: ${ual}`);
|
|
@@ -30,9 +32,25 @@ module.exports = {
|
|
|
30
32
|
return {
|
|
31
33
|
blockchain: args[0],
|
|
32
34
|
contract: args[1],
|
|
33
|
-
tokenId: args[2],
|
|
35
|
+
tokenId: parseInt(args[2], 10),
|
|
34
36
|
};
|
|
35
37
|
},
|
|
38
|
+
deriveRepository(graphLocation, graphState) {
|
|
39
|
+
switch (graphLocation + graphState) {
|
|
40
|
+
case GRAPH_LOCATIONS.PUBLIC_KG + GRAPH_STATES.CURRENT:
|
|
41
|
+
return OT_NODE_TRIPLE_STORE_REPOSITORIES.PUBLIC_CURRENT;
|
|
42
|
+
case GRAPH_LOCATIONS.PUBLIC_KG + GRAPH_STATES.HISTORICAL:
|
|
43
|
+
return OT_NODE_TRIPLE_STORE_REPOSITORIES.PUBLIC_HISTORY;
|
|
44
|
+
case GRAPH_LOCATIONS.LOCAL_KG + GRAPH_STATES.CURRENT:
|
|
45
|
+
return OT_NODE_TRIPLE_STORE_REPOSITORIES.PRIVATE_CURRENT;
|
|
46
|
+
case GRAPH_LOCATIONS.LOCAL_KG + GRAPH_STATES.HISTORICAL:
|
|
47
|
+
return OT_NODE_TRIPLE_STORE_REPOSITORIES.PRIVATE_HISTORY;
|
|
48
|
+
default:
|
|
49
|
+
throw new Error(
|
|
50
|
+
`Unknown graph location and state: ${graphLocation}, ${graphState}`,
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
},
|
|
36
54
|
async sleepForMilliseconds(milliseconds) {
|
|
37
55
|
// eslint-disable-next-line no-promise-executor-return
|
|
38
56
|
await new Promise((r) => setTimeout(r, milliseconds));
|
|
@@ -50,4 +68,24 @@ module.exports = {
|
|
|
50
68
|
...operationData,
|
|
51
69
|
};
|
|
52
70
|
},
|
|
71
|
+
async toNQuads(content, inputFormat) {
|
|
72
|
+
const options = {
|
|
73
|
+
algorithm: 'URDNA2015',
|
|
74
|
+
format: 'application/n-quads',
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
if (inputFormat) {
|
|
78
|
+
options.inputFormat = inputFormat;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const canonized = await jsonld.canonize(content, options);
|
|
82
|
+
|
|
83
|
+
return canonized.split('\n').filter((x) => x !== '');
|
|
84
|
+
},
|
|
85
|
+
async toJSONLD(nquads) {
|
|
86
|
+
return jsonld.fromRDF(nquads, {
|
|
87
|
+
algorithm: 'URDNA2015',
|
|
88
|
+
format: 'application/n-quads',
|
|
89
|
+
});
|
|
90
|
+
},
|
|
53
91
|
};
|