-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathapp.ts
52 lines (43 loc) · 1.18 KB
/
app.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import express from "express";
import * as bip39 from "bip39";
import * as bitcoin from "bitcoinjs-lib";
import BIP32Factory from "bip32";
import * as ecc from "tiny-secp256k1";
import cors from "cors";
// Initialize bip32 with the ECC library
const bip32 = BIP32Factory(ecc);
const network = bitcoin.networks.testnet;
const path = "m/44'/1'/0'/0";
const app = express();
const port = 5000;
app.use(
cors({
origin: "http://localhost:3000",
methods: ["GET", "POST"],
allowedHeaders: ["Origin", "X-Requested-With", "Content-Type"],
credentials: true,
})
);
function generateWallet() {
let mnemonic = bip39.generateMnemonic();
const seed = bip39.mnemonicToSeedSync(mnemonic);
const root = bip32.fromSeed(seed, network);
let account = root.derivePath(path);
let node = account.derive(0).derive(0);
let btcAddress = bitcoin.payments.p2pkh({
pubkey: node.publicKey,
network,
}).address;
return {
address: btcAddress,
key: node.toWIF(),
mnemonic: mnemonic,
};
}
app.get("/create-wallet", (req, res) => {
const wallet = generateWallet();
res.json(wallet);
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});