-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathauthorize_and_store_papi.js
More file actions
78 lines (67 loc) · 2.73 KB
/
authorize_and_store_papi.js
File metadata and controls
78 lines (67 loc) · 2.73 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import assert from "assert";
import { createClient } from 'polkadot-api';
import { getWsProvider } from 'polkadot-api/ws';
import { cryptoWaitReady } from '@polkadot/util-crypto';
import { authorizeAccount, fetchCid, store, TX_MODE_FINALIZED_BLOCK } from './api.js';
import { setupKeyringAndSigners, waitForBlockProduction, DEFAULT_IPFS_GATEWAY_URL } from './common.js';
import { logHeader, logConnection, logSuccess, logError, logTestResult } from './logger.js';
import { cidFromBytes } from "./cid_dag_metadata.js";
import { bulletin } from './.papi/descriptors/dist/index.js';
// Command line arguments: [ws_url] [seed] [ipfs_api_url]
const args = process.argv.slice(2);
const NODE_WS = args[0] || 'ws://localhost:10000';
const SEED = args[1] || '//Alice';
const HTTP_IPFS_API = args[2] || DEFAULT_IPFS_GATEWAY_URL;
async function main() {
await cryptoWaitReady();
logHeader('AUTHORIZE AND STORE TEST (WebSocket)');
logConnection(NODE_WS, SEED, HTTP_IPFS_API);
let client, resultCode;
try {
// Init WS PAPI client and typed api.
client = createClient(getWsProvider(NODE_WS));
const bulletinAPI = client.getTypedApi(bulletin);
await waitForBlockProduction(bulletinAPI);
// Signers.
const { authorizationSigner, whoSigner, whoAddress } = setupKeyringAndSigners(SEED, '//Papisigner');
// Data to store.
const dataToStore = "Hello, Bulletin with PAPI - " + new Date().toString();
let expectedCid = await cidFromBytes(dataToStore);
// Authorize an account.
await authorizeAccount(
bulletinAPI,
authorizationSigner,
whoAddress,
100,
BigInt(100 * 1024 * 1024), // 100 MiB
TX_MODE_FINALIZED_BLOCK,
);
// Store data.
const { cid } = await store(bulletinAPI, whoSigner, dataToStore);
logSuccess(`Data stored successfully with CID: ${cid}`);
// Read back from IPFS
let downloadedContent = await fetchCid(HTTP_IPFS_API, cid);
logSuccess(`Downloaded content: ${downloadedContent.toString()}`);
assert.deepStrictEqual(
cid,
expectedCid,
'❌ expectedCid does not match cid!'
);
assert.deepStrictEqual(
dataToStore,
downloadedContent.toString(),
'❌ dataToStore does not match downloadedContent!'
);
logSuccess('Verified content!');
logTestResult(true, 'Authorize and Store Test');
resultCode = 0;
} catch (error) {
logError(`Error: ${error.message}`);
console.error(error);
resultCode = 1;
} finally {
if (client) client.destroy();
process.exit(resultCode);
}
}
await main();