본문 바로가기
blockchain

[BitcoinWallet] wallet에 필요한 private key, address 생성

by clolee 2022. 10. 27.

Bitcoin wallet 만들기 with javascript (node.js)

bitcore-lib, bitcore-mnemonic install

 

https://www.npmjs.com/package/bitcore-lib

https://www.npmjs.com/package/bitcore-mnemonic

 

npm i bitcore-lib
npm i bitcore-mnemonic

 

wallet에 필요한 private key, address 생성

 

  • bitcoin을 받기 위한 address
  • bitcoin을 사용하기 위한 private key

 

src/api/wallet.js

const { PrivateKey } = require("bitcore-lib");
const { mainnet, testnet } = require("bitcore-lib/lib/networks");

const Mnemonic = require("bitcore-mnemonic");

const createWallet = (network = mainnet) => {
  var privateKey = new PrivateKey();
  var address = privateKey.toAddress(network);
  return {
    privateKey: privateKey.toString(),
    address: address.toString(),
  };
};

module.exports = {
  createWallet: createWallet,
};

 

 

src/app.js

testnet 사용.

const { testnet } = require("bitcore-lib/lib/networks");
const createWallet = require("./api/wallet").createWallet;
// const sendBitcoin = require("./api/sendBitcoin");

console.log(createWallet(testnet));

 

node src/app.js 실행

testnet 사용시 주소는 일반적으로 n, m으로 시작

{
  privateKey: '34bb4f21e69bc71564819ebb440e05a05ede5f940efbc880d65a59e4b45df278',
  address: 'msooPWX1dYAhqUkbkziR3gGdSgDaKtE2Mk'
}

 

mainnet 으로 변경 후 app.js 실행해보면 결과 바뀌어 있음

 

 

참고 :

https://youtu.be/ObRnmvIdecI

https://github.com/bitpay/bitcore/tree/master/packages/bitcore-lib

https://github.com/bitpay/bitcore/blob/master/packages/bitcore-lib/docs/address.md

 

댓글