Skip to content

Commit c84636a

Browse files
committed
Last nit to the authorize and store
1 parent c598eaa commit c84636a

File tree

4 files changed

+27
-36
lines changed

4 files changed

+27
-36
lines changed

examples/api.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export async function authorizeAccount(typedApi, sudoSigner, who, transactions,
1818
}
1919

2020
export async function store(typedApi, signer, data) {
21-
console.log('⬆️ Storing data...');
21+
console.log('⬆️ Storing data with length=', data.length);
2222
const cid = await cidFromBytes(data);
2323

2424
const dataBytes = typeof data === 'string' ?

examples/authorize_and_store_papi.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,8 @@ import assert from "assert";
22
import { createClient } from 'polkadot-api';
33
import { getWsProvider } from 'polkadot-api/ws-provider';
44
import { cryptoWaitReady } from '@polkadot/util-crypto';
5-
import {authorizeAccount, fetchCid, store} from './api.js';
6-
import {
7-
setupKeyringAndSigners,
8-
AUTH_TRANSACTIONS,
9-
AUTH_BYTES,
10-
ALICE_ADDRESS,
11-
cidFromBytes
12-
} from './common.js';
5+
import { authorizeAccount, fetchCid, store} from './api.js';
6+
import { setupKeyringAndSigners, cidFromBytes } from './common.js';
137
import { bulletin } from './.papi/descriptors/dist/index.mjs';
148

159
const NODE_WS = 'ws://localhost:10000';
@@ -20,23 +14,27 @@ async function main() {
2014

2115
let client, resultCode;
2216
try {
17+
// Init WS PAPI client and typed api.
2318
client = createClient(getWsProvider(NODE_WS));
24-
2519
const bulletinAPI = client.getTypedApi(bulletin);
2620

27-
const { sudoSigner, whoSigner } = setupKeyringAndSigners();
21+
// Signers.
22+
const { sudoSigner, whoSigner, whoAddress } = setupKeyringAndSigners('//Alice', '//Alice');
2823

24+
// Data to store.
2925
const dataToStore = "Hello, Bulletin with PAPI - " + new Date().toString();
3026
let expectedCid = await cidFromBytes(dataToStore);
3127

28+
// Authorize an account.
3229
await authorizeAccount(
3330
bulletinAPI,
3431
sudoSigner,
35-
ALICE_ADDRESS,
36-
AUTH_TRANSACTIONS,
37-
AUTH_BYTES
32+
whoAddress,
33+
1,
34+
BigInt(dataToStore.length)
3835
);
39-
36+
37+
// Store data.
4038
const cid = await store(bulletinAPI, whoSigner, dataToStore);
4139
console.log("✅ Data stored successfully with CID:", cid);
4240

examples/authorize_and_store_papi_smoldot.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,7 @@ import { createClient } from 'polkadot-api';
55
import { getSmProvider } from 'polkadot-api/sm-provider';
66
import { cryptoWaitReady } from '@polkadot/util-crypto';
77
import { authorizeAccount, fetchCid, store } from './api.js';
8-
import {
9-
setupKeyringAndSigners,
10-
AUTH_TRANSACTIONS,
11-
AUTH_BYTES,
12-
ALICE_ADDRESS,
13-
cidFromBytes
14-
} from './common.js';
8+
import { setupKeyringAndSigners, cidFromBytes } from './common.js';
159
import { bulletin } from './.papi/descriptors/dist/index.mjs';
1610

1711
// Constants
@@ -60,25 +54,29 @@ async function main() {
6054

6155
let sd, client, resultCode;
6256
try {
57+
// Init Smoldot PAPI client and typed api.
6358
({ client, sd } = await createSmoldotClient());
6459
console.log(`⏭️ Waiting ${SYNC_WAIT_SEC} seconds for smoldot to sync...`);
6560
await new Promise(resolve => setTimeout(resolve, SYNC_WAIT_SEC * 1000));
66-
6761
const bulletinAPI = client.getTypedApi(bulletin);
6862

69-
const { sudoSigner, whoSigner } = setupKeyringAndSigners();
63+
// Signers.
64+
const { sudoSigner, whoSigner, whoAddress } = setupKeyringAndSigners('//Alice', '//Alice');
7065

66+
// Data to store.
7167
const dataToStore = "Hello, Bulletin with PAPI + Smoldot - " + new Date().toString();
7268
let expectedCid = await cidFromBytes(dataToStore);
7369

70+
// Authorize an account.
7471
await authorizeAccount(
7572
bulletinAPI,
7673
sudoSigner,
77-
ALICE_ADDRESS,
78-
AUTH_TRANSACTIONS,
79-
AUTH_BYTES
74+
whoAddress,
75+
1,
76+
BigInt(dataToStore.length)
8077
);
81-
78+
79+
// Store data.
8280
const cid = await store(bulletinAPI, whoSigner, dataToStore);
8381
console.log("✅ Data stored successfully with CID:", cid);
8482

examples/common.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ import { CID } from 'multiformats/cid'
44
import { Keyring } from '@polkadot/keyring';
55
import { getPolkadotSigner } from '@polkadot-api/signer';
66

7-
// Authorization constants
8-
export const AUTH_TRANSACTIONS = 32;
9-
export const AUTH_BYTES = 64n * 1024n * 1024n; // 64 MB
10-
export const ALICE_ADDRESS = "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY";
11-
127
export async function waitForNewBlock() {
138
// TODO: wait for a new block.
149
console.log('🛰 Waiting for new block...')
@@ -55,10 +50,10 @@ export function createSigner(account) {
5550
);
5651
}
5752

58-
export function setupKeyringAndSigners() {
53+
export function setupKeyringAndSigners(sudoSeed, accountSeed) {
5954
const keyring = new Keyring({ type: 'sr25519' });
60-
const sudoAccount = keyring.addFromUri('//Alice');
61-
const whoAccount = keyring.addFromUri('//Alice');
55+
const sudoAccount = keyring.addFromUri(sudoSeed);
56+
const whoAccount = keyring.addFromUri(accountSeed);
6257

6358
const sudoSigner = createSigner(sudoAccount);
6459
const whoSigner = createSigner(whoAccount);

0 commit comments

Comments
 (0)