|
1 | 1 | import { cidFromBytes } from "./cid_dag_metadata.js"; |
2 | | -import { Binary } from '@polkadot-api/substrate-bindings'; |
3 | | - |
4 | | -export async function authorizeAccount(typedApi, sudoSigner, who, transactions, bytes) { |
5 | | - console.log('Authorizing account...'); |
6 | | - |
7 | | - const authorizeTx = typedApi.tx.TransactionStorage.authorize_account({ |
8 | | - who, |
9 | | - transactions, |
10 | | - bytes |
11 | | - }); |
12 | | - |
13 | | - const sudoTx = typedApi.tx.Sudo.sudo({ |
14 | | - call: authorizeTx.decodedCall |
15 | | - }); |
| 2 | +import { Binary, Enum } from '@polkadot-api/substrate-bindings'; |
| 3 | + |
| 4 | +export async function authorizeAccount( |
| 5 | + typedApi, |
| 6 | + sudoSigner, |
| 7 | + whos, |
| 8 | + transactions, |
| 9 | + bytes, |
| 10 | + txMode = TX_MODE_IN_BLOCK |
| 11 | +) { |
| 12 | + const accounts = Array.isArray(whos) ? whos : [whos]; |
| 13 | + |
| 14 | + console.log( |
| 15 | + `⬆️ Authorizing accounts: ${accounts.join(', ')} ` + |
| 16 | + `for transactions: ${transactions} and bytes: ${bytes}...` |
| 17 | + ); |
| 18 | + |
| 19 | + // TODO: rewrite with batch |
| 20 | + for (const who of accounts) { |
| 21 | + const auth = await typedApi.query.TransactionStorage.Authorizations.getValue(Enum("Account", who)); |
| 22 | + console.log(`ℹ Account: ${who} Authorization info: `, auth); |
| 23 | + if (auth != null) { |
| 24 | + const authValue = auth.extent; |
| 25 | + const accountTransactions = authValue.transactions; |
| 26 | + const accountBytes = authValue.bytes; |
| 27 | + |
| 28 | + if (accountTransactions > transactions && accountBytes > bytes) { |
| 29 | + console.log('✅ Account authorization is sufficient.'); |
| 30 | + continue; |
| 31 | + } |
| 32 | + } else { |
| 33 | + console.log('ℹ️ No existing authorization found — requesting new one...'); |
| 34 | + } |
| 35 | + |
| 36 | + const authorizeTx = typedApi.tx.TransactionStorage.authorize_account({ |
| 37 | + who, |
| 38 | + transactions, |
| 39 | + bytes |
| 40 | + }); |
| 41 | + const sudoTx = typedApi.tx.Sudo.sudo({ |
| 42 | + call: authorizeTx.decodedCall |
| 43 | + }); |
16 | 44 |
|
17 | | - // Wait for inclusion in best block (finalization can be unreliable with light clients) |
18 | | - await waitForTransaction(sudoTx, sudoSigner, "Authorize", TX_MODE_IN_BLOCK); |
| 45 | + await waitForTransaction(sudoTx, sudoSigner, "Authorize", txMode); |
| 46 | + } |
19 | 47 | } |
20 | 48 |
|
21 | | -export async function store(typedApi, signer, data) { |
| 49 | +export async function store(typedApi, signer, data, txMode = TX_MODE_IN_BLOCK) { |
22 | 50 | console.log('⬆️ Storing data with length=', data.length); |
23 | 51 | const cid = await cidFromBytes(data); |
24 | 52 |
|
25 | 53 | // Convert data to Uint8Array then wrap in Binary for PAPI typed API |
26 | | - const dataBytes = typeof data === 'string' ? |
27 | | - new Uint8Array(Buffer.from(data)) : |
28 | | - new Uint8Array(data); |
| 54 | + const bytes = |
| 55 | + typeof data === 'string' |
| 56 | + ? new Uint8Array(Buffer.from(data)) |
| 57 | + : data instanceof Uint8Array |
| 58 | + ? data |
| 59 | + : new Uint8Array(data); |
| 60 | + const binaryData = new Binary(bytes); |
29 | 61 |
|
30 | | - const binaryData = Binary.fromBytes(dataBytes); |
31 | 62 | const tx = typedApi.tx.TransactionStorage.store({ data: binaryData }); |
32 | | - |
33 | | - // Wait for inclusion in best block (finalization can be unreliable with light clients) |
34 | | - await waitForTransaction(tx, signer, "Store", TX_MODE_IN_BLOCK); |
| 63 | + await waitForTransaction(tx, signer, "Store", txMode); |
35 | 64 | return cid; |
36 | 65 | } |
37 | 66 |
|
|
0 commit comments