Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.
Sjors Provoost edited this page Feb 9, 2017 · 3 revisions

Note: this is still quite experimental. Consider using service-my-wallet-v3 instead.

Overview

Install

npm install blockchain/my-wallet-v3#v3.27.1

Load

In a file, or in a console (REPL)

Blockchain = require('blockchain-wallet-client')

A small hack is needed for the time being:

npm install xmlhttprequest
XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest
navigator = {userAgent: ""}
Blockchain.API.API_ROOT_URL = "https://api.blockchain.info/"

Create a wallet

email = '[email protected]'
pwd = 'very_weak'

Blockchain.MyWallet.createNewWallet(
  email,
  pwd,
  '',
  'EUR',
  'en',
  console.log,  // Success
  console.error // Error
)
// 1822bb00-5c33-46a9-bbcb-b75de17fca66

This returns your wallet identifier.

Login

user_id = '1822bb00-5c33-46a9-bbcb-b75de17fca66'
pwd = 'very_weak'
Blockchain.MyWallet.login(
  user_id,
  pwd,
  { twoFactor: null, sessionToken: null }, {}
).then(MyWallet.wallet.fetchAccountInfo)

It's a bit more complicated once you enable 2FA on the wallet.

Get funding address

account = MyWallet.wallet.hdwallet.accounts[0]
account.receiveAddress
// '1ALfF8Ps8bkZT1qyf2TVa1Lu3P74wUfog2'

Show account balance

// Loads balance and recent transactions
MyWallet.wallet.getHistory()
account.balance
// 113702 (in Satoshi = €0,70)

Spend coins

p = new Blockchain.Payment()
p
  .to('1AvPkgjx2RKFW6xeqKVoKjsmJMZWrqs17w')
  .from(0)
  .amount(50000) // In Satoshi = €0.35
  .build()
  .sign()
  .publish()

This spends from the first account (0).

Import private key

E.g. from a paper wallet.

wallet = Blockchain.MyWallet.wallet
pk = 'L3qqTb8FD1wWhfZjjyJSiwQZyp3oLbycBNB9So45bj4uAYrP5YmS'
wallet.importLegacyAddress(pk, '')
addr = wallet.keys[0].address;
// 1AvPkgjx2RKFW6xeqKVoKjsmJMZWrqs17w

Show balance:

addr.getBalance()
// 50000 // In Satoshi = €0.35

Monitor address

E.g. waiting for a payment

op = MyWallet.ws.msgAddrSub(account.receiveAddress)
// '{"op":"addr_sub","addr":"1ALfF8Ps8bkZT1qyf2TVa1Lu3P74wUfog2"}'
MyWallet.ws.send(op)

A websocket event will look like this:

{
	"op": "utx",
	"x": {
    "hash": "f22525fd3fdbac441267b4f68f13fb8f1cabd6d0967a48844647b91e3f9ef8ea",
		"inputs": [{
			"prev_out": {
			    "spent": true,
				"addr": "1Ea4SXJbd2z7NszrS23i5rAd4v7LjuuJCE", // Sender wallet
				"value": 3040127, // 0.003 BTC
			},
		}],
		"out": [{
			"spent": false,
			"addr": "1ALfF8Ps8bkZT1qyf2TVa1Lu3P74wUfog2", // Our wallet
		}, {
		   ... // Change
		}]

Process websocket message

MyWallet.getSocketOnMessage = (msg) => {
  obj = JSON.parse(msg)
  if (obj.op === 'utx') {
    if (obj.x.out.filter(
      (output) => output.addr === account.receiveAddress
    ).length) {
      console.log('Coins received!')
    }
  }    
}

Buy Bitcoin with NodeJS

Partner company handles the purchase.

Verify your email and whitelist your IP address in advanced settings before calling this.

MyWallet.wallet.external.addCoinify()
coinify = MyWallet.wallet.external.coinify
coinify.partnerId = ...
coinify.signup('NL','EUR').then(console.log)
// user: ....,
// offlineToken: 'AkwyQ...'

Partner account details are stored in the new metadata service, which uses a deterministic URL and encryption key, based on your BIP 39 seed.

Bitcoin goes straight to your wallet. Wallet communicates with partner API.

We use a JSON web token to prove to Coinify that we verified your email.

Ask quote and buy:

coinify.getBuyQuote(10 * 100, 'EUR', 'BTC').then((quote) => {
  console.log(quote.quoteAmount / 100000000, "BTC")
  quote.buy('card').then((trade) =>
    console.log(trade.address)
    // Do some manual stuff with your creditcard
  )
})

Monitor purchase address (web socket):

To simulate a purchase, call:

coinify.trades[0]._state = 'completed'

Then send some BTC to the address.

coinify.monitorPayments()
coinify.trades[0].watchAddress().then(() => console.log('Bitcoin Received!'))

Sign a message

  • Prove that you have the private key for an address:
addr = MyWallet.wallet.keys[0].address;
// 1AvPkgjx2RKFW6xeqKVoKjsmJMZWrqs17w
msg = 'Hallo Delft'
sig = MyWallet.wallet.keys[0].signMessage(msg)
// 'H2yfcyYDoSj+siL4rvMmnHIvKUDH4GThFHYGlQk28q79AhreibtQvmaH+5W6hRty+S1gQeFXbaKs04xjIsHRCOE='
  • Verify the proof:
Blockchain.Helpers.verifyMessage('1AvPkgjx2RKFW6xeqKVoKjsmJMZWrqs17w', sig, msg )
// true