# Key generation

```javascript
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](https://github.com/mybucks-online/key-generation/blob/master/index.js) 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](/concept/mybucks.online-core.md) 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](https://codesandbox.io/p/sandbox/mybucks-online-key-generation-sandbox-default-7jktdl).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.mybucks.online/concept/key-generation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
