-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathauthorize_preimage_and_store_papi.js
More file actions
141 lines (121 loc) · 5.29 KB
/
authorize_preimage_and_store_papi.js
File metadata and controls
141 lines (121 loc) · 5.29 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import assert from "assert";
import { createClient } from 'polkadot-api';
import { getWsProvider } from 'polkadot-api/ws';
import { cryptoWaitReady } from '@polkadot/util-crypto';
import { authorizeAccount, authorizePreimage, fetchCid, store, TX_MODE_IN_BLOCK, TX_MODE_FINALIZED_BLOCK } from './api.js';
import { setupKeyringAndSigners, getContentHash, waitForBlockProduction, DEFAULT_IPFS_GATEWAY_URL } from './common.js';
import { logHeader, logConnection, logSection, logSuccess, logError, logInfo, 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;
/**
* Run a preimage authorization + store test.
*
* @param {string} testName - Name of the test for logging
* @param {object} bulletinAPI - PAPI typed API
* @param {object} authorizationSigner - Signer for authorization (must be a TestAccount, e.g. Alice)
* @param {object|null} signer - Signer for store (null for unsigned)
* @param {string|null} signerAddress - Address of the signer (required if signer is not null)
* @param {number|null} cidCodec - CID codec (null for default)
* @param {number|null} mhCode - Multihash code (null for default)
* @param {object|null} client - Client for unsigned transactions
*/
async function runPreimageStoreTest(testName, bulletinAPI, authorizationSigner, signer, signerAddress, cidCodec, mhCode, client) {
logSection(testName);
// Data to store
const dataToStore = `Hello, Bulletin - ${testName} - ${new Date().toString()}`;
// Compute expected CID (use undefined to get defaults, since null overrides them)
const expectedCid = await cidFromBytes(dataToStore, cidCodec ?? undefined, mhCode ?? undefined);
// Authorization always uses blake2_256 hash (pallet internal behavior)
const contentHash = getContentHash(dataToStore);
// Authorize the preimage
await authorizePreimage(
bulletinAPI,
authorizationSigner,
contentHash,
BigInt(dataToStore.length),
TX_MODE_FINALIZED_BLOCK
);
// If signer is provided, also authorize the account (to increment inc_providers/inc_sufficients for `CheckNonce`).
if (signer != null && signerAddress != null) {
logInfo(`Also authorizing account ${signerAddress} to verify preimage auth is preferred`);
await authorizeAccount(
bulletinAPI,
authorizationSigner,
signerAddress,
10, // dummy transactions
BigInt(10000), // dummy bytes
TX_MODE_FINALIZED_BLOCK
);
}
// Store data
const { cid } = await store(bulletinAPI, signer, dataToStore, cidCodec, mhCode, TX_MODE_IN_BLOCK, client);
logSuccess(`Data stored successfully with CID: ${cid.toString()}`);
// Read back from IPFS
const downloadedContent = await fetchCid(HTTP_IPFS_API, cid);
logSuccess(`Downloaded content: ${downloadedContent.toString()}`);
// Verify CID matches
assert.deepStrictEqual(
cid.toString(),
expectedCid.toString(),
'❌ Expected CID does not match actual CID!'
);
// Verify content matches
assert.deepStrictEqual(
dataToStore,
downloadedContent.toString(),
'❌ Stored data does not match downloaded content!'
);
logSuccess('Verified content!');
}
async function main() {
await cryptoWaitReady();
logHeader('AUTHORIZE PREIMAGE AND STORE TEST');
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, '//Preimagesigner');
// Test 1: Unsigned store with preimage auth (default CID config)
await runPreimageStoreTest(
"Test 1: Unsigned store with preimage auth",
bulletinAPI,
authorizationSigner,
null, // unsigned
null, // no signer address
null, // default codec
null, // default hash
client
);
// Test 2: Signed store with preimage auth and custom CID config (raw + SHA2-256)
// Also authorizes account to verify preimage auth is preferred
await runPreimageStoreTest(
"Test 2: Signed store with preimage auth and custom CID",
bulletinAPI,
authorizationSigner,
whoSigner, // signed
whoAddress, // signer address for account auth
0x55, // raw
0x12, // sha2-256
client
);
logTestResult(true, 'Authorize Preimage 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();