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
  • Simple Fabriik Protocol for Tokens

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

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
  • 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

Public Keys


Source code: publickey.js

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

A public key is a key that is derived from a private key and under normal circumstances can be shared publicly. Public keys can be used for encryption, Diffie-Helman shared secrets, and deriving addresses for receiving money. Mathematically, a public key is a point on the secp256k1 elliptic curve and it is equal to a private key times the base point.

In bsv, the PubKey class is wrapper of Point.

To generate a new private key and the corresponding public key, use:

let privateKey = bsv.PrivKey.fromRandom()
let publicKey = bsv.PubKey.fromPrivKey(privateKey)

If you wish to save or display a public key, you should use compressed hex format, and you can use the .toHex() method to do that. For example:

console.log(publicKey.toHex())

// prints:
// 0340a908047b5aa865e48f6c5917bc04c9d45e50ed81b43d10798b6aebe2e55ed9
// ...or whatever your public key is corresponding to the private key.

You can import a public key and export it again:

let str = '0340a908047b5aa865e48f6c5917bc04c9d45e50ed81b43d10798b6aebe2e55ed9'
let publicKey2 = bsv.PubKey.fromHex(str)

console.log(publicKey2.toHex())
// prints:
// 0340a908047b5aa865e48f6c5917bc04c9d45e50ed81b43d10798b6aebe2e55ed9

Notice how the length is 33 bytes:

console.log(publicKey2.toBuffer().length)
// prints:
// 33

That is because public keys are normally in compressed format, which means only the X value of the point is included. The y can be either odd or even, and the first byte of the buffer indicates that. The Y value can then be derived from the X value. See the point documentation for more information.

Although you will probably never need to do so in practice, you can convert a public key to uncompressed format this way:

let publicKey3 = bsv.PubKey.fromPoint(publicKey2.point, false)

console.log(publicKey3.toHex())
// prints:
// 0440a908047b5aa865e48f6c5917bc04c9d45e50ed81b43d10798b6aebe2e55ed9316ff7efb4bad174108f7d8d68635178c5f93145ab30dca239d815f8ac8a2e4b

console.log(publicKey3.toBuffer().length)
// prints:
// 65

Notice how in this case the public key is 65 bytes long as a buffer. The extra byte indicates that it is in uncompressed format. You can confirm by eye that the bytes 1 through 33 are in fact the same as the uncompressed form - that is the X value.

You can print out the point like this:

console.log(publicKey3.point.getX().toString())
// prints:
// 29246674798077863133660435020507606994691259363264582031446921457222776348377

console.log(publicKey3.point.getY().toString())
// prints:
// 22361160798578429449991880814502461758948300840999541549272530012209202998859

console.log(publicKey3.compressed)
// prints:
// false

console.log(publicKey2.point.getX().toString())
// prints:
// 29246674798077863133660435020507606994691259363264582031446921457222776348377

console.log(publicKey2.point.getY().toString())
// prints:
// 22361160798578429449991880814502461758948300840999541549272530012209202998859

console.log(publicKey2.compressed)
// prints:
// true

Notice how both public keys have the same point, but one is compressed and the other is not.

← Private KeysAddresses →
Money Button Documentation
Docs
Money ButtonAPIDesignbsv
Community
redditYoutubeTelegramTwitter
More
BlogInstagramGitHubStar
See an error in our documentation? Issue a pull request to fix it.
Copyright © 2021 Yours Inc.