Skip to content

Commit bbef8c6

Browse files
karolk91bkontur
andauthored
Allow custom hashing algo and codec for Upload page (#256)
* Allow custom hashing algo and codec for Upload page * Build errors --------- Co-authored-by: Branislav Kontur <bkontur@gmail.com>
1 parent b0792b6 commit bbef8c6

File tree

8 files changed

+34
-28
lines changed

8 files changed

+34
-28
lines changed

console-ui/.papi/descriptors/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "0.1.0-autogenerated.12823647032323857113",
2+
"version": "0.1.0-autogenerated.11803746693258403074",
33
"name": "@polkadot-api/descriptors",
44
"files": [
55
"dist"
188 Bytes
Binary file not shown.
188 Bytes
Binary file not shown.

console-ui/.papi/polkadot-api.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
"wsUrl": "wss://westend-bulletin-rpc.polkadot.io",
77
"metadata": ".papi/metadata/bulletin_westend.scale",
88
"genesis": "0xee1f44f62e68312c4852f37585941e9b64b5ceae539e4aa112ce9d3cf7bbe9fd",
9-
"codeHash": "0xa5411eee1eccdffb4f8c29d74cd4a69134a5c8f7452236c4a2d6ffa003bba040"
9+
"codeHash": "0xce76768a4d9db3a9dbea8acdefeec037a1119dafdc98c62c30c90453ef3a2c75"
1010
},
1111
"bulletin_paseo": {
1212
"wsUrl": "wss://paseo-bulletin-rpc.polkadot.io",
1313
"metadata": ".papi/metadata/bulletin_paseo.scale",
1414
"genesis": "0x744960c32e3a3df5440e1ecd4d34096f1ce2230d7016a5ada8a765d5a622b4ea",
15-
"codeHash": "0xa5411eee1eccdffb4f8c29d74cd4a69134a5c8f7452236c4a2d6ffa003bba040"
15+
"codeHash": "0xce76768a4d9db3a9dbea8acdefeec037a1119dafdc98c62c30c90453ef3a2c75"
1616
},
1717
"bulletin_dotspark": {
1818
"wsUrl": "wss://bulletin.dotspark.app",

console-ui/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

console-ui/src/lib/cid.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,14 @@ export function getCidInfo(cid: CID): {
107107
/**
108108
* Convert multihash code to runtime HashingAlgorithm enum
109109
*/
110-
export function toHashingEnum(mhCode: number): { type: string } {
110+
export function toHashingEnum(mhCode: number) {
111111
switch (mhCode) {
112112
case HASH_CODES.blake2b256:
113-
return { type: "Blake2b256" };
113+
return { type: "Blake2b256" as const, value: undefined };
114114
case HASH_CODES.sha256:
115-
return { type: "Sha2_256" };
115+
return { type: "Sha2_256" as const, value: undefined };
116116
case HASH_CODES.keccak256:
117-
return { type: "Keccak256" };
117+
return { type: "Keccak256" as const, value: undefined };
118118
default:
119119
throw new Error(`Unhandled multihash code: 0x${mhCode.toString(16)}`);
120120
}

console-ui/src/lib/helia.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ import { createHelia, type Helia } from "helia";
22
import { CID } from "multiformats/cid";
33
import { multiaddr } from "@multiformats/multiaddr";
44
import { blake2b256 } from "@multiformats/blake2/blake2b";
5+
import { sha256 } from "multiformats/hashes/sha2";
6+
import { from as hasherFrom } from "multiformats/hashes/hasher";
7+
import { keccak_256 } from "@noble/hashes/sha3";
8+
9+
const keccak256Hasher = hasherFrom({
10+
name: "keccak-256",
11+
code: 0x1b,
12+
encode: (input: Uint8Array) => keccak_256(input),
13+
});
514

615
export interface HeliaClientConfig {
716
peerMultiaddrs: string[];
@@ -55,7 +64,7 @@ export class HeliaClient {
5564

5665
// Create Helia node with blake2b256 hasher for Polkadot/Substrate CID compatibility
5766
this.helia = await createHelia({
58-
hashers: [blake2b256],
67+
hashers: [blake2b256, sha256, keccak256Hasher],
5968
libp2p: {
6069
connectionGater: {
6170
denyDialMultiaddr: async (maAddr) => {

console-ui/src/pages/Upload/Upload.tsx

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { useApi, useClient } from "@/state/chain.state";
2020
import { useSelectedAccount } from "@/state/wallet.state";
2121
import { useAuthorization } from "@/state/storage.state";
2222
import { formatBytes } from "@/utils/format";
23-
import { cidFromBytes } from "@/lib/cid";
23+
import { cidFromBytes, toHashingEnum } from "@/lib/cid";
2424
import { Binary } from "polkadot-api";
2525

2626
type HashAlgorithm = "blake2b256" | "sha256" | "keccak256";
@@ -108,29 +108,26 @@ export function Upload() {
108108
// Calculate expected CID
109109
const expectedCid = await cidFromBytes(data, codecConfig.codec, hashConfig.mhCode);
110110

111-
// Build transaction options with custom extension if non-default config
112-
const txOpts: Record<string, unknown> = {};
113-
// if (hashAlgorithm !== "blake2b256" || cidCodec !== "raw") {
114-
// txOpts.customSignedExtensions = {
115-
// ProvideCidConfig: {
116-
// value: {
117-
// codec: BigInt(codecConfig.codec),
118-
// hashing: toHashingEnum(hashConfig.mhCode),
119-
// },
120-
// },
121-
// };
122-
// }
123-
124-
// Create and submit transaction
125-
const tx = api.tx.TransactionStorage.store({
126-
data: Binary.fromBytes(data),
127-
});
111+
// Use store_with_cid_config for non-default CID settings, plain store otherwise
112+
const isCustomCid = hashAlgorithm !== "blake2b256" || cidCodec !== "raw";
113+
114+
const tx = isCustomCid
115+
? api.tx.TransactionStorage.store_with_cid_config({
116+
cid: {
117+
codec: BigInt(codecConfig.codec),
118+
hashing: toHashingEnum(hashConfig.mhCode),
119+
},
120+
data: Binary.fromBytes(data),
121+
})
122+
: api.tx.TransactionStorage.store({
123+
data: Binary.fromBytes(data),
124+
});
128125

129126
// Sign and submit
130127
const result = await new Promise<{ blockHash?: string; blockNumber?: number }>((resolve, reject) => {
131128
let resolved = false;
132129

133-
const subscription = tx.signSubmitAndWatch(selectedAccount.polkadotSigner, txOpts).subscribe({
130+
const subscription = tx.signSubmitAndWatch(selectedAccount.polkadotSigner).subscribe({
134131
next: (ev: any) => {
135132
console.log("TX event:", ev.type);
136133
if (ev.type === "txBestBlocksState" && ev.found && !resolved) {

0 commit comments

Comments
 (0)