Skip to content

Commit 7f40aeb

Browse files
committed
papi script WIP
1 parent 2169223 commit 7f40aeb

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
// npm install polkadot-api @polkadot-api/pjs-signer @polkadot/keyring @polkadot/util-crypto multiformats ipfs-http-client
2+
// npx papi add -w ws://localhost:10000 bulletin
3+
// ipfs daemon &
4+
// ipfs swarm connect /ip4/127.0.0.1/tcp/10001/ws/p2p/12D3KooWQCkBm1BYtkHpocxCwMgR8yjitEeHGx8spzcDLGt2gkBm
5+
// ipfs swarm connect /ip4/127.0.0.1/tcp/12347/ws/p2p/12D3KooWRkZhiRhsqmrQ28rt73K7V3aCBpqKugpsqfpggl4nzazpieyemw6xme
6+
// ipfs swarm peers - should be there
7+
// ipfs bitswap stat
8+
// ipfs block get /ipfs/bafk2bzacebcnty2x5l3jr2sk5rvn7engdfkugpsqfpggl4nzazpieyemw6xme
9+
10+
import { createClient } from 'polkadot-api';
11+
import { getWsProvider } from 'polkadot-api/ws-provider/node';
12+
import { getPolkadotSignerFromPjs } from '@polkadot-api/pjs-signer';
13+
import { Keyring } from '@polkadot/keyring';
14+
import { cryptoWaitReady } from '@polkadot/util-crypto';
15+
import { create } from 'ipfs-http-client';
16+
import { waitForNewBlock, cidFromBytes } from './common.js';
17+
import { bulletin } from '@polkadot-api/descriptors';
18+
19+
async function authorizeAccount(typedApi, sudoPair, who, transactions, bytes) {
20+
console.log('Creating authorizeAccount transaction...');
21+
22+
const authorizeTx = typedApi.tx.TransactionStorage.authorize_account({
23+
who,
24+
transactions,
25+
bytes
26+
});
27+
28+
const sudoTx = typedApi.tx.Sudo.sudo({
29+
call: authorizeTx.decodedCall
30+
});
31+
32+
const result = await sudoTx.signAndSubmit(sudoPair);
33+
console.log('Transaction authorizeAccount submitted:', result);
34+
return result;
35+
}
36+
37+
async function store(typedApi, pair, data) {
38+
console.log('Storing data:', data);
39+
const cid = cidFromBytes(data);
40+
41+
const tx = typedApi.tx.TransactionStorage.store({
42+
data: Array.from(typeof data === 'string' ? Buffer.from(data) : data)
43+
});
44+
45+
const result = await tx.signAndSubmit(pair);
46+
console.log('Transaction store submitted:', result);
47+
48+
return cid;
49+
}
50+
51+
// Connect to a local IPFS gateway (e.g. Kubo)
52+
const ipfs = create({
53+
url: 'http://127.0.0.1:5001', // Local IPFS API
54+
});
55+
56+
async function read_from_ipfs(cid) {
57+
// Fetch the block (downloads via Bitswap if not local)
58+
console.log('Trying to get cid: ', cid);
59+
try {
60+
const block = await ipfs.block.get(cid, {timeout: 10000});
61+
console.log('Received block: ', block);
62+
if (block.length !== 0) {
63+
return block;
64+
}
65+
} catch (error) {
66+
console.log('Block not found directly, trying cat...', error.message);
67+
}
68+
69+
// Fetch the content from IPFS
70+
console.log('Trying to chunk cid: ', cid);
71+
const chunks = [];
72+
for await (const chunk of ipfs.cat(cid)) {
73+
chunks.push(chunk);
74+
}
75+
76+
const content = Buffer.concat(chunks);
77+
return content;
78+
}
79+
80+
async function main() {
81+
await cryptoWaitReady();
82+
83+
// Create PAPI client with WebSocket provider
84+
const wsProvider = getWsProvider('ws://localhost:10000');
85+
const client = createClient(wsProvider);
86+
87+
// Get typed API - requires generated descriptors
88+
const typedApi = client.getTypedApi(bulletin);
89+
90+
// Create keyring and accounts
91+
const keyring = new Keyring({ type: 'sr25519' });
92+
const sudoAccount = keyring.addFromUri('//Alice');
93+
const whoAccount = keyring.addFromUri('//Alice');
94+
95+
// Create PAPI-compatible signers using pjs-signer
96+
const sudoSigner = getPolkadotSignerFromPjs(sudoAccount);
97+
const whoSigner = getPolkadotSignerFromPjs(whoAccount);
98+
99+
// Data
100+
const who = whoAccount.address;
101+
const transactions = 32; // u32 - regular number
102+
const bytes = 64n * 1024n * 1024n; // u64 - BigInt for large numbers
103+
104+
console.log('Doing authorization...');
105+
await authorizeAccount(typedApi, sudoSigner, who, transactions, bytes);
106+
await waitForNewBlock();
107+
console.log('Authorized!');
108+
109+
console.log('Storing data ...');
110+
const dataToStore = "Hello, Bulletin with PAPI - " + new Date().toString();
111+
let cid = await store(typedApi, whoSigner, dataToStore);
112+
console.log('Stored data with CID: ', cid);
113+
await waitForNewBlock();
114+
115+
console.log('Reading content... cid: ', cid);
116+
let content = await read_from_ipfs(cid);
117+
console.log('Content as bytes:', content);
118+
console.log('Content as string:', content.toString());
119+
120+
client.destroy();
121+
}
122+
123+
main().catch(console.error);
124+

0 commit comments

Comments
 (0)