# 초기화
yarn init
# bitcore-lib 설치
yarn add bitcore-lib
# axios 설치
yarn add axios
wallet.js
const bitcore = require("bitcore-lib");
const { testnet } = require("bitcore-lib/lib/networks");
const createWallet = () => {
const privateKey = new bitcore.PrivateKey();
const address = privateKey.toAddress(testnet); // 네트워크 선택 (기본값은 메인넷)
return {
privateKey: privateKey.toString(),
address: address.toString(),
};
};
module.exports = {
createWallet: createWallet,
};
app.js
const { createWallet } = require("./wallet");
const start = async () => {
const wallet = createWallet();
console.log(wallet);
};
start();
node app.js
{
privateKey: 'a0e2b014317b443f5fb1708b4193e9be4d9223013ca0ca729ebf0decf476bd91',
address: 'mwRoSmns2iWowMmsYAxwDukyTgaDx3qvGj'
}
테스트넷 코인 받기
- https://coinfaucet.eu/en/btc-testnet/
- https://kuttler.eu/en/bitcoin/btc/faucet/
- https://testnet-faucet.com/btc-testnet/
테스트넷 explorer
wallet.js
const axios = require("axios");
const bitcore = require("bitcore-lib");
const { testnet } = require("bitcore-lib/lib/networks");
...
// 비트코인 주소가 맞는지 확인
const validWalletAddress = async (address) => {
return bitcore.Address.isValid(address, testnet); // 네트워크 testnet
};
const getBalance = async (address) => {
const isValid = await validWalletAddress(address);
if (!isValid) {
return "invalid address";
}
const url = `https://api.blockcypher.com/v1/btc/test3/addrs/${address}/balance`;
const balance = await axios.get(url);
return balance.data.balance;
};
module.exports = {
createWallet: createWallet,
getBalance: getBalance,
};
app.js
const { getBalance } = require("./wallet");
const start = async () => {
const balance = await getBalance("mwRoSmns2iWowMmsYAxwDukyTgaDx3qvGj");
console.log("wallet balance : ", balance);
};
start();
node app.js
토큰발급
- 전송 api는 apiToken이 필요하므로 가입하여 토큰발급을 받는다.
- https://accounts.blockcypher.com/
const axios = require("axios");
const bitcore = require("bitcore-lib");
const { testnet } = require("bitcore-lib/lib/networks");
...
const send = async (to, value) => {
const privateKey = new bitcore.PrivateKey(
"a0e2b014317b443f5fb1708b4193e9be4d9223013ca0ca729ebf0decf476bd91"
);
const apiToken = {token};
// input, output 지정
let newtx = {
inputs: [{ addresses: ["mwRoSmns2iWowMmsYAxwDukyTgaDx3qvGj"] }],
outputs: [{ addresses: [to], value: value }],
};
// 새로운 트랜잭션 생성
const newTxData = await axios.post(
`https://api.blockcypher.com/v1/btc/test3/txs/new`,
JSON.stringify(newtx)
);
// 트랜잭션 서명
const tmptx = newTxData.data;
tmptx.pubkeys = [];
tmptx.signatures = tmptx.tosign.map((sign, n) => {
const pubkey = privateKey.toPublicKey().toString("hex");
tmptx.pubkeys.push(pubkey);
// private key를 가지고 서명
const signature = bitcore.crypto.ECDSA.sign(
Buffer.from(sign, "hex"),
privateKey,
"big"
).toString("hex");
return signature;
});
// 전송
axios
.post(
`https://api.blockcypher.com/v1/btc/test3/txs/send?token=${apiToken}`,
JSON.stringify(tmptx)
)
.then((finaltx) => {
console.log(finaltx.data);
})
.catch((err) => {
console.log(err);
});
};
module.exports = {
createWallet: createWallet,
getBalance: getBalance,
send: send,
};
app.js
const { send } = require("./wallet");
const start = async () => {
send("mogiyYzT98HWbXW4bDhpDg5VJBtWDBLUgS", 100000);
};
start();
node app.js