@palliora.org/chainsdk 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +306 -114
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +414 -225
- package/dist/index.d.ts +414 -225
- package/dist/index.js +281 -89
- package/dist/index.js.map +1 -1
- package/package.json +3 -1
package/dist/index.js
CHANGED
|
@@ -230,7 +230,7 @@ var API_TYPES = {
|
|
|
230
230
|
pk_bytes: "Vec<u8>",
|
|
231
231
|
tau_params: "Vec<u8>"
|
|
232
232
|
},
|
|
233
|
-
|
|
233
|
+
ThresholdParams: {
|
|
234
234
|
_enum: {
|
|
235
235
|
SilentThreshold: "SilentThresholdParams"
|
|
236
236
|
}
|
|
@@ -241,24 +241,61 @@ var API_TYPES = {
|
|
|
241
241
|
Aes256GcmParams: {
|
|
242
242
|
nonce: "[u8; 12]"
|
|
243
243
|
},
|
|
244
|
-
|
|
244
|
+
SymmetricParams: {
|
|
245
245
|
_enum: {
|
|
246
246
|
ChaCha20Poly1305: "ChaCha20Poly1305Params",
|
|
247
247
|
Aes256Gcm: "Aes256GcmParams"
|
|
248
248
|
}
|
|
249
249
|
},
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
250
|
+
KdfParams: {
|
|
251
|
+
_enum: {
|
|
252
|
+
HkdfSha256: "Null",
|
|
253
|
+
HkdfSha512: "Null"
|
|
254
|
+
}
|
|
255
|
+
},
|
|
256
|
+
Secp256k1Params: {
|
|
257
|
+
recipient_public_key: "Vec<u8>",
|
|
258
|
+
ephemeral_public_key: "Option<Vec<u8>>",
|
|
259
|
+
compressed: "bool",
|
|
260
|
+
kdf: "KdfParams",
|
|
261
|
+
salt: "Option<Vec<u8>>",
|
|
262
|
+
info: "Option<Vec<u8>>"
|
|
263
|
+
},
|
|
264
|
+
Ed25519Params: {
|
|
265
|
+
recipient_public_key: "Vec<u8>",
|
|
266
|
+
ephemeral_public_key: "Option<Vec<u8>>",
|
|
267
|
+
kdf: "KdfParams",
|
|
268
|
+
salt: "Option<Vec<u8>>",
|
|
269
|
+
info: "Option<Vec<u8>>"
|
|
270
|
+
},
|
|
271
|
+
AsymmetricParams: {
|
|
272
|
+
_enum: {
|
|
273
|
+
Secp256k1: "Secp256k1Params",
|
|
274
|
+
Ed25519: "Ed25519Params"
|
|
275
|
+
}
|
|
276
|
+
},
|
|
277
|
+
ThresholdHybridParams: {
|
|
278
|
+
threshold_params: "ThresholdParams",
|
|
279
|
+
symmetric_params: "SymmetricParams"
|
|
280
|
+
},
|
|
281
|
+
AsymmetricHybridParams: {
|
|
282
|
+
asymmetric_params: "AsymmetricParams",
|
|
283
|
+
symmetric_params: "SymmetricParams"
|
|
253
284
|
},
|
|
254
285
|
CipherSuite: {
|
|
255
286
|
_enum: {
|
|
256
287
|
Plaintext: "Null",
|
|
257
|
-
|
|
288
|
+
ThresholdHybrid: "ThresholdHybridParams",
|
|
289
|
+
AsymmetricHybrid: "AsymmetricHybridParams"
|
|
258
290
|
}
|
|
259
291
|
},
|
|
260
292
|
ConfidentialityLevel: {
|
|
261
|
-
_enum:
|
|
293
|
+
_enum: {
|
|
294
|
+
Trusted: "u32",
|
|
295
|
+
TEE: "Null",
|
|
296
|
+
FHE: "Null",
|
|
297
|
+
SMPC: "Null"
|
|
298
|
+
}
|
|
262
299
|
},
|
|
263
300
|
NativeExecuteDA: {
|
|
264
301
|
_enum: ["Inference"]
|
|
@@ -284,6 +321,7 @@ var API_TYPES = {
|
|
|
284
321
|
},
|
|
285
322
|
DAInput: {
|
|
286
323
|
_enum: {
|
|
324
|
+
Null: "Null",
|
|
287
325
|
Inline: "DAInputInline",
|
|
288
326
|
ChainTransaction: "DAInputChainTransaction",
|
|
289
327
|
Ipfs: "DAInputIpfs",
|
|
@@ -294,16 +332,16 @@ var API_TYPES = {
|
|
|
294
332
|
},
|
|
295
333
|
ContractType: {
|
|
296
334
|
_enum: {
|
|
297
|
-
Dormant: "
|
|
298
|
-
Active: "
|
|
335
|
+
Dormant: "Null",
|
|
336
|
+
Active: "Null"
|
|
299
337
|
}
|
|
300
338
|
},
|
|
301
339
|
StoreType: {
|
|
302
340
|
_enum: {
|
|
303
|
-
Dataset: "
|
|
304
|
-
Model: "
|
|
305
|
-
Agent: "
|
|
306
|
-
Other: "
|
|
341
|
+
Dataset: "Null",
|
|
342
|
+
Model: "Null",
|
|
343
|
+
Agent: "Null",
|
|
344
|
+
Other: "Null"
|
|
307
345
|
}
|
|
308
346
|
},
|
|
309
347
|
ComputeMetadata: {
|
|
@@ -929,11 +967,48 @@ var createGuardianGroup = async (account, selectedGuardians) => {
|
|
|
929
967
|
);
|
|
930
968
|
const result = await signAndSend(tx, account);
|
|
931
969
|
debugLog("Guardian group created:", result);
|
|
970
|
+
return result;
|
|
932
971
|
} catch (error) {
|
|
933
972
|
console.error("Error creating guardian group:", error);
|
|
934
973
|
throw new Error(`Failed to create guardian group: ${error instanceof Error ? error.message : error}`);
|
|
935
974
|
}
|
|
936
975
|
};
|
|
976
|
+
var createGuardianGroupAndWatch = async (account, guardians, maxBlocks = 20) => {
|
|
977
|
+
assert(guardians.length === 3, "Exactly 3 guardians are required");
|
|
978
|
+
assert(account, "Failed to load account");
|
|
979
|
+
const api2 = await getApi();
|
|
980
|
+
assert(api2, "Failed to initialize API");
|
|
981
|
+
debugLog(`Submitting guardian group creation...`);
|
|
982
|
+
const creationResult = await createGuardianGroup(account, guardians);
|
|
983
|
+
const startBlock = (creationResult?.blockNumber ?? 0) + 1;
|
|
984
|
+
debugLog(`Group creation tx confirmed at block ${startBlock - 1}. Listening for DaccGuardianGroup event from block ${startBlock}...`);
|
|
985
|
+
const match = await scanForBlockEvent(
|
|
986
|
+
api2,
|
|
987
|
+
{
|
|
988
|
+
predicate: async (block2, event, phase) => {
|
|
989
|
+
if (event.section.toLowerCase() !== "dataavailability" || event.method.toLowerCase() !== "daccguardiangroup") return false;
|
|
990
|
+
if (!phase.isApplyExtrinsic) return false;
|
|
991
|
+
const ext2 = block2.block.extrinsics[phase.asApplyExtrinsic.toNumber()];
|
|
992
|
+
return ext2?.method?.section?.toLowerCase() === "dataavailability" && ext2?.method?.method?.toLowerCase() === "daccguardiangroupinfo";
|
|
993
|
+
}
|
|
994
|
+
},
|
|
995
|
+
startBlock,
|
|
996
|
+
maxBlocks
|
|
997
|
+
);
|
|
998
|
+
assert(match.extrinsicIndex !== null, "DaccGuardianGroup event was not emitted by an extrinsic");
|
|
999
|
+
debugLog(`DaccGuardianGroup event found at block ${match.blockNumber}, extrinsic index ${match.extrinsicIndex}. Decoding args...`);
|
|
1000
|
+
const block = await match.block();
|
|
1001
|
+
const ext = block.block.extrinsics[match.extrinsicIndex];
|
|
1002
|
+
const [group_id, group_pk, tau_params, agg_key] = ext.method.args;
|
|
1003
|
+
debugLog(`Guardian group parameters decoded: group_id=${group_id.toHex()}`);
|
|
1004
|
+
return {
|
|
1005
|
+
groupId: group_id.toHex(),
|
|
1006
|
+
groupPk: group_pk.toHex(),
|
|
1007
|
+
tauParams: tau_params.toHex(),
|
|
1008
|
+
aggKey: agg_key.toHex(),
|
|
1009
|
+
guardians
|
|
1010
|
+
};
|
|
1011
|
+
};
|
|
937
1012
|
|
|
938
1013
|
// src/guardian/join.ts
|
|
939
1014
|
async function joinGuardian(account, prefs) {
|
|
@@ -980,7 +1055,7 @@ var signAndSend = async (request, account, opts = DEFAULT_COMPUTE_PAYLOAD) => {
|
|
|
980
1055
|
});
|
|
981
1056
|
});
|
|
982
1057
|
if (tx_result.isError) {
|
|
983
|
-
throw new Error(`Transaction failed with error: ${tx_result.
|
|
1058
|
+
throw new Error(`Transaction failed with error: ${tx_result.dispatchError}`);
|
|
984
1059
|
}
|
|
985
1060
|
if (tx_result.dispatchError) {
|
|
986
1061
|
throw new Error(
|
|
@@ -991,8 +1066,8 @@ var signAndSend = async (request, account, opts = DEFAULT_COMPUTE_PAYLOAD) => {
|
|
|
991
1066
|
`Transaction ${tx_result.txHash.toHex()} included in timepoint ${tx_result.blockNumber}-${tx_result.txIndex}`
|
|
992
1067
|
);
|
|
993
1068
|
return {
|
|
994
|
-
blockNumber: tx_result
|
|
995
|
-
index: tx_result
|
|
1069
|
+
blockNumber: tx_result.blockNumber?.toNumber() ?? 0,
|
|
1070
|
+
index: tx_result.txIndex ?? 0,
|
|
996
1071
|
hash: tx_result.txHash.toHex(),
|
|
997
1072
|
tx_result
|
|
998
1073
|
};
|
|
@@ -1010,14 +1085,14 @@ var getGuardianAddress = async () => {
|
|
|
1010
1085
|
const api2 = await getApi();
|
|
1011
1086
|
if (!api2) throw new Error("API not initialized");
|
|
1012
1087
|
assert(
|
|
1013
|
-
isFunction2(api2.query["guardian"]?.["
|
|
1014
|
-
`api.query.guardian.
|
|
1088
|
+
isFunction2(api2.query["guardian"]?.["workerByKey"]),
|
|
1089
|
+
`api.query.guardian.workerByKey does not exist`
|
|
1015
1090
|
);
|
|
1016
1091
|
const list = await getGuardianList();
|
|
1017
1092
|
const addresses = await Promise.all(
|
|
1018
1093
|
list.map(async (item) => {
|
|
1019
|
-
const peerid = bs58.decode(item).slice(
|
|
1020
|
-
return (await api2.query["guardian"]["
|
|
1094
|
+
const peerid = bs58.decode(item).slice(6, 38);
|
|
1095
|
+
return (await api2.query["guardian"]["workerByKey"](peerid)).toString();
|
|
1021
1096
|
})
|
|
1022
1097
|
);
|
|
1023
1098
|
return list.map((peerid, index) => ({ peerid, address: addresses[index] }));
|
|
@@ -1044,6 +1119,149 @@ var getGuardianNwParams = async () => {
|
|
|
1044
1119
|
throw error;
|
|
1045
1120
|
}
|
|
1046
1121
|
};
|
|
1122
|
+
async function fetchAndDecodeExtrinsic(blockHeight, extrinsicIndex) {
|
|
1123
|
+
const api2 = await getApi();
|
|
1124
|
+
if (!api2) throw new Error("API not initialized");
|
|
1125
|
+
const blockHash = await api2.rpc.chain.getBlockHash(blockHeight);
|
|
1126
|
+
const signedBlock = await api2.rpc.chain.getBlock(blockHash);
|
|
1127
|
+
const extrinsics = signedBlock.block.extrinsics;
|
|
1128
|
+
if (extrinsicIndex >= extrinsics.length) {
|
|
1129
|
+
throw new Error(
|
|
1130
|
+
`extrinsic index ${extrinsicIndex} out of range \u2014 block ${blockHeight} has ${extrinsics.length} extrinsics`
|
|
1131
|
+
);
|
|
1132
|
+
}
|
|
1133
|
+
const raw = extrinsics[extrinsicIndex];
|
|
1134
|
+
const decoded = raw.toHuman();
|
|
1135
|
+
return { raw, decoded };
|
|
1136
|
+
}
|
|
1137
|
+
var scanForBlockEvent = (api2, filter, startBlock, maxBlocks = 20) => {
|
|
1138
|
+
const hasPredicate = "predicate" in filter;
|
|
1139
|
+
let blocksScanned = 0;
|
|
1140
|
+
let done = false;
|
|
1141
|
+
return new Promise((resolve, reject) => {
|
|
1142
|
+
const unsubPromise = api2.rpc.chain.subscribeNewHeads(
|
|
1143
|
+
async (header) => {
|
|
1144
|
+
if (done) return;
|
|
1145
|
+
const currentBlock = header.number.toNumber();
|
|
1146
|
+
if (startBlock !== void 0 && currentBlock < startBlock) return;
|
|
1147
|
+
blocksScanned++;
|
|
1148
|
+
if (maxBlocks > 0 && blocksScanned > maxBlocks) {
|
|
1149
|
+
done = true;
|
|
1150
|
+
unsubPromise.then((unsub) => unsub());
|
|
1151
|
+
reject(new Error(`No matching event found within ${maxBlocks} blocks`));
|
|
1152
|
+
return;
|
|
1153
|
+
}
|
|
1154
|
+
try {
|
|
1155
|
+
const blockHash = await api2.rpc.chain.getBlockHash(currentBlock);
|
|
1156
|
+
const eventsResult = await api2.query.system.events.at(blockHash);
|
|
1157
|
+
const events = eventsResult;
|
|
1158
|
+
const blockData = await (hasPredicate ? api2.rpc.chain.getBlock(blockHash) : Promise.resolve(null));
|
|
1159
|
+
for (const record of events) {
|
|
1160
|
+
const { event, phase } = record;
|
|
1161
|
+
const matched = hasPredicate ? await filter.predicate(blockData, event, phase) : event.section.toLowerCase() === filter.section.toLowerCase() && event.method.toLowerCase() === filter.method.toLowerCase();
|
|
1162
|
+
if (!matched) continue;
|
|
1163
|
+
done = true;
|
|
1164
|
+
unsubPromise.then((unsub) => unsub());
|
|
1165
|
+
resolve({
|
|
1166
|
+
blockHash: blockHash.toHex(),
|
|
1167
|
+
blockNumber: currentBlock,
|
|
1168
|
+
extrinsicIndex: phase.isApplyExtrinsic ? phase.asApplyExtrinsic.toNumber() : null,
|
|
1169
|
+
block: blockData ? () => Promise.resolve(blockData) : () => api2.rpc.chain.getBlock(blockHash),
|
|
1170
|
+
events
|
|
1171
|
+
});
|
|
1172
|
+
return;
|
|
1173
|
+
}
|
|
1174
|
+
} catch (err) {
|
|
1175
|
+
debugLog(`scanForBlockEvent: error scanning block ${currentBlock}: ${err}`);
|
|
1176
|
+
}
|
|
1177
|
+
}
|
|
1178
|
+
);
|
|
1179
|
+
});
|
|
1180
|
+
};
|
|
1181
|
+
async function watchForSubmissionReceipt(requestId, timeoutMs = 10 * 60 * 1e3) {
|
|
1182
|
+
const api2 = await getApi();
|
|
1183
|
+
if (!api2) throw new Error("API not initialized");
|
|
1184
|
+
return new Promise((resolve, reject) => {
|
|
1185
|
+
let unsubFn;
|
|
1186
|
+
let settled = false;
|
|
1187
|
+
const cleanup = (settleFn) => {
|
|
1188
|
+
if (settled) return;
|
|
1189
|
+
settled = true;
|
|
1190
|
+
unsubFn?.();
|
|
1191
|
+
settleFn();
|
|
1192
|
+
};
|
|
1193
|
+
const timer = setTimeout(
|
|
1194
|
+
() => cleanup(() => reject(new Error(`receipt watch timed out for requestId=${requestId}`))),
|
|
1195
|
+
timeoutMs
|
|
1196
|
+
);
|
|
1197
|
+
api2.rpc.chain.subscribeNewHeads(async (header) => {
|
|
1198
|
+
if (settled) return;
|
|
1199
|
+
try {
|
|
1200
|
+
const blockNumber = header.number.toNumber();
|
|
1201
|
+
const blockHash = header.hash;
|
|
1202
|
+
const signedBlock = await api2.rpc.chain.getBlock(blockHash);
|
|
1203
|
+
const extrinsics = signedBlock.block.extrinsics;
|
|
1204
|
+
for (let i = 0; i < extrinsics.length; i++) {
|
|
1205
|
+
const ext = extrinsics[i];
|
|
1206
|
+
const decoded = ext.toHuman();
|
|
1207
|
+
const method = decoded?.method;
|
|
1208
|
+
if (String(method?.section ?? "").toLowerCase() === "compute" && String(method?.method ?? "").toLowerCase() === "result") {
|
|
1209
|
+
const args = method?.args;
|
|
1210
|
+
const onChainId = String(args?.request_id ?? args?.requestId ?? args?.[0] ?? "");
|
|
1211
|
+
if (onChainId === requestId) {
|
|
1212
|
+
clearTimeout(timer);
|
|
1213
|
+
cleanup(
|
|
1214
|
+
() => resolve({
|
|
1215
|
+
extrinsicHash: ext.hash.toHex(),
|
|
1216
|
+
blockHeight: blockNumber,
|
|
1217
|
+
extrinsicIndex: i
|
|
1218
|
+
})
|
|
1219
|
+
);
|
|
1220
|
+
return;
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
}
|
|
1224
|
+
} catch (err) {
|
|
1225
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
1226
|
+
console.warn(`[watchReceipt] block check failed requestId=${requestId}: ${msg}`);
|
|
1227
|
+
}
|
|
1228
|
+
}).then((fn) => {
|
|
1229
|
+
if (settled) {
|
|
1230
|
+
fn();
|
|
1231
|
+
} else {
|
|
1232
|
+
unsubFn = fn;
|
|
1233
|
+
}
|
|
1234
|
+
}).catch((err) => {
|
|
1235
|
+
clearTimeout(timer);
|
|
1236
|
+
cleanup(() => reject(err));
|
|
1237
|
+
});
|
|
1238
|
+
});
|
|
1239
|
+
}
|
|
1240
|
+
async function getAgreementCreatedRequestId(blockHeight, extrinsicIndex) {
|
|
1241
|
+
const api2 = await getApi();
|
|
1242
|
+
if (!api2) throw new Error("API not initialized");
|
|
1243
|
+
const blockHash = await api2.rpc.chain.getBlockHash(blockHeight);
|
|
1244
|
+
const apiAt = await api2.at(blockHash);
|
|
1245
|
+
const allEvents = await apiAt.query.system.events();
|
|
1246
|
+
for (const record of allEvents) {
|
|
1247
|
+
const { phase, event } = record;
|
|
1248
|
+
if (!phase.isApplyExtrinsic || phase.asApplyExtrinsic.toNumber() !== extrinsicIndex) {
|
|
1249
|
+
continue;
|
|
1250
|
+
}
|
|
1251
|
+
if (String(event.section ?? "").toLowerCase() !== "compute" || String(event.method ?? "").toLowerCase() !== "agreementcreated") {
|
|
1252
|
+
continue;
|
|
1253
|
+
}
|
|
1254
|
+
const dataHuman = event.data.toHuman();
|
|
1255
|
+
if (Array.isArray(dataHuman)) {
|
|
1256
|
+
const first = dataHuman[0];
|
|
1257
|
+
if (first != null) return String(first);
|
|
1258
|
+
} else if (dataHuman !== null && typeof dataHuman === "object") {
|
|
1259
|
+
const id = dataHuman["requestId"] ?? dataHuman["request_id"] ?? dataHuman["id"];
|
|
1260
|
+
if (id != null) return String(id);
|
|
1261
|
+
}
|
|
1262
|
+
}
|
|
1263
|
+
return null;
|
|
1264
|
+
}
|
|
1047
1265
|
|
|
1048
1266
|
// src/chain/onchainfs.ts
|
|
1049
1267
|
var getBlock2 = async (api2, blockNumber) => {
|
|
@@ -1082,12 +1300,10 @@ var MCryptFs = class _MCryptFs {
|
|
|
1082
1300
|
this._metaRef = metadataRef;
|
|
1083
1301
|
this._metadata = metadata;
|
|
1084
1302
|
this._init = true;
|
|
1303
|
+
this._chunkRefs = [];
|
|
1085
1304
|
}
|
|
1086
1305
|
static dummyInstance(mcryptApi) {
|
|
1087
|
-
return new _MCryptFs(mcryptApi, null,
|
|
1088
|
-
name: "on-chain-file-name.txt",
|
|
1089
|
-
description: "File uploaded using mcrypt-onchain-fs"
|
|
1090
|
-
});
|
|
1306
|
+
return new _MCryptFs(mcryptApi, null, null);
|
|
1091
1307
|
}
|
|
1092
1308
|
isValid() {
|
|
1093
1309
|
if (!this._metaRef) {
|
|
@@ -1151,8 +1367,8 @@ var MCryptFsWriter = class {
|
|
|
1151
1367
|
request.signAndSend(this._account, { app_id: 1 }, (result) => {
|
|
1152
1368
|
if (result.isInBlock || result.isFinalized || result.isError) {
|
|
1153
1369
|
resolve({
|
|
1154
|
-
blockNumber: result.blockNumber?.toNumber(),
|
|
1155
|
-
index: result.txIndex
|
|
1370
|
+
blockNumber: result.blockNumber?.toNumber() ?? 0,
|
|
1371
|
+
index: result.txIndex ?? 0
|
|
1156
1372
|
});
|
|
1157
1373
|
}
|
|
1158
1374
|
});
|
|
@@ -1174,7 +1390,7 @@ var MCryptFsWriter = class {
|
|
|
1174
1390
|
ciphertext: uint8ArrayToBase64(Uint8Array.from(ciphertext)),
|
|
1175
1391
|
td_params,
|
|
1176
1392
|
group_pk: this._guardianInfo.groupPk,
|
|
1177
|
-
tau_params: this._guardianInfo.
|
|
1393
|
+
tau_params: this._guardianInfo.tauParams,
|
|
1178
1394
|
chosen_guardians: this._guardianInfo.guardians,
|
|
1179
1395
|
blobRef: [blobRef.blockNumber, blobRef.extrinsicIndex]
|
|
1180
1396
|
});
|
|
@@ -1217,7 +1433,7 @@ var MCryptFsWriter = class {
|
|
|
1217
1433
|
const metadataRef = await this.writeMetadata();
|
|
1218
1434
|
return await FileFromMetadataRef(this._api, [
|
|
1219
1435
|
metadataRef.blockNumber,
|
|
1220
|
-
metadataRef.index
|
|
1436
|
+
metadataRef.index ?? 0
|
|
1221
1437
|
]);
|
|
1222
1438
|
}
|
|
1223
1439
|
};
|
|
@@ -1229,6 +1445,9 @@ var MCryptFsReader = class {
|
|
|
1229
1445
|
}
|
|
1230
1446
|
async downloadFile() {
|
|
1231
1447
|
const chunks = [];
|
|
1448
|
+
if (!this._onChainFile._metadata) {
|
|
1449
|
+
throw new Error("Metadata not loaded");
|
|
1450
|
+
}
|
|
1232
1451
|
let _blockNumber = this._onChainFile._metadata.startRef[0];
|
|
1233
1452
|
let _extIndex = this._onChainFile._metadata.startRef[1];
|
|
1234
1453
|
while (_blockNumber !== 0) {
|
|
@@ -1260,21 +1479,13 @@ var FileFromMetadataRef = async (mcryptApi, metadataRef) => {
|
|
|
1260
1479
|
};
|
|
1261
1480
|
|
|
1262
1481
|
// src/compute/agreement.ts
|
|
1263
|
-
import bs582 from "bs58";
|
|
1264
1482
|
async function createAgreement(contract, account) {
|
|
1265
1483
|
const api2 = await getApi();
|
|
1266
1484
|
if (!api2) throw new Error("Api not initialized");
|
|
1267
|
-
const
|
|
1268
|
-
const agreement = guardians.map((g) => bs582.decode(g.peerid).subarray(6));
|
|
1269
|
-
const txContract = {
|
|
1270
|
-
...contract,
|
|
1271
|
-
guardians: guardians.map((g) => g.address)
|
|
1272
|
-
};
|
|
1273
|
-
const tx = api2.tx.compute.agreement(txContract);
|
|
1485
|
+
const tx = api2.tx["compute"]["agreement"](contract);
|
|
1274
1486
|
const opts = {
|
|
1275
1487
|
compute: {
|
|
1276
1488
|
da_type: 1,
|
|
1277
|
-
agreement,
|
|
1278
1489
|
verification: 0,
|
|
1279
1490
|
compute: contract.contract_type === "Active" ? 1 : 0
|
|
1280
1491
|
}
|
|
@@ -1285,17 +1496,16 @@ async function createAgreement(contract, account) {
|
|
|
1285
1496
|
opts
|
|
1286
1497
|
);
|
|
1287
1498
|
if (!tx_result.isError) {
|
|
1288
|
-
const agreementCreatedEvent = tx_result.events.find(
|
|
1289
|
-
|
|
1290
|
-
|
|
1499
|
+
const agreementCreatedEvent = tx_result.events.find(
|
|
1500
|
+
(event) => {
|
|
1501
|
+
return event.event.section === "compute" && event.event.method === "AgreementCreated";
|
|
1502
|
+
}
|
|
1503
|
+
);
|
|
1291
1504
|
if (agreementCreatedEvent) {
|
|
1292
|
-
debugLog(
|
|
1293
|
-
"Agreement data:",
|
|
1294
|
-
agreementCreatedEvent.event.data.toString()
|
|
1295
|
-
);
|
|
1505
|
+
debugLog("Agreement data:", agreementCreatedEvent.event.data.toString());
|
|
1296
1506
|
return {
|
|
1297
1507
|
blockNumber,
|
|
1298
|
-
index,
|
|
1508
|
+
index: index ?? 0,
|
|
1299
1509
|
hash,
|
|
1300
1510
|
agreementId: agreementCreatedEvent.event.data[0]?.toHex?.() ?? agreementCreatedEvent.event.data.toString()
|
|
1301
1511
|
};
|
|
@@ -1303,10 +1513,10 @@ async function createAgreement(contract, account) {
|
|
|
1303
1513
|
debugLog("AgreementCreated event not found");
|
|
1304
1514
|
}
|
|
1305
1515
|
}
|
|
1306
|
-
return { blockNumber, index, hash };
|
|
1516
|
+
return { blockNumber, index: index ?? 0, hash };
|
|
1307
1517
|
}
|
|
1308
1518
|
async function createSimpleAgreement() {
|
|
1309
|
-
const guardianIds = (await getGuardianAddress()).slice(0, 3);
|
|
1519
|
+
const guardianIds = (await getGuardianAddress()).slice(0, 3).map((g) => g.address);
|
|
1310
1520
|
assert(guardianIds.length === 3, "Not enough guardians to create agreement");
|
|
1311
1521
|
const contract = {
|
|
1312
1522
|
contract_type: "Dormant",
|
|
@@ -1315,9 +1525,9 @@ async function createSimpleAgreement() {
|
|
|
1315
1525
|
compute: {
|
|
1316
1526
|
cipher: "Plaintext",
|
|
1317
1527
|
computer_indices: [0, 1, 2],
|
|
1318
|
-
fees:
|
|
1528
|
+
fees: toAtomicPaliAmount("0.01"),
|
|
1319
1529
|
deadline: 0,
|
|
1320
|
-
confidentiality: { Trusted:
|
|
1530
|
+
confidentiality: { Trusted: 0 },
|
|
1321
1531
|
fee_function: null,
|
|
1322
1532
|
input: null,
|
|
1323
1533
|
program: { NativeData: "DaFalse" },
|
|
@@ -1332,9 +1542,7 @@ async function createSimpleAgreement() {
|
|
|
1332
1542
|
}
|
|
1333
1543
|
|
|
1334
1544
|
// src/compute/data.ts
|
|
1335
|
-
async function dataContract(params) {
|
|
1336
|
-
const keyring2 = await getKeyring();
|
|
1337
|
-
const account = keyring2.getPairs()[0];
|
|
1545
|
+
async function dataContract(params, account) {
|
|
1338
1546
|
const plaintextCipher = "Plaintext";
|
|
1339
1547
|
const atomicFees = toAtomicPaliAmount(params.fees ?? "0");
|
|
1340
1548
|
const computeStep = {
|
|
@@ -1342,11 +1550,7 @@ async function dataContract(params) {
|
|
|
1342
1550
|
computer_indices: params.guardians.map((_, i) => i),
|
|
1343
1551
|
fees: atomicFees,
|
|
1344
1552
|
deadline: params.deadline ?? 0,
|
|
1345
|
-
confidentiality: {
|
|
1346
|
-
Trusted: {
|
|
1347
|
-
trust_index: params.trustIndex ?? 0
|
|
1348
|
-
}
|
|
1349
|
-
},
|
|
1553
|
+
confidentiality: { Trusted: params.trustIndex ?? 0 },
|
|
1350
1554
|
fee_function: null,
|
|
1351
1555
|
input: {
|
|
1352
1556
|
Url: {
|
|
@@ -1369,9 +1573,7 @@ async function dataContract(params) {
|
|
|
1369
1573
|
}
|
|
1370
1574
|
|
|
1371
1575
|
// src/compute/inference.ts
|
|
1372
|
-
async function inferenceCompute(params) {
|
|
1373
|
-
const keyring2 = await getKeyring();
|
|
1374
|
-
const account = keyring2.getPairs()[0];
|
|
1576
|
+
async function inferenceCompute(params, account) {
|
|
1375
1577
|
const inputData = typeof params.input === "string" ? Array.from(new TextEncoder().encode(params.input)) : Array.from(params.input);
|
|
1376
1578
|
const atomicFees = toAtomicPaliAmount(params.fees ?? "0");
|
|
1377
1579
|
const plaintextCipher = "Plaintext";
|
|
@@ -1380,7 +1582,7 @@ async function inferenceCompute(params) {
|
|
|
1380
1582
|
computer_indices: params.guardians.map((_, i) => i),
|
|
1381
1583
|
fees: atomicFees,
|
|
1382
1584
|
deadline: params.deadline ?? 0,
|
|
1383
|
-
confidentiality: { Trusted:
|
|
1585
|
+
confidentiality: { Trusted: 0 },
|
|
1384
1586
|
fee_function: null,
|
|
1385
1587
|
input: { Inline: { data: inputData } },
|
|
1386
1588
|
program: { NativeExecute: "Inference" }
|
|
@@ -1395,7 +1597,7 @@ async function inferenceCompute(params) {
|
|
|
1395
1597
|
}
|
|
1396
1598
|
|
|
1397
1599
|
// src/compute/participants.ts
|
|
1398
|
-
import
|
|
1600
|
+
import bs582 from "bs58";
|
|
1399
1601
|
async function getGuardianParticipants() {
|
|
1400
1602
|
const api2 = await getApi();
|
|
1401
1603
|
if (!api2) throw new Error("Api not initialized");
|
|
@@ -1405,8 +1607,8 @@ async function getGuardianParticipants() {
|
|
|
1405
1607
|
const currentEra = await api2.query.staking.currentEra();
|
|
1406
1608
|
const currentIndex = await api2.query.guardian.currentIndex();
|
|
1407
1609
|
const peerid = await api2.rpc.system.localPeerId();
|
|
1408
|
-
const _peerid =
|
|
1409
|
-
const account = await api2.query.guardian.
|
|
1610
|
+
const _peerid = bs582.decode((peerid || "").toString());
|
|
1611
|
+
const account = await api2.query.guardian.workerByKey(_peerid.slice(6, 38));
|
|
1410
1612
|
const nextIndex = currentIndex ? Number(currentIndex) + 1 : 1;
|
|
1411
1613
|
const guardiansList = guardians?.toJSON() || [];
|
|
1412
1614
|
const nextGuardiansList = nextGuardians?.toJSON() || [];
|
|
@@ -1458,9 +1660,7 @@ async function getGuardianParticipants() {
|
|
|
1458
1660
|
}
|
|
1459
1661
|
|
|
1460
1662
|
// src/compute/simple.ts
|
|
1461
|
-
async function simpleCompute(params) {
|
|
1462
|
-
const keyring2 = await getKeyring();
|
|
1463
|
-
const account = keyring2.getPairs()[0];
|
|
1663
|
+
async function simpleCompute(params, account) {
|
|
1464
1664
|
const plaintextCipher = "Plaintext";
|
|
1465
1665
|
const atomicFees = toAtomicPaliAmount(params.fees ?? "0");
|
|
1466
1666
|
const computeStep = {
|
|
@@ -1468,18 +1668,9 @@ async function simpleCompute(params) {
|
|
|
1468
1668
|
computer_indices: params.guardians.map((_, i) => i),
|
|
1469
1669
|
fees: atomicFees,
|
|
1470
1670
|
deadline: params.deadline ?? 0,
|
|
1471
|
-
confidentiality: {
|
|
1472
|
-
Trusted: {
|
|
1473
|
-
trust_index: params.trustIndex ?? 0
|
|
1474
|
-
}
|
|
1475
|
-
},
|
|
1671
|
+
confidentiality: { Trusted: params.trustIndex ?? 0 },
|
|
1476
1672
|
fee_function: null,
|
|
1477
|
-
input:
|
|
1478
|
-
ChainTransaction: {
|
|
1479
|
-
block_number: params.inputBlockNumber,
|
|
1480
|
-
extrinsic_index: params.inputExtrinsicIndex
|
|
1481
|
-
}
|
|
1482
|
-
},
|
|
1673
|
+
input: null,
|
|
1483
1674
|
program: {
|
|
1484
1675
|
Url: {
|
|
1485
1676
|
url: Array.from(new TextEncoder().encode(params.programUrl))
|
|
@@ -1551,11 +1742,7 @@ async function registerDataAgreement(account, params) {
|
|
|
1551
1742
|
computer_indices: params.guardians.map((_, i) => i),
|
|
1552
1743
|
fees: params.fees,
|
|
1553
1744
|
deadline: params.deadline ?? 0,
|
|
1554
|
-
confidentiality: {
|
|
1555
|
-
Trusted: {
|
|
1556
|
-
trust_index: params.trustIndex ?? 0
|
|
1557
|
-
}
|
|
1558
|
-
},
|
|
1745
|
+
confidentiality: { Trusted: params.trustIndex ?? 0 },
|
|
1559
1746
|
fee_function: null,
|
|
1560
1747
|
input: {
|
|
1561
1748
|
ChainTransaction: {
|
|
@@ -1657,15 +1844,15 @@ async function submitTEDataWithCipher(account, data, chosenGuardians, tau_params
|
|
|
1657
1844
|
return {
|
|
1658
1845
|
ref,
|
|
1659
1846
|
cipher: {
|
|
1660
|
-
|
|
1661
|
-
|
|
1847
|
+
ThresholdHybrid: {
|
|
1848
|
+
threshold_params: {
|
|
1662
1849
|
SilentThreshold: {
|
|
1663
1850
|
td_params: Array.from(hexToUint8Array(encryptedKey)),
|
|
1664
1851
|
pk_bytes: Array.from(hexToUint8Array(group_pk)),
|
|
1665
1852
|
tau_params: Array.from(hexToUint8Array(tau_params))
|
|
1666
1853
|
}
|
|
1667
1854
|
},
|
|
1668
|
-
|
|
1855
|
+
symmetric_params: {
|
|
1669
1856
|
ChaCha20Poly1305: { nonce: Array.from(nonce) }
|
|
1670
1857
|
}
|
|
1671
1858
|
}
|
|
@@ -1894,7 +2081,7 @@ Setting identity for account: ${account.address} as ${display}`);
|
|
|
1894
2081
|
}
|
|
1895
2082
|
|
|
1896
2083
|
// src/rotateKeys.ts
|
|
1897
|
-
import
|
|
2084
|
+
import bs583 from "bs58";
|
|
1898
2085
|
import assert3 from "assert";
|
|
1899
2086
|
async function rotateAndSetKeys(account) {
|
|
1900
2087
|
const api2 = await getApi();
|
|
@@ -1909,8 +2096,8 @@ async function rotateAndSetKeys(account) {
|
|
|
1909
2096
|
async function setWorker(account) {
|
|
1910
2097
|
const api2 = await getApi();
|
|
1911
2098
|
assert3(api2, "API not initialized");
|
|
1912
|
-
const peerid =
|
|
1913
|
-
const setWorkerTx = api2.tx.guardian.setWorker(peerid.slice(
|
|
2099
|
+
const peerid = bs583.decode((await api2.rpc.system.localPeerId()).toString());
|
|
2100
|
+
const setWorkerTx = api2.tx.guardian.setWorker(peerid.slice(6, 38));
|
|
1914
2101
|
const hash = await signAndSend(setWorkerTx, account);
|
|
1915
2102
|
debugLog(`Set worker id transaction sent with hash: ${hash.hash}`);
|
|
1916
2103
|
}
|
|
@@ -1948,6 +2135,7 @@ export {
|
|
|
1948
2135
|
createAccount,
|
|
1949
2136
|
createAgreement,
|
|
1950
2137
|
createGuardianGroup,
|
|
2138
|
+
createGuardianGroupAndWatch,
|
|
1951
2139
|
createSimpleAgreement,
|
|
1952
2140
|
dataContract,
|
|
1953
2141
|
debugLog,
|
|
@@ -1958,6 +2146,7 @@ export {
|
|
|
1958
2146
|
encrypt as ecncryptTest,
|
|
1959
2147
|
encodeCiphertext,
|
|
1960
2148
|
encrypt2 as encrypt,
|
|
2149
|
+
fetchAndDecodeExtrinsic,
|
|
1961
2150
|
fetchTokenProperties,
|
|
1962
2151
|
formatBalanceWithTokenProperties,
|
|
1963
2152
|
formatPaliAmount,
|
|
@@ -1966,6 +2155,7 @@ export {
|
|
|
1966
2155
|
gen_shared_key,
|
|
1967
2156
|
gen_stretched_key,
|
|
1968
2157
|
generateRandomBytes,
|
|
2158
|
+
getAgreementCreatedRequestId,
|
|
1969
2159
|
getApi,
|
|
1970
2160
|
getCachedTokenProperties,
|
|
1971
2161
|
getEncKeyring,
|
|
@@ -1989,6 +2179,7 @@ export {
|
|
|
1989
2179
|
removeStake,
|
|
1990
2180
|
rotateAndSetKeys,
|
|
1991
2181
|
runAgent,
|
|
2182
|
+
scanForBlockEvent,
|
|
1992
2183
|
setIdentity,
|
|
1993
2184
|
setWorker,
|
|
1994
2185
|
signAndSend,
|
|
@@ -2005,6 +2196,7 @@ export {
|
|
|
2005
2196
|
uploadDataLegacy,
|
|
2006
2197
|
utilCrypto,
|
|
2007
2198
|
wasmCrypto,
|
|
2199
|
+
watchForSubmissionReceipt,
|
|
2008
2200
|
withdrawStake,
|
|
2009
2201
|
writeMetadata
|
|
2010
2202
|
};
|