Skip to content

Instantly share code, notes, and snippets.

@josepot
Last active July 31, 2023 07:40
Show Gist options
  • Save josepot/073563333b62168cc418482f6074a751 to your computer and use it in GitHub Desktop.
Save josepot/073563333b62168cc418482f6074a751 to your computer and use it in GitHub Desktop.
create polkadot-js extension backup file
import { fromHex, utf16StrToUtf8Bytes } from "@unstoppablejs/utils";
import { jsonEncrypt } from "@polkadot/util-crypto";
import { createPair, Keyring } from "@polkadot/keyring";
const keyToBytes = (input: string | Uint8Array): Uint8Array =>
typeof input === "string" ? fromHex(input) : input;
export function createBackUpFileContent(
addresses: Array<{
name: string;
password: string;
publicKey: string | Uint8Array;
privateKey: string | Uint8Array;
}>,
filePassword: string
) {
const keyring = new Keyring();
const pairs = addresses.map(({ name, publicKey, privateKey }) =>
createPair(
{ toSS58: keyring.encodeAddress, type: "sr25519" },
{ publicKey: keyToBytes(publicKey), secretKey: keyToBytes(privateKey) },
{
name,
whenCreated: Date.now(),
}
)
);
const encryptedPairs = utf16StrToUtf8Bytes(
JSON.stringify(
pairs.map((pair, idx) => pair.toJson(addresses[idx].password))
)
);
return {
...jsonEncrypt(encryptedPairs, ["batch-pkcs8"], filePassword),
accounts: pairs.map(({ address, meta }) => ({ address, meta })),
};
}
// I'm definitely not using these keys... Well, unless someone else uses them :P
const keys: Array<[privateKey: string, publicKey: string]> = [
[
"0x47fade4e38060d1c2f818aef6a563d7c5daac80b8c70c856feb1cfeed3f04c84",
"0xaec7317630727847e5248c32ecc225a5e3c575685754c762a7748b2040aaab4d",
],
[
"0x97c75e800b8ec87653ac8094ae761a55492c54564b2a1b2c4a69833143463067",
"0x34bcac21b7658e9ad90732bb5c18326aaff13b26ab934b3c83fdda7e7f94a322",
],
[
"0x430120a7b4d2b2a970f5654f973669c9c7d66a46d9f12d8ad72c85e05ed528f8",
"0xf648366356aa719dd19a35c463a663cf8aa614e77eb4fc6174395787ec2d6130",
],
[
"0xb77d6c13916ea41753c72089470d821f5fe09974f69058dbd7a3569f66262f39",
"0xb660610f3c021bf91344023090b66891fe97c37d2e01541d4314f446abb0c011",
],
];
const addresses = keys.map(([privateKey, publicKey], idx) => ({
publicKey,
privateKey,
name: `CompromisedKey_${idx}`,
password: `weakPassword${idx}`,
}));
console.log(
JSON.stringify(createBackUpFileContent(addresses, "ThisIsDangerousAF!"))
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment