3 ways to generate bitcoin wallet
By: 胡家維 Hu Kenneth
Tags: Bitcoin, Blockchain

Brain wallet

WIF wallet

Mnemonic

Brain wallet (Dangerous way to generate bitcoin wallet)

A brain wallet is a standard wallet that generates its address by hashing a passphrase to create a private key and therefore a public key and resultant address.

var bitcore = require(“bitcore-lib”); var input = new Buffer(“Hello! I am Kenneth”);

var hash = bitcore.crypto.Hash.sha256(input);

var bn = bitcore.crypto.BN.fromBuffer(hash);

//private key

var pk = new bitcore.PrivateKey(bn);

//public key

var addy = pk.toAddress(); console.log(“private key : “ + pk + “ , Public key : “ + addy);

private key : 65be4aa2cbc47df49336225155481f20305cf49ee8baf66edeb808bc4e7bc4c9

Public key : 14TbUVUR7BbzBvjKoURTgLNcpq6EbScEdF

Wallet Import Format

Wallet Import Format (WIF, also known as Wallet Export Format) is a way of encoding a private ECDSA key so as to make it easier to copy.

var bitcore = require(“bitcore-lib”); var pk = new bitcore.PrivateKey()

var WIF = pk.toWIF(); var address = pk.toAddress(); console.log(“private key : “ + pk + “ ,WIF key : “ + WIF + “ , Public key : “ + address);

private key : 773ad035a4e219a89fae40afa2ab44f5a0da52c67acac2dd1806dec45939e765 ,

WIF key : L1DUfxYGU7jcNteQx37CawZxhKJ5RnvJmofy6ndrn4gBUt1k5dWh ,

Public key : 1H9kHTxwWFKbSzber7ZHybC7mnLvAnfoc

Mnemonic

A mnemonic phrase, mnemonic recovery phrase or mnemonic seed is a list of words which store all the information needed to recover a Bitcoin wallet. Wallet software will typically generate a mnemonic backup phrase and instruct the user to write it down on paper. If the user’s computer breaks or their hard drive becomes corrupted, they can download the same wallet software again and use the paper backup to get their bitcoins back.

Anybody else who discovers the phrase can steal the bitcoins, so it must be kept safe like jewels or cash. For example, it must not be typed into any website.

Mnemonic phrases are an excellent way of backing up and storing bitcoins and so they are used by almost all well-regarded wallets.

var bitcore = require(“bitcore-lib”);

var Mnemonic = require(‘bitcore-mnemonic’); var code = new Mnemonic();

var input = new Buffer(code.toString());

var hash = bitcore.crypto.Hash.sha256(input); var bn = bitcore.crypto.BN.fromBuffer(hash);

var pk = new bitcore.PrivateKey(bn);

var WIF = pk.toWIF();

var address = pk.toAddress(); console.log(“Mnemonic : “ + code.toString() + “, WIF key : “ + WIF + “ ,private key : “ + pk + “ , Public key : “ + address);

Mnemonic : assault permit naive arrive pyramid earn clock prefer extra usage flat vacant,

WIF key : L1K9ogANtDUKr2G5zDXoHXvFLXaDDB7wp3fHd15Vjxh4vbsBPEvm ,

Private key : 7a26267393274d0c0e556549cc82c49cc430a51dcac263b6aa520ad7126cb5df ,

Public key : 1B1d326rgBQ7W36xXvg9GxFhuTHkqdWAf7

Transaction

K is sender (account1) and G is receiver (account2)

var bitcore = require(‘bitcore-lib’);

var Insight = require(‘bitcore-explorers’).Insight; var insight = new Insight(‘testnet’); insight.getUnspentUtxos(K_public_key, function (err, utxos) {

if (err) {

//Handle errors…

}

else {



var tx = bitcore.Transaction();

tx.from(utxos); // Feed information about what unspent outputs one can use

tx.to( G_public_key, 10000) // Add an output with the given amount of satoshis

tx.change(K_public_key); // Sets up a change address where the rest of the funds will go

tx.fee(50000); // Minimum non-dust amount

tx.sign(K_private_key); // Signs all the inputs it can

tx.serialize(); insight.broadcast(tx.toString(), function (error, returnedTxId) {

if (error) {

console.log(“error : “ + error);

} else {

console.log(“Transaction : “ + returnedTxId);

}

});

} });

UXTO （Unspent Transaction Outputs）

UXTO （Unspent Transaction Outputs）, An Unspent Transaction Output (UTXO) that can be spent as an input in a new transaction.

source : https://bitcoin.org/en/glossary/unspent-transaction-output