Skip to content

Commit 389f56c

Browse files
committed
finally works, need to refactor
1 parent 2783a58 commit 389f56c

File tree

1 file changed

+55
-26
lines changed

1 file changed

+55
-26
lines changed

examples/authorize_and_store_papi_smoldot.js

Lines changed: 55 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@ import { Keyring } from "@polkadot/keyring";
55
import { getSmProvider } from 'polkadot-api/sm-provider';
66
import { getPolkadotSigner } from '@polkadot-api/signer';
77
import { cryptoWaitReady } from '@polkadot/util-crypto';
8-
import { create } from 'ipfs-http-client';
98
import { cidFromBytes } from './common.js';
109
import { bulletin } from './.papi/descriptors/dist/index.mjs';
11-
import { authorizeAccount, store } from './api.js';
12-
import assert from "assert";
10+
import { Binary } from '@polkadot-api/substrate-bindings';
1311

1412
// Generate PAPI descriptors using local node:
1513
// npx papi add -w ws://localhost:10000 bulletin
@@ -41,7 +39,7 @@ async function main() {
4139
// Data
4240
const who = whoAccount.publicKey;
4341
const transactions = 32;
44-
const bytes = 64 * 1024 * 1024; // 64 MB
42+
const bytes = 64n * 1024n * 1024n; // 64 MB
4543

4644
// Prepare data for storage
4745
const dataToStore = "Hello, Bulletin with PAPI + Smoldot - " + new Date().toString();
@@ -50,6 +48,7 @@ async function main() {
5048
// Note: In real usage, this step is not required — the chain spec with bootNodes should be included as part of the dApp.
5149
// For local testing, we use this to fetch the actual chain spec from the local node.
5250
// Get chain spec from Bob node and remove protocolId to allow smoldot to sync with local chain.
51+
// Use false to get full genesis spec, not light sync spec starting at finalized block
5352
const chainSpec = (await bobApi.rpc.syncstate.genSyncSpec(true)).toString();
5453
const chainSpecObj = JSON.parse(chainSpec);
5554
chainSpecObj.protocolId = null;
@@ -68,33 +67,63 @@ async function main() {
6867
const client = createClient(getSmProvider(chain));
6968
const bulletinAPI = client.getTypedApi(bulletin);
7069

71-
// console.log('⏭️ Waiting for 15 seconds for smoldot to sync...');
72-
// await new Promise(resolve => setTimeout(resolve, 15000));
70+
console.log('⏭️ Waiting for 15 seconds for smoldot to sync...');
71+
await new Promise(resolve => setTimeout(resolve, 15000));
7372

74-
const w = who.toString();
75-
console.log('✅ who is who: ', w);
76-
bulletinAPI.tx.transactionStorage.authorizeAccount({
77-
who: w,
73+
const ALICE = "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY";
74+
const authorizeTx = bulletinAPI.tx.TransactionStorage.authorize_account({
75+
who: ALICE,
7876
transactions,
7977
bytes
80-
}).signAndSubmit(whoSigner)
81-
.then(() => console.log("✅ Authorized!"))
82-
.catch((err) => {
83-
console.error("❌ authorize error: ", err);
78+
});
79+
const sudoTx = bulletinAPI.tx.Sudo.sudo({
80+
call: authorizeTx.decodedCall
81+
});
82+
83+
sudoTx.signSubmitAndWatch(sudoSigner).subscribe({
84+
next: (ev) => {
85+
console.log("✅ Authorize event: ", ev.type)
86+
if (ev.type === "txBestBlocksState" && ev.found) {
87+
console.log("✅ Authorization included in block:", ev.block.hash)
88+
}
89+
},
90+
error: (err) => {
91+
console.error("❌ authorize error: ", err)
92+
client.destroy()
93+
sd.terminate()
8494
process.exit(1);
95+
},
96+
complete() {
97+
console.log("✅ Authorized! Now storing data...");
98+
99+
// Convert data to Uint8Array then wrap in Binary for PAPI typed API
100+
const dataBytes = new Uint8Array(Buffer.from(dataToStore));
101+
const binaryData = Binary.fromBytes(dataBytes);
102+
103+
bulletinAPI.tx.TransactionStorage.store({ data: binaryData })
104+
.signSubmitAndWatch(whoSigner).subscribe({
105+
next: (ev) => {
106+
console.log("⏭️ Store event: ", ev.type);
107+
if (ev.type === "txBestBlocksState" && ev.found) {
108+
console.log("✅ Data stored in block:", ev.block.hash);
109+
console.log("✅ Expected CID:", expectedCid);
110+
}
111+
},
112+
error: (err) => {
113+
console.error("❌ store error: ", err);
114+
client.destroy();
115+
sd.terminate();
116+
process.exit(1);
117+
},
118+
complete() {
119+
console.log("✅ Complete! Data stored successfully.");
120+
client.destroy();
121+
sd.terminate();
122+
process.exit(0);
123+
},
124+
});
125+
},
85126
});
86-
87-
// console.log('✅ storing...');
88-
// bulletinAPI.tx.transactionStorage.store(dataToStore)
89-
// .signSubmitAndWatch(aliceSigner).subscribe({
90-
// next: (ev) => {
91-
// console.log("⏭️ store next: ", ev);
92-
// },
93-
// error: (err) => {
94-
// console.error("❌ store error: ", err);
95-
// process.exit(1);
96-
// },
97-
// });
98127
}
99128

100129
await main();

0 commit comments

Comments
 (0)