dash-platform-sdk 1.3.0-dev.4 → 1.3.0-dev.6
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/bundle.min.js +21 -2
- package/index.js +2 -5
- package/package.json +30 -31
- package/proto/generated/google/protobuf/wrappers.d.ts +260 -29
- package/proto/generated/google/protobuf/wrappers.js +562 -345
- package/proto/generated/platform.client.d.ts +251 -0
- package/proto/generated/platform.client.js +175 -0
- package/proto/generated/platform.d.ts +3682 -1939
- package/proto/generated/platform.js +8119 -18366
- package/src/DashPlatformSDK.d.ts +5 -4
- package/src/DashPlatformSDK.js +56 -46
- package/src/constants.js +7 -10
- package/src/contestedResources/createStateTransition.d.ts +1 -2
- package/src/contestedResources/createStateTransition.js +4 -7
- package/src/contestedResources/getContestedResourceVoteState.js +67 -118
- package/src/contestedResources/index.js +9 -55
- package/src/dataContracts/create.js +5 -8
- package/src/dataContracts/createStateTransition.js +10 -14
- package/src/dataContracts/getDataContractByIdentifier.js +40 -83
- package/src/dataContracts/index.js +17 -63
- package/src/documents/create.js +3 -6
- package/src/documents/createStateTransition.js +19 -31
- package/src/documents/index.js +25 -72
- package/src/documents/query.js +58 -90
- package/src/grpcConnectionPool.d.ts +30 -8
- package/src/grpcConnectionPool.js +63 -121
- package/src/identities/createStateTransition.js +14 -26
- package/src/identities/getIdentityBalance.js +37 -80
- package/src/identities/getIdentityByIdentifier.js +37 -80
- package/src/identities/getIdentityByNonUniquePublicKeyHash.js +41 -82
- package/src/identities/getIdentityByPublicKeyHash.js +37 -80
- package/src/identities/getIdentityContractNonce.js +40 -83
- package/src/identities/getIdentityNonce.js +38 -81
- package/src/identities/getIdentityPublicKeys.js +43 -81
- package/src/identities/index.js +57 -153
- package/src/keyPair/deriveChild.d.ts +2 -2
- package/src/keyPair/deriveChild.js +2 -49
- package/src/keyPair/derivePath.d.ts +2 -2
- package/src/keyPair/derivePath.js +2 -49
- package/src/keyPair/index.d.ts +39 -114
- package/src/keyPair/index.js +62 -296
- package/src/keyPair/mnemonicToSeed.d.ts +1 -1
- package/src/keyPair/mnemonicToSeed.js +3 -46
- package/src/names/index.js +43 -111
- package/src/names/registerName.js +60 -127
- package/src/names/searchByIdentity.d.ts +1 -1
- package/src/names/searchByIdentity.js +4 -50
- package/src/names/searchByName.js +15 -63
- package/src/names/testNameContested.js +1 -4
- package/src/names/validateName.js +3 -6
- package/src/node/epochs.js +43 -85
- package/src/node/index.d.ts +3 -3
- package/src/node/index.js +18 -77
- package/src/node/status.js +37 -81
- package/src/node/totalCredits.d.ts +2 -1
- package/src/node/totalCredits.js +32 -73
- package/src/signer/AbstractSigner.js +1 -2
- package/src/signer/setSigner.js +1 -4
- package/src/stateTransitions/broadcast.js +7 -57
- package/src/stateTransitions/index.js +13 -66
- package/src/stateTransitions/waitForStateTransitionResult.js +6 -58
- package/src/tokens/createStateTransition.js +23 -35
- package/src/tokens/getIdentitiesTokenBalances.js +40 -83
- package/src/tokens/getIdentityTokensBalances.js +40 -83
- package/src/tokens/getTokenContractInfo.js +40 -83
- package/src/tokens/getTokenDirectPurchasePrices.js +37 -80
- package/src/tokens/getTokenTotalSupply.js +38 -81
- package/src/tokens/index.js +39 -124
- package/src/types.d.ts +6 -12
- package/src/types.js +6 -16
- package/src/utils/base58ToBytes.js +3 -6
- package/src/utils/bytesToHex.js +2 -5
- package/src/utils/bytesToTypedArray.d.ts +1 -0
- package/src/utils/bytesToTypedArray.js +3 -0
- package/src/utils/calculateMsgHash.d.ts +1 -1
- package/src/utils/calculateMsgHash.js +10 -13
- package/src/utils/calculateSignHash.d.ts +1 -1
- package/src/utils/calculateSignHash.js +7 -10
- package/src/utils/calculateStateIdHash.d.ts +1 -1
- package/src/utils/calculateStateIdHash.js +8 -10
- package/src/utils/convertToHomographSafeChars.js +2 -5
- package/src/utils/createVoterIdentityId.d.ts +1 -1
- package/src/utils/createVoterIdentityId.js +12 -11
- package/src/utils/getEvonodeList.d.ts +2 -2
- package/src/utils/getEvonodeList.js +10 -61
- package/src/utils/getQuorumPublicKey.js +15 -66
- package/src/utils/getRandomArrayItem.js +1 -4
- package/src/utils/getRandomBytes.js +2 -5
- package/src/utils/hexToBytes.js +2 -6
- package/src/utils/index.js +21 -28
- package/src/utils/indexBytesToString.js +2 -5
- package/src/utils/sha256.d.ts +1 -1
- package/src/utils/sha256.js +10 -8
- package/src/utils/signHash.d.ts +1 -1
- package/src/utils/signHash.js +9 -12
- package/src/utils/signRequestId.d.ts +1 -1
- package/src/utils/signRequestId.js +9 -12
- package/src/utils/sleep.js +2 -50
- package/src/utils/stringToIndexValueBytes.js +4 -7
- package/src/utils/verifyTenderdashProof.d.ts +1 -1
- package/src/utils/verifyTenderdashProof.js +13 -17
- package/src/voting/createStateTransition.d.ts +1 -2
- package/src/voting/createStateTransition.js +4 -7
- package/src/voting/createVote.js +4 -7
- package/src/voting/index.d.ts +3 -4
- package/src/voting/index.js +19 -26
- package/test/unit/ContestedResources.spec.js +92 -177
- package/test/unit/DataContract.spec.js +44 -111
- package/test/unit/Document.spec.js +93 -183
- package/test/unit/Identity.spec.js +221 -346
- package/test/unit/KeyPair.spec.js +30 -145
- package/test/unit/Names.spec.js +27 -90
- package/test/unit/Node.spec.js +55 -126
- package/test/unit/SDK.spec.js +7 -9
- package/test/unit/Tokens.spec.js +111 -278
- package/bundle.min.js.LICENSE.txt +0 -1
- package/src/keyPair/keyToPublicKey.d.ts +0 -2
- package/src/keyPair/keyToPublicKey.js +0 -47
- package/src/keyPair/keyToWalletId.d.ts +0 -2
- package/src/keyPair/keyToWalletId.js +0 -50
- package/src/keyPair/keyToXPrivateKey.d.ts +0 -3
- package/src/keyPair/keyToXPrivateKey.js +0 -50
- package/src/keyPair/keyToXPublicKey.d.ts +0 -3
- package/src/keyPair/keyToXPublicKey.js +0 -50
- package/src/keyPair/mnemonicToIdentityKey.d.ts +0 -3
- package/src/keyPair/mnemonicToIdentityKey.js +0 -57
- package/src/keyPair/mnemonicToWallet.d.ts +0 -2
- package/src/keyPair/mnemonicToWallet.js +0 -55
- package/src/keyPair/privateKeyToWif.d.ts +0 -2
- package/src/keyPair/privateKeyToWif.js +0 -50
- package/src/keyPair/publicKeyToAddress.d.ts +0 -2
- package/src/keyPair/publicKeyToAddress.js +0 -50
- package/src/keyPair/seedToWallet.d.ts +0 -2
- package/src/keyPair/seedToWallet.js +0 -50
- package/src/keyPair/walletToIdentityKey.d.ts +0 -3
- package/src/keyPair/walletToIdentityKey.js +0 -56
- package/src/keyPair/xkeyToHDXKey.d.ts +0 -2
- package/src/keyPair/xkeyToHDXKey.js +0 -50
|
@@ -1,87 +1,44 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
13
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
1
|
+
import { DataContractWASM, IdentifierWASM, PlatformVersionWASM, verifyContractProof } from 'pshenmic-dpp';
|
|
2
|
+
import { GetDataContractRequest } from '../../proto/generated/platform';
|
|
3
|
+
import { getQuorumPublicKey } from '../utils/getQuorumPublicKey';
|
|
4
|
+
import bytesToHex from '../utils/bytesToHex';
|
|
5
|
+
import verifyTenderdashProof from '../utils/verifyTenderdashProof';
|
|
6
|
+
import { DPNS_DATA_CONTRACT_BYTES, DPNS_DATA_CONTRACT_ID } from '../constants';
|
|
7
|
+
export default async function getByIdentifier(grpcPool, identifier) {
|
|
8
|
+
const id = new IdentifierWASM(identifier);
|
|
9
|
+
if (id.base58() === DPNS_DATA_CONTRACT_ID) {
|
|
10
|
+
return DataContractWASM.fromBase64(DPNS_DATA_CONTRACT_BYTES, true, 9);
|
|
36
11
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
var bytesToHex_1 = require("../utils/bytesToHex");
|
|
44
|
-
var verifyTenderdashProof_1 = require("../utils/verifyTenderdashProof");
|
|
45
|
-
var constants_1 = require("../constants");
|
|
46
|
-
function getByIdentifier(grpcPool, identifier) {
|
|
47
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
48
|
-
var id, getDataContractRequest, v0, _a, proof, metadata, _b, rootHash, dataContract, quorumPublicKey, verify;
|
|
49
|
-
return __generator(this, function (_c) {
|
|
50
|
-
switch (_c.label) {
|
|
51
|
-
case 0:
|
|
52
|
-
id = new pshenmic_dpp_1.IdentifierWASM(identifier);
|
|
53
|
-
if (id.base58() === constants_1.DPNS_DATA_CONTRACT_ID) {
|
|
54
|
-
return [2 /*return*/, pshenmic_dpp_1.DataContractWASM.fromBase64(constants_1.DPNS_DATA_CONTRACT_BYTES, true, 9)];
|
|
55
|
-
}
|
|
56
|
-
getDataContractRequest = platform_1.GetDataContractRequest.fromPartial({
|
|
57
|
-
v0: {
|
|
58
|
-
id: id.bytes(),
|
|
59
|
-
prove: true
|
|
60
|
-
}
|
|
61
|
-
});
|
|
62
|
-
return [4 /*yield*/, grpcPool.getClient().getDataContract(getDataContractRequest)];
|
|
63
|
-
case 1:
|
|
64
|
-
v0 = (_c.sent()).v0;
|
|
65
|
-
_a = v0, proof = _a.proof, metadata = _a.metadata;
|
|
66
|
-
if (proof == null) {
|
|
67
|
-
throw new Error('Proof not found');
|
|
68
|
-
}
|
|
69
|
-
if (metadata == null) {
|
|
70
|
-
throw new Error('Metadata not found');
|
|
71
|
-
}
|
|
72
|
-
_b = (0, pshenmic_dpp_1.verifyContractProof)(proof.grovedbProof, undefined, false, false, id.bytes(), pshenmic_dpp_1.PlatformVersionWASM.PLATFORM_V9), rootHash = _b.rootHash, dataContract = _b.dataContract;
|
|
73
|
-
if (dataContract == null) {
|
|
74
|
-
throw new Error("Data Contract with identifier ".concat(id.base58(), " not found"));
|
|
75
|
-
}
|
|
76
|
-
return [4 /*yield*/, (0, getQuorumPublicKey_1.getQuorumPublicKey)(grpcPool.network, proof.quorumType, (0, bytesToHex_1.default)(proof.quorumHash))];
|
|
77
|
-
case 2:
|
|
78
|
-
quorumPublicKey = _c.sent();
|
|
79
|
-
verify = (0, verifyTenderdashProof_1.default)(proof, metadata, rootHash, quorumPublicKey);
|
|
80
|
-
if (!verify) {
|
|
81
|
-
throw new Error('Failed to verify query');
|
|
82
|
-
}
|
|
83
|
-
return [2 /*return*/, dataContract];
|
|
12
|
+
const getDataContractRequest = GetDataContractRequest.create({
|
|
13
|
+
version: {
|
|
14
|
+
oneofKind: 'v0',
|
|
15
|
+
v0: {
|
|
16
|
+
id: id.bytes(),
|
|
17
|
+
prove: true
|
|
84
18
|
}
|
|
85
|
-
}
|
|
19
|
+
}
|
|
86
20
|
});
|
|
21
|
+
const { response } = await grpcPool.getClient().getDataContract(getDataContractRequest);
|
|
22
|
+
const { version } = response;
|
|
23
|
+
if (version.oneofKind !== 'v0') {
|
|
24
|
+
throw new Error('Unexpected oneOf type returned from DAPI (must be v0)');
|
|
25
|
+
}
|
|
26
|
+
const { v0 } = version;
|
|
27
|
+
if (v0.result.oneofKind !== 'proof') {
|
|
28
|
+
throw new Error('Unexpected oneOf type returned from DAPI (must be proof)');
|
|
29
|
+
}
|
|
30
|
+
const { result: { proof }, metadata } = v0;
|
|
31
|
+
if (metadata == null) {
|
|
32
|
+
throw new Error('Metadata not found');
|
|
33
|
+
}
|
|
34
|
+
const { rootHash, dataContract } = verifyContractProof(proof.grovedbProof, undefined, false, false, id.bytes(), PlatformVersionWASM.PLATFORM_V9);
|
|
35
|
+
if (dataContract == null) {
|
|
36
|
+
throw new Error(`Data Contract with identifier ${id.base58()} not found`);
|
|
37
|
+
}
|
|
38
|
+
const quorumPublicKey = await getQuorumPublicKey(grpcPool.network, proof.quorumType, bytesToHex(proof.quorumHash));
|
|
39
|
+
const verify = await verifyTenderdashProof(proof, metadata, rootHash, quorumPublicKey);
|
|
40
|
+
if (!verify) {
|
|
41
|
+
throw new Error('Failed to verify query');
|
|
42
|
+
}
|
|
43
|
+
return dataContract;
|
|
87
44
|
}
|
|
@@ -1,53 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
13
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.DataContractsController = void 0;
|
|
40
|
-
var getDataContractByIdentifier_1 = require("./getDataContractByIdentifier");
|
|
41
|
-
var create_1 = require("./create");
|
|
42
|
-
var createStateTransition_1 = require("./createStateTransition");
|
|
1
|
+
import getDataContractByIdentifier from './getDataContractByIdentifier';
|
|
2
|
+
import createDataContract from './create';
|
|
3
|
+
import createStateTransition from './createStateTransition';
|
|
43
4
|
/**
|
|
44
5
|
* Collection of methods necessary to work with Data Contracts in the network,
|
|
45
6
|
* such as data contract creation or retrieval
|
|
46
7
|
*
|
|
47
8
|
* @hideconstructor
|
|
48
9
|
*/
|
|
49
|
-
|
|
50
|
-
|
|
10
|
+
export class DataContractsController {
|
|
11
|
+
/** @ignore */
|
|
12
|
+
grpcPool;
|
|
13
|
+
constructor(grpcPool) {
|
|
51
14
|
this.grpcPool = grpcPool;
|
|
52
15
|
}
|
|
53
16
|
/**
|
|
@@ -64,9 +27,9 @@ var DataContractsController = /** @class */ (function () {
|
|
|
64
27
|
*
|
|
65
28
|
* @return {DataContractWASM}
|
|
66
29
|
*/
|
|
67
|
-
|
|
68
|
-
return (
|
|
69
|
-
}
|
|
30
|
+
create(ownerId, identityNonce, schema, fullValidation, tokenConfiguration, config, platformVersion) {
|
|
31
|
+
return createDataContract(ownerId, identityNonce, schema, tokenConfiguration, config, fullValidation, platformVersion);
|
|
32
|
+
}
|
|
70
33
|
/**
|
|
71
34
|
* Retrieves a Data Contract by an identifier from the network
|
|
72
35
|
*
|
|
@@ -74,16 +37,9 @@ var DataContractsController = /** @class */ (function () {
|
|
|
74
37
|
*
|
|
75
38
|
* @return {Promise<DataContractWASM>}
|
|
76
39
|
*/
|
|
77
|
-
|
|
78
|
-
return
|
|
79
|
-
|
|
80
|
-
switch (_a.label) {
|
|
81
|
-
case 0: return [4 /*yield*/, (0, getDataContractByIdentifier_1.default)(this.grpcPool, identifier)];
|
|
82
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
});
|
|
86
|
-
};
|
|
40
|
+
async getDataContractByIdentifier(identifier) {
|
|
41
|
+
return await getDataContractByIdentifier(this.grpcPool, identifier);
|
|
42
|
+
}
|
|
87
43
|
/**
|
|
88
44
|
* Helper function to create a state transition from a {DataContractWASM} instance
|
|
89
45
|
* Pass {DataContractTransitionType} in type param to specify which type of operation you want
|
|
@@ -95,9 +51,7 @@ var DataContractsController = /** @class */ (function () {
|
|
|
95
51
|
*
|
|
96
52
|
* @return {StateTransitionWASM}
|
|
97
53
|
*/
|
|
98
|
-
|
|
99
|
-
return (
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
}());
|
|
103
|
-
exports.DataContractsController = DataContractsController;
|
|
54
|
+
createStateTransition(dataContract, type, identityNonce) {
|
|
55
|
+
return createStateTransition(dataContract, type, identityNonce);
|
|
56
|
+
}
|
|
57
|
+
}
|
package/src/documents/create.js
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var pshenmic_dpp_1 = require("pshenmic-dpp");
|
|
5
|
-
function createDocument(dataContractId, documentType, data, owner, revision, documentId) {
|
|
6
|
-
return new pshenmic_dpp_1.DocumentWASM(data, documentType, revision !== null && revision !== void 0 ? revision : BigInt(1), dataContractId, owner, documentId);
|
|
1
|
+
import { DocumentWASM } from 'pshenmic-dpp';
|
|
2
|
+
export default function createDocument(dataContractId, documentType, data, owner, revision, documentId) {
|
|
3
|
+
return new DocumentWASM(data, documentType, revision ?? BigInt(1), dataContractId, owner, documentId);
|
|
7
4
|
}
|
|
@@ -1,64 +1,52 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
4
|
-
if (ar || !(i in from)) {
|
|
5
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
6
|
-
ar[i] = from[i];
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.default = createStateTransition;
|
|
13
|
-
var pshenmic_dpp_1 = require("pshenmic-dpp");
|
|
14
|
-
var documentTransitionsMap = {
|
|
1
|
+
import { BatchedTransitionWASM, BatchTransitionWASM, DocumentCreateTransitionWASM, DocumentDeleteTransitionWASM, DocumentPurchaseTransitionWASM, DocumentReplaceTransitionWASM, DocumentTransferTransitionWASM, DocumentUpdatePriceTransitionWASM } from 'pshenmic-dpp';
|
|
2
|
+
const documentTransitionsMap = {
|
|
15
3
|
create: {
|
|
16
|
-
class:
|
|
4
|
+
class: DocumentCreateTransitionWASM,
|
|
17
5
|
arguments: ['identityContractNonce'],
|
|
18
6
|
optionalArguments: ['prefundedVotingBalance', 'tokenPaymentInfo']
|
|
19
7
|
},
|
|
20
8
|
replace: {
|
|
21
|
-
class:
|
|
9
|
+
class: DocumentReplaceTransitionWASM,
|
|
22
10
|
arguments: ['identityContractNonce'],
|
|
23
11
|
optionalArguments: ['tokenPaymentInfo']
|
|
24
12
|
},
|
|
25
13
|
delete: {
|
|
26
|
-
class:
|
|
14
|
+
class: DocumentDeleteTransitionWASM,
|
|
27
15
|
arguments: ['identityContractNonce'],
|
|
28
16
|
optionalArguments: ['tokenPaymentInfo']
|
|
29
17
|
},
|
|
30
18
|
updatePrice: {
|
|
31
|
-
class:
|
|
19
|
+
class: DocumentUpdatePriceTransitionWASM,
|
|
32
20
|
arguments: ['identityContractNonce', 'price'],
|
|
33
21
|
optionalArguments: ['tokenPaymentInfo']
|
|
34
22
|
},
|
|
35
23
|
transfer: {
|
|
36
|
-
class:
|
|
24
|
+
class: DocumentTransferTransitionWASM,
|
|
37
25
|
arguments: ['identityContractNonce', 'recipientId'],
|
|
38
26
|
optionalArguments: ['tokenPaymentInfo']
|
|
39
27
|
},
|
|
40
28
|
purchase: {
|
|
41
|
-
class:
|
|
29
|
+
class: DocumentPurchaseTransitionWASM,
|
|
42
30
|
arguments: ['identityContractNonce', 'amount'],
|
|
43
31
|
optionalArguments: ['tokenPaymentInfo']
|
|
44
32
|
}
|
|
45
33
|
};
|
|
46
|
-
function createStateTransition(document, type, params) {
|
|
47
|
-
|
|
34
|
+
export default function createStateTransition(document, type, params) {
|
|
35
|
+
const { class: TransitionClass, arguments: classArguments, optionalArguments } = documentTransitionsMap[type];
|
|
48
36
|
if (TransitionClass == null) {
|
|
49
|
-
throw new Error(
|
|
37
|
+
throw new Error(`Unimplemented transition type: ${type}`);
|
|
50
38
|
}
|
|
51
39
|
// check if all required params for document transition exists
|
|
52
|
-
|
|
53
|
-
.filter(
|
|
54
|
-
!(optionalArguments).includes(classArgument)
|
|
40
|
+
const [missingArgument] = classArguments
|
|
41
|
+
.filter((classArgument) => params[classArgument] == null &&
|
|
42
|
+
!(optionalArguments).includes(classArgument));
|
|
55
43
|
if (missingArgument != null) {
|
|
56
|
-
throw new Error(
|
|
44
|
+
throw new Error(`Document transition param "${missingArgument}" is missing`);
|
|
57
45
|
}
|
|
58
|
-
|
|
46
|
+
const transitionParams = classArguments.concat(optionalArguments).map((classArgument) => params[classArgument]);
|
|
59
47
|
// @ts-expect-error
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
48
|
+
const documentTransition = new TransitionClass(document, ...transitionParams).toDocumentTransition();
|
|
49
|
+
const batchedTransition = new BatchedTransitionWASM(documentTransition);
|
|
50
|
+
const batch = BatchTransitionWASM.fromV1BatchedTransitions([batchedTransition], document.ownerId, 1);
|
|
63
51
|
return batch.toStateTransition();
|
|
64
52
|
}
|
package/src/documents/index.js
CHANGED
|
@@ -1,53 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
13
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.DocumentsController = void 0;
|
|
40
|
-
var create_1 = require("./create");
|
|
41
|
-
var pshenmic_dpp_1 = require("pshenmic-dpp");
|
|
42
|
-
var createStateTransition_1 = require("./createStateTransition");
|
|
43
|
-
var query_1 = require("./query");
|
|
1
|
+
import createDocument from './create';
|
|
2
|
+
import { IdentifierWASM, PrefundedVotingBalanceWASM, TokenPaymentInfoWASM } from 'pshenmic-dpp';
|
|
3
|
+
import createStateTransition from './createStateTransition';
|
|
4
|
+
import query from './query';
|
|
44
5
|
/**
|
|
45
6
|
* Collection of methods to work with documents like creation, querying or preparing a transition action
|
|
46
7
|
*
|
|
47
8
|
* @hideconstructor
|
|
48
9
|
*/
|
|
49
|
-
|
|
50
|
-
|
|
10
|
+
export class DocumentsController {
|
|
11
|
+
/** @ignore **/
|
|
12
|
+
grpcPool;
|
|
13
|
+
constructor(grpcPool) {
|
|
51
14
|
this.grpcPool = grpcPool;
|
|
52
15
|
}
|
|
53
16
|
/**
|
|
@@ -61,9 +24,9 @@ var DocumentsController = /** @class */ (function () {
|
|
|
61
24
|
*
|
|
62
25
|
* @return {DataContractWASM}
|
|
63
26
|
*/
|
|
64
|
-
|
|
65
|
-
return (
|
|
66
|
-
}
|
|
27
|
+
create(dataContractId, documentType, data, owner, revision) {
|
|
28
|
+
return createDocument(new IdentifierWASM(dataContractId), documentType, data, new IdentifierWASM(owner), revision);
|
|
29
|
+
}
|
|
67
30
|
/**
|
|
68
31
|
* Make a query for documents retrieval from the network.
|
|
69
32
|
*
|
|
@@ -75,20 +38,12 @@ var DocumentsController = /** @class */ (function () {
|
|
|
75
38
|
* @param startAt {IdentifierLike=} Optional offset, where startAt is an identifier of the document. Use identifier of last item in previous response
|
|
76
39
|
* @param startAfter {IdentifierLike=} Same as previous, but with exclusion. Cannot be set if startAt already provided
|
|
77
40
|
*/
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
throw new Error('You may only set either startAfter or startAt at once');
|
|
85
|
-
}
|
|
86
|
-
return [4 /*yield*/, (0, query_1.default)(this.grpcPool, new pshenmic_dpp_1.IdentifierWASM(dataContractId), documentType, where, orderBy, limit !== null && limit !== void 0 ? limit : 100, startAt, startAfter)];
|
|
87
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
});
|
|
91
|
-
};
|
|
41
|
+
async query(dataContractId, documentType, where, orderBy, limit, startAt, startAfter) {
|
|
42
|
+
if (startAfter != null && startAt !== null) {
|
|
43
|
+
throw new Error('You may only set either startAfter or startAt at once');
|
|
44
|
+
}
|
|
45
|
+
return await query(this.grpcPool, new IdentifierWASM(dataContractId), documentType, where, orderBy, limit ?? 100, startAt, startAfter);
|
|
46
|
+
}
|
|
92
47
|
/**
|
|
93
48
|
* Helper function for creating {StateTransitionWASM} from documents. It may be used to create any of 6 Document Transition actions:
|
|
94
49
|
*
|
|
@@ -103,7 +58,7 @@ var DocumentsController = /** @class */ (function () {
|
|
|
103
58
|
* @param type {string} Type of the document transition, must be a one of ('create' | 'replace' | 'delete' | 'updatePrice' |'transfer' | 'purchase')
|
|
104
59
|
* @param params {DocumentTransitionParams} params
|
|
105
60
|
*/
|
|
106
|
-
|
|
61
|
+
createStateTransition(document, type, params) {
|
|
107
62
|
if (type === 'transfer' && params.recipientId == null) {
|
|
108
63
|
throw new Error('Recipient is required for transfer transition');
|
|
109
64
|
}
|
|
@@ -114,17 +69,15 @@ var DocumentsController = /** @class */ (function () {
|
|
|
114
69
|
throw new Error('Amount is required for updatePrice transition');
|
|
115
70
|
}
|
|
116
71
|
if (params.prefundedVotingBalance != null) {
|
|
117
|
-
|
|
72
|
+
const { indexName, amount } = params.prefundedVotingBalance;
|
|
118
73
|
// @ts-expect-error
|
|
119
|
-
params.prefundedVotingBalance = new
|
|
74
|
+
params.prefundedVotingBalance = new PrefundedVotingBalanceWASM(indexName, amount);
|
|
120
75
|
}
|
|
121
76
|
if (params.tokenPaymentInfo != null) {
|
|
122
|
-
|
|
77
|
+
const { tokenContractId, tokenContractPosition, minimumTokenCost, maximumTokenCost, gasFeesPaidBy } = params.tokenPaymentInfo;
|
|
123
78
|
// @ts-expect-error
|
|
124
|
-
params.tokenPaymentInfo = new
|
|
79
|
+
params.tokenPaymentInfo = new TokenPaymentInfoWASM(new IdentifierWASM(tokenContractId), tokenContractPosition, minimumTokenCost, maximumTokenCost, gasFeesPaidBy);
|
|
125
80
|
}
|
|
126
|
-
return (
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
}());
|
|
130
|
-
exports.DocumentsController = DocumentsController;
|
|
81
|
+
return createStateTransition(document, type, params);
|
|
82
|
+
}
|
|
83
|
+
}
|
package/src/documents/query.js
CHANGED
|
@@ -1,94 +1,62 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
1
|
+
import { GetDocumentsRequest } from '../../proto/generated/platform';
|
|
2
|
+
import { IdentifierWASM, PlatformVersionWASM, verifyDocumentsProof } from 'pshenmic-dpp';
|
|
3
|
+
import { DAPI_DEFAULT_LIMIT } from '../constants';
|
|
4
|
+
import { getQuorumPublicKey } from '../utils/getQuorumPublicKey';
|
|
5
|
+
import bytesToHex from '../utils/bytesToHex';
|
|
6
|
+
import verifyTenderdashProof from '../utils/verifyTenderdashProof';
|
|
7
|
+
import { encode } from 'cbor-x';
|
|
8
|
+
import getDataContractByIdentifier from '../dataContracts/getDataContractByIdentifier';
|
|
9
|
+
export default async function query(grpcPool, dataContractId, documentTypeName, where, orderBy, limit = 100, startAt, startAfter) {
|
|
10
|
+
if ([startAt, startAfter].filter(e => e != null).length === 2) {
|
|
11
|
+
throw new Error('Only startAt or startAfter could be specified at one time');
|
|
12
|
+
}
|
|
13
|
+
let start;
|
|
14
|
+
if (startAt != null) {
|
|
15
|
+
start = {
|
|
16
|
+
oneofKind: 'startAt',
|
|
17
|
+
startAt: startAt.base58()
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
if (startAfter != null) {
|
|
21
|
+
start = {
|
|
22
|
+
oneofKind: 'startAfter',
|
|
23
|
+
startAt: startAfter.base58()
|
|
24
|
+
};
|
|
36
25
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
function query(grpcPool_1, dataContractId_1, documentTypeName_1, where_1, orderBy_1) {
|
|
49
|
-
return __awaiter(this, arguments, void 0, function (grpcPool, dataContractId, documentTypeName, where, orderBy, limit, startAt, startAfter) {
|
|
50
|
-
var getDocumentsRequest, dataContract, v0, _a, proof, metadata, startAtIncluded, _b, rootHash, documents, quorumPublicKey, verify;
|
|
51
|
-
var _c, _d;
|
|
52
|
-
if (limit === void 0) { limit = 100; }
|
|
53
|
-
return __generator(this, function (_e) {
|
|
54
|
-
switch (_e.label) {
|
|
55
|
-
case 0:
|
|
56
|
-
getDocumentsRequest = platform_1.GetDocumentsRequest.fromPartial({
|
|
57
|
-
v0: {
|
|
58
|
-
dataContractId: (new pshenmic_dpp_1.IdentifierWASM(dataContractId)).bytes(),
|
|
59
|
-
documentType: documentTypeName,
|
|
60
|
-
where: (where != null) ? (0, cbor_x_1.encode)(where) : undefined,
|
|
61
|
-
orderBy: (orderBy != null) ? (0, cbor_x_1.encode)(orderBy) : undefined,
|
|
62
|
-
limit: limit !== null && limit !== void 0 ? limit : constants_1.DAPI_DEFAULT_LIMIT,
|
|
63
|
-
startAt: (startAt != null) ? startAt.bytes() : undefined,
|
|
64
|
-
startAfter: (startAfter != null) ? startAfter.bytes() : undefined,
|
|
65
|
-
prove: true
|
|
66
|
-
}
|
|
67
|
-
});
|
|
68
|
-
return [4 /*yield*/, (0, getDataContractByIdentifier_1.default)(grpcPool, dataContractId)];
|
|
69
|
-
case 1:
|
|
70
|
-
dataContract = _e.sent();
|
|
71
|
-
return [4 /*yield*/, grpcPool.getClient().getDocuments(getDocumentsRequest)];
|
|
72
|
-
case 2:
|
|
73
|
-
v0 = (_e.sent()).v0;
|
|
74
|
-
_a = v0, proof = _a.proof, metadata = _a.metadata;
|
|
75
|
-
if (proof == null) {
|
|
76
|
-
throw new Error('Proof not found');
|
|
77
|
-
}
|
|
78
|
-
if (metadata == null) {
|
|
79
|
-
throw new Error('Metadata not found');
|
|
80
|
-
}
|
|
81
|
-
startAtIncluded = ((_c = getDocumentsRequest.v0) === null || _c === void 0 ? void 0 : _c.startAfter) == null;
|
|
82
|
-
_b = (0, pshenmic_dpp_1.verifyDocumentsProof)(proof.grovedbProof, dataContract, documentTypeName, where, orderBy, limit, (_d = getDocumentsRequest.v0) === null || _d === void 0 ? void 0 : _d.startAt, startAtIncluded, BigInt(metadata === null || metadata === void 0 ? void 0 : metadata.timeMs), pshenmic_dpp_1.PlatformVersionWASM.PLATFORM_V9), rootHash = _b.rootHash, documents = _b.documents;
|
|
83
|
-
return [4 /*yield*/, (0, getQuorumPublicKey_1.getQuorumPublicKey)(grpcPool.network, proof.quorumType, (0, bytesToHex_1.default)(proof.quorumHash))];
|
|
84
|
-
case 3:
|
|
85
|
-
quorumPublicKey = _e.sent();
|
|
86
|
-
verify = (0, verifyTenderdashProof_1.default)(proof, metadata, rootHash, quorumPublicKey);
|
|
87
|
-
if (!verify) {
|
|
88
|
-
throw new Error('Failed to verify query');
|
|
89
|
-
}
|
|
90
|
-
return [2 /*return*/, documents !== null && documents !== void 0 ? documents : []];
|
|
26
|
+
const getDocumentsRequest = GetDocumentsRequest.create({
|
|
27
|
+
version: {
|
|
28
|
+
oneofKind: 'v0',
|
|
29
|
+
v0: {
|
|
30
|
+
dataContractId: (new IdentifierWASM(dataContractId)).bytes(),
|
|
31
|
+
documentType: documentTypeName,
|
|
32
|
+
where: encode(where),
|
|
33
|
+
orderBy: encode(orderBy),
|
|
34
|
+
limit: limit ?? DAPI_DEFAULT_LIMIT,
|
|
35
|
+
start,
|
|
36
|
+
prove: true
|
|
91
37
|
}
|
|
92
|
-
}
|
|
38
|
+
}
|
|
93
39
|
});
|
|
40
|
+
const dataContract = await getDataContractByIdentifier(grpcPool, dataContractId);
|
|
41
|
+
const { response } = await grpcPool.getClient().getDocuments(getDocumentsRequest);
|
|
42
|
+
const { version } = response;
|
|
43
|
+
if (version.oneofKind !== 'v0') {
|
|
44
|
+
throw new Error('Unexpected oneOf type returned from DAPI (must be v0)');
|
|
45
|
+
}
|
|
46
|
+
const { v0 } = version;
|
|
47
|
+
if (v0.result.oneofKind !== 'proof') {
|
|
48
|
+
throw new Error('Unexpected oneOf type returned from DAPI (must be proof)');
|
|
49
|
+
}
|
|
50
|
+
const { result: { proof }, metadata } = v0;
|
|
51
|
+
if (metadata == null) {
|
|
52
|
+
throw new Error('Metadata not found');
|
|
53
|
+
}
|
|
54
|
+
const startAtIncluded = startAt != null;
|
|
55
|
+
const { rootHash, documents } = verifyDocumentsProof(proof.grovedbProof, dataContract, documentTypeName, where, orderBy, limit, startAt?.bytes(), startAtIncluded, BigInt(metadata?.timeMs), PlatformVersionWASM.PLATFORM_V9);
|
|
56
|
+
const quorumPublicKey = await getQuorumPublicKey(grpcPool.network, proof.quorumType, bytesToHex(proof.quorumHash));
|
|
57
|
+
const verify = await verifyTenderdashProof(proof, metadata, rootHash, quorumPublicKey);
|
|
58
|
+
if (!verify) {
|
|
59
|
+
throw new Error('Failed to verify query');
|
|
60
|
+
}
|
|
61
|
+
return documents ?? [];
|
|
94
62
|
}
|