@flarenetwork/flare-periphery-contract-artifacts 0.1.1 → 0.1.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/dist/coston/StateConnector/libs/ts/AttestationDefinitionStore.d.ts +68 -0
- package/dist/coston/StateConnector/libs/ts/AttestationDefinitionStore.d.ts.map +1 -0
- package/dist/coston/StateConnector/libs/ts/AttestationDefinitionStore.js +164 -0
- package/dist/coston/StateConnector/libs/ts/AttestationDefinitionStore.js.map +1 -0
- package/dist/coston/StateConnector/libs/ts/AttestationResponse.d.ts +33 -0
- package/dist/coston/StateConnector/libs/ts/AttestationResponse.d.ts.map +1 -0
- package/dist/coston/StateConnector/libs/ts/AttestationResponse.js +30 -0
- package/dist/coston/StateConnector/libs/ts/AttestationResponse.js.map +1 -0
- package/dist/coston/StateConnector/libs/ts/MerkleTree.d.ts +103 -0
- package/dist/coston/StateConnector/libs/ts/MerkleTree.d.ts.map +1 -0
- package/dist/coston/StateConnector/libs/ts/MerkleTree.js +212 -0
- package/dist/coston/StateConnector/libs/ts/MerkleTree.js.map +1 -0
- package/dist/coston/StateConnector/libs/ts/config-types.d.ts +143 -0
- package/dist/coston/StateConnector/libs/ts/config-types.d.ts.map +1 -0
- package/dist/coston/StateConnector/libs/ts/config-types.js +3 -0
- package/dist/coston/StateConnector/libs/ts/config-types.js.map +1 -0
- package/dist/coston/StateConnector/libs/ts/interfaces.d.ts +32 -0
- package/dist/coston/StateConnector/libs/ts/interfaces.d.ts.map +1 -0
- package/dist/coston/StateConnector/libs/ts/interfaces.js +3 -0
- package/dist/coston/StateConnector/libs/ts/interfaces.js.map +1 -0
- package/dist/coston/StateConnector/libs/ts/random.d.ts +9 -0
- package/dist/coston/StateConnector/libs/ts/random.d.ts.map +1 -0
- package/dist/coston/StateConnector/libs/ts/random.js +70 -0
- package/dist/coston/StateConnector/libs/ts/random.js.map +1 -0
- package/dist/coston/StateConnector/libs/ts/utils.d.ts +73 -0
- package/dist/coston/StateConnector/libs/ts/utils.d.ts.map +1 -0
- package/dist/coston/StateConnector/libs/ts/utils.js +282 -0
- package/dist/coston/StateConnector/libs/ts/utils.js.map +1 -0
- package/dist/coston/StateConnector/typescript/AddressValidity.d.ts +91 -0
- package/dist/coston/StateConnector/typescript/AddressValidity.d.ts.map +1 -0
- package/dist/coston/StateConnector/typescript/AddressValidity.js +9 -0
- package/dist/coston/StateConnector/typescript/AddressValidity.js.map +1 -0
- package/dist/coston/StateConnector/typescript/BalanceDecreasingTransaction.d.ts +107 -0
- package/dist/coston/StateConnector/typescript/BalanceDecreasingTransaction.d.ts.map +1 -0
- package/dist/coston/StateConnector/typescript/BalanceDecreasingTransaction.js +9 -0
- package/dist/coston/StateConnector/typescript/BalanceDecreasingTransaction.js.map +1 -0
- package/dist/coston/StateConnector/typescript/ConfirmedBlockHeightExists.d.ts +103 -0
- package/dist/coston/StateConnector/typescript/ConfirmedBlockHeightExists.d.ts.map +1 -0
- package/dist/coston/StateConnector/typescript/ConfirmedBlockHeightExists.js +9 -0
- package/dist/coston/StateConnector/typescript/ConfirmedBlockHeightExists.js.map +1 -0
- package/dist/coston/StateConnector/typescript/EVMTransaction.d.ts +160 -0
- package/dist/coston/StateConnector/typescript/EVMTransaction.d.ts.map +1 -0
- package/dist/coston/StateConnector/typescript/EVMTransaction.js +9 -0
- package/dist/coston/StateConnector/typescript/EVMTransaction.js.map +1 -0
- package/dist/coston/StateConnector/typescript/Payment.d.ts +139 -0
- package/dist/coston/StateConnector/typescript/Payment.d.ts.map +1 -0
- package/dist/coston/StateConnector/typescript/Payment.js +9 -0
- package/dist/coston/StateConnector/typescript/Payment.js.map +1 -0
- package/dist/coston/StateConnector/typescript/ReferencedPaymentNonexistence.d.ts +115 -0
- package/dist/coston/StateConnector/typescript/ReferencedPaymentNonexistence.d.ts.map +1 -0
- package/dist/coston/StateConnector/typescript/ReferencedPaymentNonexistence.js +9 -0
- package/dist/coston/StateConnector/typescript/ReferencedPaymentNonexistence.js.map +1 -0
- package/dist/coston/StateConnector/typescript/TypeTemplate.d.ts +146 -0
- package/dist/coston/StateConnector/typescript/TypeTemplate.d.ts.map +1 -0
- package/dist/coston/StateConnector/typescript/TypeTemplate.js +9 -0
- package/dist/coston/StateConnector/typescript/TypeTemplate.js.map +1 -0
- package/dist/coston/StateConnector/typescript/data-sources/data-sources.d.ts +29 -0
- package/dist/coston/StateConnector/typescript/data-sources/data-sources.d.ts.map +1 -0
- package/dist/coston/StateConnector/typescript/data-sources/data-sources.js +33 -0
- package/dist/coston/StateConnector/typescript/data-sources/data-sources.js.map +1 -0
- package/dist/coston/StateConnector/typescript/index.d.ts +9 -0
- package/dist/coston/StateConnector/typescript/index.d.ts.map +1 -0
- package/dist/coston/StateConnector/typescript/index.js +25 -0
- package/dist/coston/StateConnector/typescript/index.js.map +1 -0
- package/dist/coston/deploys/mapper.js.map +1 -1
- package/dist/coston2/deploys/mapper.js.map +1 -1
- package/dist/flare/deploys/mapper.js.map +1 -1
- package/dist/songbird/deploys/mapper.js.map +1 -1
- package/package.json +3 -2
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { TypeRecord } from "./config-types";
|
|
2
|
+
import { ethers } from "ethers";
|
|
3
|
+
import { ARBase, ARESBase } from "./interfaces";
|
|
4
|
+
/**
|
|
5
|
+
* Attestation definition store. Contains all the attestation type definitions
|
|
6
|
+
* that are contained in the folder, from which the store was initialized.
|
|
7
|
+
*/
|
|
8
|
+
export declare class AttestationDefinitionStore {
|
|
9
|
+
definitions: Map<string, TypeRecord>;
|
|
10
|
+
coder: ethers.AbiCoder;
|
|
11
|
+
constructor(configsPath?: string);
|
|
12
|
+
/**
|
|
13
|
+
* Initializes the store by reading the attestation type definition configs from the
|
|
14
|
+
* provided path.
|
|
15
|
+
* @param configsPath
|
|
16
|
+
*/
|
|
17
|
+
private initialize;
|
|
18
|
+
/**
|
|
19
|
+
* Returns the attestation type definition for the provided attestation type id.
|
|
20
|
+
* @param attestationType
|
|
21
|
+
* @returns
|
|
22
|
+
*/
|
|
23
|
+
getDefinitionForDecodedAttestationType(attestationType: string): TypeRecord | undefined;
|
|
24
|
+
getABIsForDecodedAttestationType(attestationTypeId: string): {
|
|
25
|
+
requestAbi: any;
|
|
26
|
+
responseAbi: any;
|
|
27
|
+
proofAbi: any;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Calculated usual or salted hash of the attestation response.
|
|
31
|
+
* The function throws an error if an attestation type is not supported or
|
|
32
|
+
* the response does not match the provided ABI definition.
|
|
33
|
+
* @param response
|
|
34
|
+
* @param salt
|
|
35
|
+
* @returns
|
|
36
|
+
*/
|
|
37
|
+
attestationResponseHash<T extends ARESBase>(response: T, salt?: string): string;
|
|
38
|
+
/**
|
|
39
|
+
* Extracts the attestation type prefix from the provided attestation request.
|
|
40
|
+
* The prefix consists of 3 x 32 bytes: attestation type, source id and message integrity code.
|
|
41
|
+
* The rest of the bytes define ABI encoded request body.
|
|
42
|
+
* @param bytes
|
|
43
|
+
* @param decodeAttestationTypeName
|
|
44
|
+
* @returns
|
|
45
|
+
*/
|
|
46
|
+
static extractPrefixFromRequest(bytes: string, decodeAttestationTypeName?: boolean): ARBase;
|
|
47
|
+
/**
|
|
48
|
+
* Encodes attestation request.
|
|
49
|
+
* The request must have the attestation type encoded into 32-byte 0x-prefixed string.
|
|
50
|
+
* @param request
|
|
51
|
+
* @returns
|
|
52
|
+
*/
|
|
53
|
+
encodeRequest(request: ARBase): string;
|
|
54
|
+
/**
|
|
55
|
+
* Parses attestation request.
|
|
56
|
+
* @param bytes
|
|
57
|
+
* @returns
|
|
58
|
+
*/
|
|
59
|
+
parseRequest<AR extends ARBase>(bytes: string, decodeAttestationTypeName?: boolean): AR;
|
|
60
|
+
/**
|
|
61
|
+
* Compares two attestation requests.
|
|
62
|
+
* @param request1
|
|
63
|
+
* @param request2
|
|
64
|
+
* @returns
|
|
65
|
+
*/
|
|
66
|
+
equalsRequest(request1: ARBase, request2: ARBase): boolean;
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=AttestationDefinitionStore.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AttestationDefinitionStore.d.ts","sourceRoot":"","sources":["../../../../../coston/StateConnector/libs/ts/AttestationDefinitionStore.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAC5C,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAYhD;;;GAGG;AACH,qBAAa,0BAA0B;IACnC,WAAW,EAAG,GAAG,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;IACtC,KAAK,EAAG,MAAM,CAAC,QAAQ,CAAC;gBAEZ,WAAW,SAAwC;IAG/D;;;;OAIG;IACH,OAAO,CAAC,UAAU;IAKlB;;;;OAIG;IACH,sCAAsC,CAAC,eAAe,EAAE,MAAM;IAI9D,gCAAgC,CAAC,iBAAiB,EAAE,MAAM;;;;;IAY1D;;;;;;;OAOG;IACH,uBAAuB,CAAC,CAAC,SAAS,QAAQ,EAAE,QAAQ,EAAE,CAAC,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM;IAe/E;;;;;;;OAOG;IACH,MAAM,CAAC,wBAAwB,CAAC,KAAK,EAAE,MAAM,EAAE,yBAAyB,UAAQ,GAAG,MAAM;IAsBzF;;;;;OAKG;IACH,aAAa,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM;IAoBtC;;;;OAIG;IACH,YAAY,CAAC,EAAE,SAAS,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,yBAAyB,UAAQ,GAAG,EAAE;IAqBrF;;;;;OAKG;IACH,aAAa,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO;CAc7D"}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AttestationDefinitionStore = void 0;
|
|
4
|
+
const ethers_1 = require("ethers");
|
|
5
|
+
const utils_1 = require("./utils");
|
|
6
|
+
/**
|
|
7
|
+
* Attestation definition store. Contains all the attestation type definitions
|
|
8
|
+
* that are contained in the folder, from which the store was initialized.
|
|
9
|
+
*/
|
|
10
|
+
class AttestationDefinitionStore {
|
|
11
|
+
constructor(configsPath = utils_1.DEFAULT_ATTESTATION_TYPE_CONFIGS_PATH) {
|
|
12
|
+
this.initialize(configsPath);
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Initializes the store by reading the attestation type definition configs from the
|
|
16
|
+
* provided path.
|
|
17
|
+
* @param configsPath
|
|
18
|
+
*/
|
|
19
|
+
initialize(configsPath) {
|
|
20
|
+
this.definitions = (0, utils_1.readAttestationTypeConfigs)(configsPath);
|
|
21
|
+
this.coder = ethers_1.ethers.AbiCoder.defaultAbiCoder();
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Returns the attestation type definition for the provided attestation type id.
|
|
25
|
+
* @param attestationType
|
|
26
|
+
* @returns
|
|
27
|
+
*/
|
|
28
|
+
getDefinitionForDecodedAttestationType(attestationType) {
|
|
29
|
+
return this.definitions.get(attestationType);
|
|
30
|
+
}
|
|
31
|
+
getABIsForDecodedAttestationType(attestationTypeId) {
|
|
32
|
+
const definition = this.getDefinitionForDecodedAttestationType(attestationTypeId);
|
|
33
|
+
if (!definition) {
|
|
34
|
+
throw new Error(`Unsupported attestation type id: '${attestationTypeId}'`);
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
requestAbi: definition.requestAbi,
|
|
38
|
+
responseAbi: definition.responseAbi,
|
|
39
|
+
proofAbi: definition.proofAbi,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Calculated usual or salted hash of the attestation response.
|
|
44
|
+
* The function throws an error if an attestation type is not supported or
|
|
45
|
+
* the response does not match the provided ABI definition.
|
|
46
|
+
* @param response
|
|
47
|
+
* @param salt
|
|
48
|
+
* @returns
|
|
49
|
+
*/
|
|
50
|
+
attestationResponseHash(response, salt) {
|
|
51
|
+
const attestationType = (0, utils_1.decodeAttestationName)(response.attestationType);
|
|
52
|
+
const definition = this.getDefinitionForDecodedAttestationType(attestationType);
|
|
53
|
+
if (!definition) {
|
|
54
|
+
throw new Error(`Unsupported attestation type id: '${attestationType}'`);
|
|
55
|
+
}
|
|
56
|
+
let abiEncoded;
|
|
57
|
+
if (salt) {
|
|
58
|
+
abiEncoded = this.coder.encode([definition.responseAbi, "string"], [response, salt]);
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
abiEncoded = this.coder.encode([definition.responseAbi], [response]);
|
|
62
|
+
}
|
|
63
|
+
return ethers_1.ethers.keccak256(abiEncoded);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Extracts the attestation type prefix from the provided attestation request.
|
|
67
|
+
* The prefix consists of 3 x 32 bytes: attestation type, source id and message integrity code.
|
|
68
|
+
* The rest of the bytes define ABI encoded request body.
|
|
69
|
+
* @param bytes
|
|
70
|
+
* @param decodeAttestationTypeName
|
|
71
|
+
* @returns
|
|
72
|
+
*/
|
|
73
|
+
static extractPrefixFromRequest(bytes, decodeAttestationTypeName = false) {
|
|
74
|
+
if (!bytes) {
|
|
75
|
+
throw new Error("Empty attestation request");
|
|
76
|
+
}
|
|
77
|
+
if (!bytes.match(/^0x[0-9a-fA-F]*$/)) {
|
|
78
|
+
throw new Error("Not a '0x'-prefixed hex string");
|
|
79
|
+
}
|
|
80
|
+
if (bytes.length < 3 * 64 + 2) {
|
|
81
|
+
throw new Error("Incorrectly formatted attestation request");
|
|
82
|
+
}
|
|
83
|
+
let attestationType = bytes.slice(0, 2 + 64);
|
|
84
|
+
if (decodeAttestationTypeName) {
|
|
85
|
+
attestationType = (0, utils_1.decodeAttestationName)(attestationType);
|
|
86
|
+
}
|
|
87
|
+
return {
|
|
88
|
+
attestationType,
|
|
89
|
+
sourceId: "0x" + bytes.slice(2 + 64, 2 + 2 * 64),
|
|
90
|
+
messageIntegrityCode: "0x" + bytes.slice(2 + 2 * 64, 2 + 3 * 64),
|
|
91
|
+
requestBody: "0x" + bytes.slice(2 + 3 * 64),
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Encodes attestation request.
|
|
96
|
+
* The request must have the attestation type encoded into 32-byte 0x-prefixed string.
|
|
97
|
+
* @param request
|
|
98
|
+
* @returns
|
|
99
|
+
*/
|
|
100
|
+
encodeRequest(request) {
|
|
101
|
+
const attestationType = (0, utils_1.decodeAttestationName)(request.attestationType);
|
|
102
|
+
const definition = this.getDefinitionForDecodedAttestationType(attestationType);
|
|
103
|
+
if (!definition) {
|
|
104
|
+
throw new Error(`Unsupported attestation type id: '${request.attestationType}'`);
|
|
105
|
+
}
|
|
106
|
+
// custom encoding for the prefix
|
|
107
|
+
const abiEncodePrefix = this.coder.encode(["bytes32", "bytes32", "bytes32"], [request.attestationType, request.sourceId, request.messageIntegrityCode || utils_1.ZERO_BYTES_32]);
|
|
108
|
+
// ABI encoding for the request body
|
|
109
|
+
const requestBodyAbi = definition.requestAbi.components.find((item) => item.name == "requestBody");
|
|
110
|
+
if (!requestBodyAbi) {
|
|
111
|
+
throw new Error(`Invalid request ABI for attestation type id: '${request.attestationType}'. No 'requestBody'.`);
|
|
112
|
+
}
|
|
113
|
+
const abiEncodeBody = this.coder.encode([requestBodyAbi], [request.requestBody]);
|
|
114
|
+
return ethers_1.ethers.concat([abiEncodePrefix, abiEncodeBody]);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Parses attestation request.
|
|
118
|
+
* @param bytes
|
|
119
|
+
* @returns
|
|
120
|
+
*/
|
|
121
|
+
parseRequest(bytes, decodeAttestationTypeName = false) {
|
|
122
|
+
var _a;
|
|
123
|
+
const prefix = AttestationDefinitionStore.extractPrefixFromRequest(bytes);
|
|
124
|
+
const attestationType = (0, utils_1.decodeAttestationName)(prefix.attestationType);
|
|
125
|
+
const definition = this.getDefinitionForDecodedAttestationType(attestationType);
|
|
126
|
+
if (!definition) {
|
|
127
|
+
throw new Error(`Unsupported attestation type id: '${attestationType}'`);
|
|
128
|
+
}
|
|
129
|
+
const requestBodyAbi = (_a = definition.requestAbi) === null || _a === void 0 ? void 0 : _a.components.find((item) => item.name == "requestBody");
|
|
130
|
+
if (!requestBodyAbi) {
|
|
131
|
+
throw new Error(`Invalid request ABI for attestation type id: '${prefix.attestationType}'. No 'requestBody'.`);
|
|
132
|
+
}
|
|
133
|
+
const parsed = this.coder.decode([requestBodyAbi], "0x" + bytes.slice(2 + 3 * 64))[0];
|
|
134
|
+
return (0, utils_1.serializeBigInts)({
|
|
135
|
+
attestationType: decodeAttestationTypeName ? definition : prefix.attestationType,
|
|
136
|
+
sourceId: prefix.sourceId,
|
|
137
|
+
messageIntegrityCode: prefix.messageIntegrityCode,
|
|
138
|
+
requestBody: (0, utils_1.remapABIParsedToObjects)(parsed, requestBodyAbi),
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Compares two attestation requests.
|
|
143
|
+
* @param request1
|
|
144
|
+
* @param request2
|
|
145
|
+
* @returns
|
|
146
|
+
*/
|
|
147
|
+
equalsRequest(request1, request2) {
|
|
148
|
+
var _a;
|
|
149
|
+
if (request1.attestationType !== request2.attestationType) {
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
if (request1.sourceId != request2.sourceId) {
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
const attestationType = (0, utils_1.decodeAttestationName)(request1.attestationType);
|
|
156
|
+
const requestAbi = (_a = this.getDefinitionForDecodedAttestationType(attestationType)) === null || _a === void 0 ? void 0 : _a.requestAbi;
|
|
157
|
+
if (!requestAbi) {
|
|
158
|
+
throw new Error(`Unsupported attestation type id: '${attestationType}'`);
|
|
159
|
+
}
|
|
160
|
+
return (0, utils_1.structsDeepEqual)(request1, request2, requestAbi);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
exports.AttestationDefinitionStore = AttestationDefinitionStore;
|
|
164
|
+
//# sourceMappingURL=AttestationDefinitionStore.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AttestationDefinitionStore.js","sourceRoot":"","sources":["../../../../../coston/StateConnector/libs/ts/AttestationDefinitionStore.ts"],"names":[],"mappings":";;;AACA,mCAAgC;AAEhC,mCASiB;AAEjB;;;GAGG;AACH,MAAa,0BAA0B;IAInC,YAAY,WAAW,GAAG,6CAAqC;QAC3D,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC;IACjC,CAAC;IACD;;;;OAIG;IACK,UAAU,CAAC,WAAmB;QAClC,IAAI,CAAC,WAAW,GAAG,IAAA,kCAA0B,EAAC,WAAW,CAAC,CAAC;QAC3D,IAAI,CAAC,KAAK,GAAG,eAAM,CAAC,QAAQ,CAAC,eAAe,EAAE,CAAC;IACnD,CAAC;IAED;;;;OAIG;IACH,sCAAsC,CAAC,eAAuB;QAC1D,OAAO,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC;IACjD,CAAC;IAED,gCAAgC,CAAC,iBAAyB;QACtD,MAAM,UAAU,GAAG,IAAI,CAAC,sCAAsC,CAAC,iBAAiB,CAAC,CAAC;QAClF,IAAI,CAAC,UAAU,EAAE,CAAC;YACd,MAAM,IAAI,KAAK,CAAC,qCAAqC,iBAAiB,GAAG,CAAC,CAAC;QAC/E,CAAC;QACD,OAAO;YACH,UAAU,EAAE,UAAU,CAAC,UAAU;YACjC,WAAW,EAAE,UAAU,CAAC,WAAW;YACnC,QAAQ,EAAE,UAAU,CAAC,QAAQ;SAChC,CAAC;IACN,CAAC;IAED;;;;;;;OAOG;IACH,uBAAuB,CAAqB,QAAW,EAAE,IAAa;QAClE,MAAM,eAAe,GAAG,IAAA,6BAAqB,EAAC,QAAQ,C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B,oBAAoB,EAAE,MAAM,CAAC,oBAAoB;YACjD,WAAW,EAAE,IAAA,+BAAuB,EAAC,MAAM,EAAE,cAAc,CAAC;SAC/D,CAAO,CAAC;IACb,CAAC;IAED;;;;;OAKG;IACH,aAAa,CAAC,QAAgB,EAAE,QAAgB;;QAC5C,IAAI,QAAQ,CAAC,eAAe,KAAK,QAAQ,CAAC,eAAe,EAAE,CAAC;YACxD,OAAO,KAAK,CAAC;QACjB,CAAC;QACD,IAAI,QAAQ,CAAC,QAAQ,IAAI,QAAQ,CAAC,QAAQ,EAAE,CAAC;YACzC,OAAO,KAAK,CAAC;QACjB,CAAC;QACD,MAAM,eAAe,GAAG,IAAA,6BAAqB,EAAC,QAAQ,CAAC,eAAe,CAAC,CAAC;QACxE,MAAM,UAAU,GAAG,MAAA,IAAI,CAAC,sCAAsC,CAAC,eAAe,CAAC,0CAAE,UAAyB,CAAC;QAC3G,IAAI,CAAC,UAAU,EAAE,CAAC;YACd,MAAM,IAAI,KAAK,CAAC,qCAAqC,eAAe,GAAG,CAAC,CAAC;QAC7E,CAAC;QACD,OAAO,IAAA,wBAAgB,EAAC,QAAQ,EAAE,QAAQ,EAAE,UAAU,CAAC,CAAC;IAC5D,CAAC;CACJ;AAnKD,gEAmKC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Attestation status
|
|
3
|
+
*/
|
|
4
|
+
export declare enum AttestationResponseStatus {
|
|
5
|
+
/**
|
|
6
|
+
* Attestation request is valid.
|
|
7
|
+
*/
|
|
8
|
+
VALID = "VALID",
|
|
9
|
+
/**
|
|
10
|
+
* Attestation request is invalid.
|
|
11
|
+
*/
|
|
12
|
+
INVALID = "INVALID",
|
|
13
|
+
/**
|
|
14
|
+
* Attestation request cannot be confirmed neither rejected by the verifier at the moment.
|
|
15
|
+
*/
|
|
16
|
+
INDETERMINATE = "INDETERMINATE"
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Object returned as a result of attestation.
|
|
20
|
+
* If status is 'VALID' then parameters @param response contains attestation response.
|
|
21
|
+
* Otherwise, @param response is undefined.
|
|
22
|
+
*/
|
|
23
|
+
export declare class AttestationResponse<RES> {
|
|
24
|
+
/**
|
|
25
|
+
* Verification status.
|
|
26
|
+
*/
|
|
27
|
+
status: AttestationResponseStatus;
|
|
28
|
+
/**
|
|
29
|
+
* Attestation response.
|
|
30
|
+
*/
|
|
31
|
+
response?: RES;
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=AttestationResponse.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AttestationResponse.d.ts","sourceRoot":"","sources":["../../../../../coston/StateConnector/libs/ts/AttestationResponse.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,oBAAY,yBAAyB;IACjC;;OAEG;IACH,KAAK,UAAU;IACf;;OAEG;IACH,OAAO,YAAY;IACnB;;OAEG;IACH,aAAa,kBAAkB;CAClC;AACD;;;;GAIG;AAEH,qBAAa,mBAAmB,CAAC,GAAG;IAChC;;OAEG;IACH,MAAM,EAAG,yBAAyB,CAAC;IACnC;;OAEG;IACH,QAAQ,CAAC,EAAE,GAAG,CAAC;CAClB"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AttestationResponse = exports.AttestationResponseStatus = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Attestation status
|
|
6
|
+
*/
|
|
7
|
+
var AttestationResponseStatus;
|
|
8
|
+
(function (AttestationResponseStatus) {
|
|
9
|
+
/**
|
|
10
|
+
* Attestation request is valid.
|
|
11
|
+
*/
|
|
12
|
+
AttestationResponseStatus["VALID"] = "VALID";
|
|
13
|
+
/**
|
|
14
|
+
* Attestation request is invalid.
|
|
15
|
+
*/
|
|
16
|
+
AttestationResponseStatus["INVALID"] = "INVALID";
|
|
17
|
+
/**
|
|
18
|
+
* Attestation request cannot be confirmed neither rejected by the verifier at the moment.
|
|
19
|
+
*/
|
|
20
|
+
AttestationResponseStatus["INDETERMINATE"] = "INDETERMINATE";
|
|
21
|
+
})(AttestationResponseStatus || (exports.AttestationResponseStatus = AttestationResponseStatus = {}));
|
|
22
|
+
/**
|
|
23
|
+
* Object returned as a result of attestation.
|
|
24
|
+
* If status is 'VALID' then parameters @param response contains attestation response.
|
|
25
|
+
* Otherwise, @param response is undefined.
|
|
26
|
+
*/
|
|
27
|
+
class AttestationResponse {
|
|
28
|
+
}
|
|
29
|
+
exports.AttestationResponse = AttestationResponse;
|
|
30
|
+
//# sourceMappingURL=AttestationResponse.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AttestationResponse.js","sourceRoot":"","sources":["../../../../../coston/StateConnector/libs/ts/AttestationResponse.ts"],"names":[],"mappings":";;;AAAA;;GAEG;AACH,IAAY,yBAaX;AAbD,WAAY,yBAAyB;IACjC;;OAEG;IACH,4CAAe,CAAA;IACf;;OAEG;IACH,gDAAmB,CAAA;IACnB;;OAEG;IACH,4DAA+B,CAAA;AACnC,CAAC,EAbW,yBAAyB,yCAAzB,yBAAyB,QAapC;AACD;;;;GAIG;AAEH,MAAa,mBAAmB;CAS/B;AATD,kDASC"}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* There are several variants for hashing sequences in Merkle trees in cases when there is odd number of hashes on some level.
|
|
3
|
+
* - Bitcoin hashes remaining hash with itself
|
|
4
|
+
* - Ethereum community was considering this:
|
|
5
|
+
* https://github.com/proofchains/python-proofmarshal/blob/efe9b58921b9a306f2b3552c30b84e1043ab866f/proofmarshal/mmr.py#L96
|
|
6
|
+
* - This review shows various options and in particular it describes the "Monero way", which gives balanced trees but they
|
|
7
|
+
* had some issues with bugs
|
|
8
|
+
* https://medium.com/coinmonks/merkle-trees-concepts-and-use-cases-5da873702318
|
|
9
|
+
*
|
|
10
|
+
* The current implementation is a derivation and simplification of "Monero" way. It uses ideas
|
|
11
|
+
* from binary heaps represented in array. This significantly simplifies the construction both of a Merkle tree and a proof.
|
|
12
|
+
*
|
|
13
|
+
* Important formulas for a left-aligned full tree represented in an array for n hashes as leafs
|
|
14
|
+
* - array has exactly 2*n - 1 nodes (n leafs other internal)
|
|
15
|
+
* - array[0] is merkle root, array[n-1, ..., 2*n - 2] contains leaf hashes in order
|
|
16
|
+
* - given tree array of length l = 2*n - 1, then n = floor((l + 1)/2) --- be careful with 1 element
|
|
17
|
+
* - parent(i) = Math.floor((i - 1) / 2);
|
|
18
|
+
* - left(i) = 2*i + 1
|
|
19
|
+
* - right(i) = 2*i + 2
|
|
20
|
+
*
|
|
21
|
+
* Importants: all input strings should represent bytes32, hence should be 32-byte padded hex strings.
|
|
22
|
+
*/
|
|
23
|
+
/**
|
|
24
|
+
* Single value hash function. Uses keccak function compatible with the one used in Solidity
|
|
25
|
+
* It is supposed to be used with `0x`-prefixed 32-byte strings as hex values
|
|
26
|
+
* @param val input hash in form of the string `0x` prefixed.
|
|
27
|
+
* @returns hash of the input value
|
|
28
|
+
*/
|
|
29
|
+
export declare function singleHash(val: string): string;
|
|
30
|
+
/**
|
|
31
|
+
* Helper function for constructing the commit hash used with StateConnector.sol contract
|
|
32
|
+
* @param merkleRoot merkle root (`0x`-prefixed 32-byte hex string)
|
|
33
|
+
* @param randomNumber secure random number (`0x`-prefixed 32-byte hex string)
|
|
34
|
+
* @param address addres (`0x`-prefixed 20-byte hex string)
|
|
35
|
+
* @returns `0x`-prefixed 32-byte hex string (hash)
|
|
36
|
+
*/
|
|
37
|
+
export declare function commitHash(merkleRoot: string, randomNumber: string, address: string): string;
|
|
38
|
+
/**
|
|
39
|
+
* A sorted hash of two 32-byte strings
|
|
40
|
+
* @param x first `0x`-prefixed 32-byte hex string
|
|
41
|
+
* @param y second `0x`-prefixed 32-byte hex string
|
|
42
|
+
* @returns the sorted hash
|
|
43
|
+
*/
|
|
44
|
+
export declare function sortedHashPair(x: string, y: string): string;
|
|
45
|
+
/**
|
|
46
|
+
* Merkle tree implementation with all the helper function for constructing the tree and extracting the root and proofs for every leaf.
|
|
47
|
+
*/
|
|
48
|
+
export declare class MerkleTree {
|
|
49
|
+
_tree: string[];
|
|
50
|
+
constructor(values: string[]);
|
|
51
|
+
/**
|
|
52
|
+
* Merkle root
|
|
53
|
+
*/
|
|
54
|
+
get root(): string | undefined;
|
|
55
|
+
/**
|
|
56
|
+
* The array representing full tree (length is `2*hashCount - 1`)
|
|
57
|
+
*/
|
|
58
|
+
get tree(): string[];
|
|
59
|
+
/**
|
|
60
|
+
* Number of leaves in the Merkle tree
|
|
61
|
+
*/
|
|
62
|
+
get hashCount(): number;
|
|
63
|
+
/**
|
|
64
|
+
* Returns leaves in array of the length `hashCount` sorted as `0x`-prefixed 32-byte hex strings.
|
|
65
|
+
*/
|
|
66
|
+
get sortedHashes(): string[];
|
|
67
|
+
/**
|
|
68
|
+
* Parent index of the node at index `i` in array
|
|
69
|
+
* @param i index of a node in the Merkle tree
|
|
70
|
+
* @returns parent index
|
|
71
|
+
*/
|
|
72
|
+
parent(i: number): number;
|
|
73
|
+
/**
|
|
74
|
+
* Given an array of leave hashes (`0x`-prefixed 32-byte hex strings) it builds the Merkle tree.
|
|
75
|
+
* @param values
|
|
76
|
+
*/
|
|
77
|
+
private build;
|
|
78
|
+
/**
|
|
79
|
+
* Returns the hash of the `i`-th leaf (index determined by sorting and positioning in the build)
|
|
80
|
+
* @param i
|
|
81
|
+
* @returns
|
|
82
|
+
*/
|
|
83
|
+
getHash(i: number): string | undefined;
|
|
84
|
+
/** Binary search
|
|
85
|
+
* Famously prone to subtle bugs, so over-documented with proof
|
|
86
|
+
*/
|
|
87
|
+
private binarySearch;
|
|
88
|
+
/**
|
|
89
|
+
* Extracts the Merkle proof for the given hash, if it is in the tree
|
|
90
|
+
* @param hashOrIndex the hash whose proof to return
|
|
91
|
+
* @returns the Merkle proof - a list of `0x`-prefixed 32-byte hex strings
|
|
92
|
+
*/
|
|
93
|
+
getProof(hashOrIndex: string | number): string[] | undefined;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Verifies a Merkle proof for a given leaf
|
|
97
|
+
* @param leaf leaf as (`0x`-prefixed 32-byte hex string)
|
|
98
|
+
* @param proof Merkle proof (a list of `0x`-prefixed 32-byte hex strings)
|
|
99
|
+
* @param root Merkle root (`0x`-prefixed 32-byte hex string)
|
|
100
|
+
* @returns `true` if the proof is valid, `false` otherwise
|
|
101
|
+
*/
|
|
102
|
+
export declare function verifyWithMerkleProof(leaf: string, proof: string[], root: string): boolean;
|
|
103
|
+
//# sourceMappingURL=MerkleTree.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MerkleTree.d.ts","sourceRoot":"","sources":["../../../../../coston/StateConnector/libs/ts/MerkleTree.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAMH;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,UAErC;AAED;;;;;;GAMG;AACH,wBAAgB,UAAU,CAAC,UAAU,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,CAE5F;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,UAKlD;AAED;;GAEG;AACH,qBAAa,UAAU;IACnB,KAAK,EAAE,MAAM,EAAE,CAAM;gBAET,MAAM,EAAE,MAAM,EAAE;IAI5B;;OAEG;IACH,IAAW,IAAI,uBAEd;IAED;;OAEG;IACH,IAAW,IAAI,IAAI,MAAM,EAAE,CAE1B;IAED;;OAEG;IACH,IAAW,SAAS,WAEnB;IAED;;OAEG;IACH,IAAW,YAAY,aAEtB;IAED;;;;OAIG;IACI,MAAM,CAAC,CAAC,EAAE,MAAM;IAIvB;;;OAGG;IACH,OAAO,CAAC,KAAK;IAsBb;;;;OAIG;IACI,OAAO,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS;IAQ7C;;OAEG;IACH,OAAO,CAAC,YAAY;IAepB;;;;OAIG;IACI,QAAQ,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS;CA8BtE;AAED;;;;;;GAMG;AACH,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,MAAM,WAOhF"}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* There are several variants for hashing sequences in Merkle trees in cases when there is odd number of hashes on some level.
|
|
4
|
+
* - Bitcoin hashes remaining hash with itself
|
|
5
|
+
* - Ethereum community was considering this:
|
|
6
|
+
* https://github.com/proofchains/python-proofmarshal/blob/efe9b58921b9a306f2b3552c30b84e1043ab866f/proofmarshal/mmr.py#L96
|
|
7
|
+
* - This review shows various options and in particular it describes the "Monero way", which gives balanced trees but they
|
|
8
|
+
* had some issues with bugs
|
|
9
|
+
* https://medium.com/coinmonks/merkle-trees-concepts-and-use-cases-5da873702318
|
|
10
|
+
*
|
|
11
|
+
* The current implementation is a derivation and simplification of "Monero" way. It uses ideas
|
|
12
|
+
* from binary heaps represented in array. This significantly simplifies the construction both of a Merkle tree and a proof.
|
|
13
|
+
*
|
|
14
|
+
* Important formulas for a left-aligned full tree represented in an array for n hashes as leafs
|
|
15
|
+
* - array has exactly 2*n - 1 nodes (n leafs other internal)
|
|
16
|
+
* - array[0] is merkle root, array[n-1, ..., 2*n - 2] contains leaf hashes in order
|
|
17
|
+
* - given tree array of length l = 2*n - 1, then n = floor((l + 1)/2) --- be careful with 1 element
|
|
18
|
+
* - parent(i) = Math.floor((i - 1) / 2);
|
|
19
|
+
* - left(i) = 2*i + 1
|
|
20
|
+
* - right(i) = 2*i + 2
|
|
21
|
+
*
|
|
22
|
+
* Importants: all input strings should represent bytes32, hence should be 32-byte padded hex strings.
|
|
23
|
+
*/
|
|
24
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
|
+
exports.verifyWithMerkleProof = exports.MerkleTree = exports.sortedHashPair = exports.commitHash = exports.singleHash = void 0;
|
|
26
|
+
const ethers_1 = require("ethers");
|
|
27
|
+
const coder = ethers_1.ethers.AbiCoder.defaultAbiCoder();
|
|
28
|
+
/**
|
|
29
|
+
* Single value hash function. Uses keccak function compatible with the one used in Solidity
|
|
30
|
+
* It is supposed to be used with `0x`-prefixed 32-byte strings as hex values
|
|
31
|
+
* @param val input hash in form of the string `0x` prefixed.
|
|
32
|
+
* @returns hash of the input value
|
|
33
|
+
*/
|
|
34
|
+
function singleHash(val) {
|
|
35
|
+
return ethers_1.ethers.keccak256(val);
|
|
36
|
+
}
|
|
37
|
+
exports.singleHash = singleHash;
|
|
38
|
+
/**
|
|
39
|
+
* Helper function for constructing the commit hash used with StateConnector.sol contract
|
|
40
|
+
* @param merkleRoot merkle root (`0x`-prefixed 32-byte hex string)
|
|
41
|
+
* @param randomNumber secure random number (`0x`-prefixed 32-byte hex string)
|
|
42
|
+
* @param address addres (`0x`-prefixed 20-byte hex string)
|
|
43
|
+
* @returns `0x`-prefixed 32-byte hex string (hash)
|
|
44
|
+
*/
|
|
45
|
+
function commitHash(merkleRoot, randomNumber, address) {
|
|
46
|
+
return ethers_1.ethers.keccak256(coder.encode(["bytes32", "bytes32", "address"], [merkleRoot, randomNumber, address]));
|
|
47
|
+
}
|
|
48
|
+
exports.commitHash = commitHash;
|
|
49
|
+
/**
|
|
50
|
+
* A sorted hash of two 32-byte strings
|
|
51
|
+
* @param x first `0x`-prefixed 32-byte hex string
|
|
52
|
+
* @param y second `0x`-prefixed 32-byte hex string
|
|
53
|
+
* @returns the sorted hash
|
|
54
|
+
*/
|
|
55
|
+
function sortedHashPair(x, y) {
|
|
56
|
+
if (x <= y) {
|
|
57
|
+
return ethers_1.ethers.keccak256(coder.encode(["bytes32", "bytes32"], [x, y]));
|
|
58
|
+
}
|
|
59
|
+
return ethers_1.ethers.keccak256(coder.encode(["bytes32", "bytes32"], [y, x]));
|
|
60
|
+
}
|
|
61
|
+
exports.sortedHashPair = sortedHashPair;
|
|
62
|
+
/**
|
|
63
|
+
* Merkle tree implementation with all the helper function for constructing the tree and extracting the root and proofs for every leaf.
|
|
64
|
+
*/
|
|
65
|
+
class MerkleTree {
|
|
66
|
+
constructor(values) {
|
|
67
|
+
this._tree = [];
|
|
68
|
+
this.build(values);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Merkle root
|
|
72
|
+
*/
|
|
73
|
+
get root() {
|
|
74
|
+
return this._tree.length === 0 ? undefined : this._tree[0];
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* The array representing full tree (length is `2*hashCount - 1`)
|
|
78
|
+
*/
|
|
79
|
+
get tree() {
|
|
80
|
+
return [...this._tree];
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Number of leaves in the Merkle tree
|
|
84
|
+
*/
|
|
85
|
+
get hashCount() {
|
|
86
|
+
return this._tree.length ? (this._tree.length + 1) / 2 : 0;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Returns leaves in array of the length `hashCount` sorted as `0x`-prefixed 32-byte hex strings.
|
|
90
|
+
*/
|
|
91
|
+
get sortedHashes() {
|
|
92
|
+
return this._tree.slice(this.hashCount - 1);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Parent index of the node at index `i` in array
|
|
96
|
+
* @param i index of a node in the Merkle tree
|
|
97
|
+
* @returns parent index
|
|
98
|
+
*/
|
|
99
|
+
parent(i) {
|
|
100
|
+
return Math.floor((i - 1) / 2);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Given an array of leave hashes (`0x`-prefixed 32-byte hex strings) it builds the Merkle tree.
|
|
104
|
+
* @param values
|
|
105
|
+
*/
|
|
106
|
+
build(values) {
|
|
107
|
+
values.forEach((x) => {
|
|
108
|
+
if (!/^0x[0-9a-f]{64}$/i.test(x)) {
|
|
109
|
+
throw new Error(`Invalid hash '${x}'`);
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
const sorted = values.map((x) => x);
|
|
113
|
+
sorted.sort();
|
|
114
|
+
const hashes = [];
|
|
115
|
+
for (let i = 0; i < sorted.length; i++) {
|
|
116
|
+
if (i == 0 || sorted[i] !== sorted[i - 1]) {
|
|
117
|
+
hashes.push(sorted[i]);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
const n = hashes.length;
|
|
121
|
+
this._tree = [...new Array(Math.max(n - 1, 0)).fill(0), ...hashes];
|
|
122
|
+
for (let i = n - 2; i >= 0; i--) {
|
|
123
|
+
this._tree[i] = sortedHashPair(this._tree[2 * i + 1], this._tree[2 * i + 2]);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Returns the hash of the `i`-th leaf (index determined by sorting and positioning in the build)
|
|
128
|
+
* @param i
|
|
129
|
+
* @returns
|
|
130
|
+
*/
|
|
131
|
+
getHash(i) {
|
|
132
|
+
if (this.hashCount === 0 || i < 0 || i >= this.hashCount) {
|
|
133
|
+
return undefined;
|
|
134
|
+
}
|
|
135
|
+
const pos = this._tree.length - this.hashCount + i;
|
|
136
|
+
return this._tree[pos];
|
|
137
|
+
}
|
|
138
|
+
/** Binary search
|
|
139
|
+
* Famously prone to subtle bugs, so over-documented with proof
|
|
140
|
+
*/
|
|
141
|
+
binarySearch(hash) {
|
|
142
|
+
let [low, high] = [0, this.hashCount];
|
|
143
|
+
let count = high;
|
|
144
|
+
if (count == 0)
|
|
145
|
+
return undefined;
|
|
146
|
+
while (count > 1) {
|
|
147
|
+
// Invariants: low < high, 2 <= count == high - low == [low .. high].length
|
|
148
|
+
const mid = low + Math.floor(count / 2); // low < mid < high _strictly_
|
|
149
|
+
hash < this.sortedHashes[mid] ? (high = mid) : (low = mid); // low < high still
|
|
150
|
+
count = high - low; // preserves invariant
|
|
151
|
+
}
|
|
152
|
+
const i = low; // Only element left: count == 1, since 0 != count <= 1
|
|
153
|
+
if (hash != this.sortedHashes[i])
|
|
154
|
+
return undefined;
|
|
155
|
+
return i;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Extracts the Merkle proof for the given hash, if it is in the tree
|
|
159
|
+
* @param hashOrIndex the hash whose proof to return
|
|
160
|
+
* @returns the Merkle proof - a list of `0x`-prefixed 32-byte hex strings
|
|
161
|
+
*/
|
|
162
|
+
getProof(hashOrIndex) {
|
|
163
|
+
if (this.hashCount === 0) {
|
|
164
|
+
return undefined;
|
|
165
|
+
}
|
|
166
|
+
if (hashOrIndex === undefined)
|
|
167
|
+
return undefined;
|
|
168
|
+
let i;
|
|
169
|
+
if (typeof hashOrIndex === "number") {
|
|
170
|
+
i = hashOrIndex;
|
|
171
|
+
}
|
|
172
|
+
else if (typeof hashOrIndex === "string") {
|
|
173
|
+
if (/^0x[0-9a-f]{64}$/i.test(hashOrIndex)) {
|
|
174
|
+
i = this.binarySearch(hashOrIndex);
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
return undefined;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
if (i === undefined)
|
|
181
|
+
return undefined;
|
|
182
|
+
if (i < 0 || i >= this.hashCount) {
|
|
183
|
+
return undefined;
|
|
184
|
+
}
|
|
185
|
+
const proof = [];
|
|
186
|
+
let pos = this._tree.length - this.hashCount + i;
|
|
187
|
+
while (pos > 0) {
|
|
188
|
+
proof.push(this._tree[pos + 2 * (pos % 2) - 1]);
|
|
189
|
+
pos = this.parent(pos);
|
|
190
|
+
}
|
|
191
|
+
return proof;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
exports.MerkleTree = MerkleTree;
|
|
195
|
+
/**
|
|
196
|
+
* Verifies a Merkle proof for a given leaf
|
|
197
|
+
* @param leaf leaf as (`0x`-prefixed 32-byte hex string)
|
|
198
|
+
* @param proof Merkle proof (a list of `0x`-prefixed 32-byte hex strings)
|
|
199
|
+
* @param root Merkle root (`0x`-prefixed 32-byte hex string)
|
|
200
|
+
* @returns `true` if the proof is valid, `false` otherwise
|
|
201
|
+
*/
|
|
202
|
+
function verifyWithMerkleProof(leaf, proof, root) {
|
|
203
|
+
if (!leaf || !proof || !root)
|
|
204
|
+
return false;
|
|
205
|
+
let hash = leaf;
|
|
206
|
+
for (const pair of proof) {
|
|
207
|
+
hash = sortedHashPair(pair, hash);
|
|
208
|
+
}
|
|
209
|
+
return hash == root;
|
|
210
|
+
}
|
|
211
|
+
exports.verifyWithMerkleProof = verifyWithMerkleProof;
|
|
212
|
+
//# sourceMappingURL=MerkleTree.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MerkleTree.js","sourceRoot":"","sources":["../../../../../coston/StateConnector/libs/ts/MerkleTree.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;;;AAEH,mCAAgC;AAEhC,MAAM,KAAK,GAAG,eAAM,CAAC,QAAQ,CAAC,eAAe,EAAE,CAAC;AAEhD;;;;;GAKG;AACH,SAAgB,UAAU,CAAC,GAAW;IAClC,OAAO,eAAM,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;AACjC,CAAC;AAFD,gCAEC;AAED;;;;;;GAMG;AACH,SAAgB,UAAU,CAAC,UAAkB,EAAE,YAAoB,EAAE,OAAe;IAChF,OAAO,eAAM,CAAC,SAAS,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,SAAS,EAAE,SAAS,EAAE,SAAS,CAAC,EAAE,CAAC,UAAU,EAAE,YAAY,EAAE,OAAO,CAAC,CAAC,CAAE,CAAC;AACnH,CAAC;AAFD,gCAEC;AAED;;;;;GAKG;AACH,SAAgB,cAAc,CAAC,CAAS,EAAE,CAAS;IAC/C,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;QACT,OAAO,eAAM,CAAC,SAAS,CAAC,KAAK,CAAC,MAAM,CAAC,C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