Money Button Documentation

Money Button Documentation

  • Money Button
  • API
  • Examples
  • Paymail
  • bsv

›Bitcoin SV Library (bsv)

Money Button

  • Money Button Overview
  • HTML
  • Javascript
  • React
  • Crypto Operations
  • Invisible Money Button
  • Paymail Signatures
  • Paymail Encryption

API

  • API Overview
  • Apps
  • Webhooks
  • Tokens
  • Currencies
  • Javascript Client
  • Authentication

    • Authentication Overview
    • OAuth
    • OAuth With JS Client

    API v1

    • Get User Identity
    • Get User Profile
    • Get User Balance
    • Get Payments
    • Get Payment By ID

    API v2

    • Get User Balances
    • Get Payments
    • Get Payment By ID
    • Get Assets
    • Create Asset
    • Modify Asset

Simple Fabriik Protocol for Tokens

  • Protocol Overview
  • Wallets Integration Guide
  • SFP Paymail Extension Build Action
  • SFP Paymail Extension Authorise Action

Examples

  • Examples Overview
  • OP_RETURN Scripts
  • Assets

Paymail

  • Paymail Overview
  • Paymail Video Series
  • Paymail Introduction
  • BRFC Specifications
  • Specification Documents
  • BRFC ID Assignment
  • Service Discovery
  • Host Discovery
  • Capability Discovery
  • Public Key Infrastructure
  • Payment Addressing
  • Payment Addressing (Basic Address Resolution)
  • Payment Addressing (Payer Validation)
  • Payment Addressing (Payee Approvals)
  • Payment Addressing (PayTo Protocol Prefix)
  • Verify Public Key Owner
  • P2P Transactions
  • P2P Payment Destination
  • P2P Payment Destination with Tokens Support
  • Asset Information
  • Recommendations

Bitcoin SV Library (bsv)

  • Bitcoin SV Library (bsv)
  • Big Numbers
  • Points (Elliptic Curves)
  • Hash Functions
  • Base 58
  • Private Keys
  • Public Keys
  • Addresses
  • ECDSA
  • Bitcoin Signed Messages
  • Signatures
  • HD Private Keys (BIP32)
  • HD Public Keys (BIP32)
  • Mnemonics (BIP39)
  • Script

HD Public Keys (BIP32)

Source code: hdpublickey.js

Note that HD Public Keys are handled automatically by Money Button behind the scenes and it is not necessary to deal with HD Public Keys directly unless you are building an advanced application.

The HDPubKey class is the public form of the HDPrivKey class. Please see the HD Private Key documentation for an important theoretical overview and video of how these keys work.

HDPubKey

Suppose one has generated a new cryptographically secure random extended private key like this:

let hdPrivKey = bsv.HDPrivKey.fromRandom()

console.log(hdPrivKey.toString())
// prints:
// xprv9s21ZrQH143K2LcEfSnFRH1JvdKAcuZj2C8kAzCDnvqC4kgo417hYmAYQKdYDSzQSnQMLWXjDG42TgWwdYqwhAWTWpEBG1ighLLNnVHNKxx

We can use this to derive a corresponding extended public key like this:

let hdPubKey = bsv.HDPubKey.fromHDPrivKey(hdPrivKey)

console.log(hdPubKey)
// prints:
// xpub661MyMwAqRbcEpghmUKFnQx3Uf9f2NHaPR4LyNbqMGNAwZ1wbYRx6ZV2FaimDygDPbrHYuii12mYCNwFRWnvXXKnh12CK17XMFGiqUYNwew

Notice how the extended private key starts with "xprv" and the extended public key starts with "xpub".

We can now derive corresponding keys. The private key can be used to derive other private keys and public keys, and the public key can be used to derive other public keys (but not private ones). Like so:

var hdPrivKey2 = hdPrivKey.deriveChild("m/5/2/8").toString()

console.log(hdPrivKey2.toString())
// prints:
// xprv9ymKnkscdL7pTQgQQVh4Depsm7Y4JZEbwQrhxGEvaawPe7CTk3LdGWyfxx7uCeCwL9YQpArGnXzGEUvVWNduXwByVDBPLHaQ67sGLSRiDHE

var hdPubKey2 = hdPubKey.deriveChild("m/5/2/8").toString()

console.log(hdPubKey2.toString())
// prints:
// xpub6CkgCGQWThg7ftksWXE4anmcK9NYi1xTJdnJkeeY8vUNWuXcHaespKJ9pDF2bFVSCSrPrxipzQPfgf5MvR4cS8KBFthjSurBc2d7zmA61FZ

The xpub can derive other xpubs and the xprv can derive other xprvs. Using the xprv we can derive this corresponding xpub:

console.log(bsv.HDPubKey.fromHDPrivKey(hdPrivKey2).toString())
xpub6CkgCGQWThg7ftksWXE4anmcK9NYi1xTJdnJkeeY8vUNWuXcHaespKJ9pDF2bFVSCSrPrxipzQPfgf5MvR4cS8KBFthjSurBc2d7zmA61FZ

But there is no way to derive the private key from the public key.

Now suppose we derive a hardened path:

var hdPrivKey2 = hdPrivKey.deriveChild("m/5'/2/8").toString()

console.log(hdPrivKey2.toString())
// prints:
// xprv9z79GdL4VLf2F69bZ3Zxem5zvVXMvngsHSge32HGUPsQgUEvMQdb7ATBVXtMzMYLjNb38F7J1d9gpWnhEYzCmoWJ8QYtGDWnYdwhJUjYQKK

var hdPubKey2 = hdPubKey.deriveChild("m/5'/2/8").toString()
// throws an error:
// Invalid argument: creating a hardened path requires an HDPrivKey

The hardened extended private key is different than the non-hardened key, and there is no way to derive any keys from the extended public key on a hardened path.

You can always derive the public key from an extended public key:

console.log(hdPubKey.publicKey.toString())
// prints:
// 02c23e9fc6a959bb5315159ac7438c5a6bff37c7197326d1060b176e3969d72af5

But not the private key:

console.log(hdPubKey.privateKey
// prints:
// undefined

Given an extended public key which lies on a hardened path, it is possible to derive a branch of non-hardened public keys. Here, using the private key, we derive an extended public key on a hardened path:

var hdPubKey3 = hdPrivKey.deriveChild("m/5'").hdPubKey
console.log(hdPubKey3.toString())
// prints:
// xpub69SAXkHK2SHsWU5hAxMMzFoRy4Je3GRusC7drBefmEvN8CpS4Cf9iGyFy9WNFMwgfYEZGkvGMVRm2h2ikFN38KSC1VftFMa669b3nrANzo6

Using this extended public key as the root of a branch, we now derive a child public key on a non-hardened path:

var hdPubKey4 = hdPubKey3.deriveChild("m/2/8")
console.log(hdPubKey4.toString())
// prints:
// xpub6D6Vg8rxKiDKTaE4f56y1u2jUXMrLFQiefcEqQgt2jQPZGa4twwqexmfLoxAjHTqRweEt7c8qyUgcbdsuz8k9qaqoFSz6MFuZEJrB2BCrAE

This child public key is the same as if we had usedthe private key with the full derivation path.

var hdPubKey5 = hdPrivKey.deriveChild("m/5'/2/8").hdPubKey
console.log(hdPubKey5.toString())
// prints:
// xpub6D6Vg8rxKiDKTaE4f56y1u2jUXMrLFQiefcEqQgt2jQPZGa4twwqexmfLoxAjHTqRweEt7c8qyUgcbdsuz8k9qaqoFSz6MFuZEJrB2BCrAE
← HD Private Keys (BIP32)Mnemonics (BIP39) →
  • HDPubKey
Money Button Documentation
Docs
Money ButtonAPIDesignbsv
Community
redditYoutubeTelegramTwitter
More
BlogInstagramGitHubStar
See an error in our documentation? Issue a pull request to fix it.
Copyright © 2022 Fermatted Drives Limited