mirror of
https://github.com/datahaven-xyz/datahaven
synced 2026-05-24 01:38:32 +00:00
## Summary Add Storage Hub basic end to end test. This PR also include some fixes to allow Storage Hub node and datahaven node to run on the same network (local chain). Before that one was running on dev and the other one on the local chain. ## What changed * Added `storagehub.test.ts` e2e test. In this file we explicitly start the storagehub node using the launch function already used in the CI * Added Storage Hub backend the flow so it can be used in the e2e test * Fix the `--chain local` vs `--chain dev` issue. The storagehub nodes were started on the dev network and therefore they were never syncing with the datahaven node * Fix the folder permission issue in the CI by fixing the folder name * Added StorageHub javascript lib --------- Co-authored-by: Gonza Montiel <gonzamontiel@users.noreply.github.com> Co-authored-by: Ahmad Kaouk <56095276+ahmadkaouk@users.noreply.github.com> Co-authored-by: Gonza Montiel <gon.montiel@gmail.com>
53 lines
1.9 KiB
TypeScript
53 lines
1.9 KiB
TypeScript
import { secp256k1 } from "@noble/curves/secp256k1";
|
|
import { keccak_256 } from "@noble/hashes/sha3";
|
|
import { datahaven } from "@polkadot-api/descriptors";
|
|
import { createClient, type PolkadotClient, type TypedApi } from "polkadot-api";
|
|
import { withPolkadotSdkCompat } from "polkadot-api/polkadot-sdk-compat";
|
|
import { getPolkadotSigner, type PolkadotSigner } from "polkadot-api/signer";
|
|
import { getWsProvider } from "polkadot-api/ws-provider/node";
|
|
import { SUBSTRATE_FUNDED_ACCOUNTS } from "./constants";
|
|
import type { Prettify } from "./types";
|
|
|
|
// A signer for EVM like chains that use AccountId20 as their public address
|
|
export const getEvmEcdsaSigner = (privateKey: string): PolkadotSigner => {
|
|
const privateKeyBytes = Buffer.from(
|
|
privateKey.startsWith("0x") ? privateKey.slice(2) : privateKey,
|
|
"hex"
|
|
);
|
|
const publicAddress = keccak_256(secp256k1.getPublicKey(privateKeyBytes, false).slice(1)).slice(
|
|
-20
|
|
);
|
|
|
|
return getPolkadotSigner(publicAddress, "Ecdsa", (input) =>
|
|
signEcdsa(keccak_256, input, privateKeyBytes)
|
|
);
|
|
};
|
|
|
|
export const signEcdsa = (
|
|
hasher: (input: Uint8Array) => Uint8Array,
|
|
value: Uint8Array,
|
|
priv: Uint8Array
|
|
) => {
|
|
const signature = secp256k1.sign(hasher(value), priv);
|
|
const signedBytes = signature.toCompactRawBytes();
|
|
|
|
const result = new Uint8Array(signedBytes.length + 1);
|
|
result.set(signedBytes);
|
|
result[signedBytes.length] = signature.recovery;
|
|
|
|
return result;
|
|
};
|
|
|
|
export const createPapiConnectors = (
|
|
wsUrl?: string
|
|
): { client: PolkadotClient; typedApi: DataHavenApi } => {
|
|
const url = wsUrl ?? "ws://127.0.0.1:9944";
|
|
const client = createClient(withPolkadotSdkCompat(getWsProvider(url)));
|
|
|
|
return { client, typedApi: client.getTypedApi(datahaven) };
|
|
};
|
|
|
|
export const getPapiSigner = (person: keyof typeof SUBSTRATE_FUNDED_ACCOUNTS = "ALITH") =>
|
|
getEvmEcdsaSigner(SUBSTRATE_FUNDED_ACCOUNTS[person].privateKey);
|
|
|
|
export type DataHavenApi = Prettify<TypedApi<typeof datahaven>>;
|