Key generation
This page shows the minimal JavaScript used in the seedless, disposable wallet framework to generate a private key from Passphrase and PIN inputs.
import { Buffer } from "buffer";
import { ethers } from "ethers";
import { scrypt } from "scrypt-js";
const HASH_OPTIONS = {
N: 131072, // CPU/memory cost parameter, 2^17
r: 8, // block size parameter
p: 1, // parallelization parameter
keyLen: 64,
};
const KDF_DOMAIN_SEPARATOR = "mybucks.online-core.generateHash.v2";
async function generatePrivateKey(passphrase, pin) {
const passwordBuffer = Buffer.from(passphrase);
const encoded = abi.encode(
["string", "string", "string"],
[KDF_DOMAIN_SEPARATOR, passphrase, pin],
);
const saltHash = ethers.keccak256(encoded);
const saltBuffer = Buffer.from(saltHash.slice(2), "hex");
const hashBuffer = await scrypt(
passwordBuffer,
saltBuffer,
HASH_OPTIONS.N,
HASH_OPTIONS.r,
HASH_OPTIONS.p,
HASH_OPTIONS.keyLen,
(p) => console.log(Math.floor(p * 100))
);
const hashHex = Buffer.from(hashBuffer).toString("hex");
const privateKey = ethers.keccak256(abi.encode(["string"], [hashHex]));
return privateKey;
}You can find the code here on Github. You can download the repository and execute it on your local machine as a confirmation and backup for account generation.
And the same codebase is encapsulated in the @mybucks.online/core npm package, facilitating developers in integrating wallet generation into third-party platforms or services.
Run in Sandbox
You can also execute this key-generation process in CodeSandbox without setting up a local environment. Please find the interactive CodeSandbox link here.
Last updated