npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sendover

v1.3.27

Published

Tools for creating and paying invoices privately on Bitcoin SV

Downloads

1,102

Readme

sendover

Tools for creating and paying invoices privately on Bitcoin SV

The code is hosted on GitHub and the package is available through NPM.

Installation

npm i sendover

Example Usage

const sendover = require('sendover')

// The merchant generates a keypair.
// They put the public key on their website, and keep the private key secret.
const merchantKeypair = sendover.generateKeypair()

// The customer also generates a keypair.
const customerKeypair = sendover.generateKeypair()

// The customer and the merchant agree on an invoice number.
// The customer knows the invoice number.
const purchaseInvoiceNumber = '341-9945319'

// The customer can now generate a Bitcoin addres for the payment.
// After generating the address, the customer sends the payment.
const paymentAddress = sendover.getPaymentAddress({
  senderPrivateKey: customerKeypair.privateKey,
  recipientPublicKey: merchantKeypair.publicKey,
  invoiceNumber: purchaseInvoiceNumber
})

// After making the payment, the customer sends a few things to the merchant.
// - The Bitcoin transaction that contains the payment
// - The invoice number they have agreed upon
// - The customer's public key
// - Any SPV proofs needed for the merchant to validate and accept the transaction
const dataSentToMerchant = {
  customerPublicKey: customerKeypair.publicKey,
  paymentTransaction: '...', // transaction that pays money to the address
  invoiceNumber: purchaseInvoiceNumber,
  transactionSPVProofs: ['...'] // Any needed SPV proofs
}

// The merchant can now calculate the private key that unlocks the money.
const privateKey = sendover.getPaymentPrivateKey({
  senderPublicKey: dataSentToMerchant.customerPublicKey,
  recipientPrivateKey: merchantKeypair.privateKey,
  invoiceNumber: dataSentToMerchant.invoiceNumber
})

API

Links: API, Interfaces, Functions

Interfaces

Interface: SendOverDeriveKeyParams

Input params to the deriveKey function.

This function derives the child key given the root key.

The flags:

rootKey, identityKey, publicKey, and sharedSymmetricKey flags

can be combined with:

counterparty, protocolID and keyID

to derive the required key.

export interface SendOverDeriveKeyParams {
    key: Uint8Array;
    counterparty: string | "self" | "anyone" | bsv.PublicKey;
    protocolID: string | [
        number,
        string
    ];
    keyID: string;
    derivationIdentity: string;
    rootKey?: boolean;
    identityKey?: boolean;
    publicKey?: boolean;
    forSelf?: boolean;
    sharedSymmetricKey?: boolean;
    deriveFromRoot?: boolean;
    revealCounterpartyLinkage?: boolean;
    revealPaymentLinkage?: boolean;
}
Property revealPaymentLinkage

Optional, defaults to false.

revealPaymentLinkage?: boolean

Links: API, Interfaces, Functions


Functions

| | | --- | | asArray | | computePaymentContext | | deriveKey | | deriveKeyWithCache | | generateKeypair | | getPaymentAddress | | getPaymentAddressString | | getPaymentPrivateKey | | getPaymentPubKey | | getPaymentPubKeyString | | getProtocolInvoiceNumber |

Links: API, Interfaces, Functions


Function: generateKeypair

Generates a public/private keypair for the sending and receiving of invoices.

export function generateKeypair(params?: {
    returnType?: "hex" | "babbage-bsv";
}): {
    privateKey: string | bsv.PrivateKey;
    publicKey: string | bsv.PublicKey;
} 

Returns

The generated keypair, with privateKey and publicKey properties.

Argument Details

  • params
    • All parameters are given in an object
  • params.returnType
    • ='hex' Return type, either "hex" or "babbage-bsv"

Links: API, Interfaces, Functions


Function: getPaymentAddress

Returns a payment address for use by the sender, given the recipient's public key, the sender's private key and the invoice number.

export function getPaymentAddress(params: {
    senderPrivateKey: string | bsvJs.crypto.BN | bsvJs.PrivateKey;
    recipientPublicKey: string | bsvJs.PublicKey;
    invoiceNumber: string;
    revealCounterpartyLinkage?: boolean;
    revealPaymentLinkage?: boolean;
    returnType?: "address" | "publicKey" | "babbage-bsv";
}): string | bsvJs.PublicKey 

Returns

The destination address or public key

Argument Details

  • params
    • All parameters are provided in an object
  • params.senderPrivateKey
    • The private key of the sender in WIF format
  • params.recipientPublicKey
    • The public key of the recipient in hexadecimal DER format
  • params.invoiceNumber
    • The invoice number to use
  • params.revealCounterpartyLinkage
    • =false When true, reveals the root shared secret between the two counterparties rather than performing key derivation, returning it as a hex string
  • params.revealPaymentLinkage
    • =false When true, reveals the secret between the two counterparties used for this specific invoice number, rather than performing key derivation. Returns the linkage as a hex string
  • params.returnType
    • =address] The destination key return type, either address or publicKey

Links: API, Interfaces, Functions


Function: computePaymentContext

export function computePaymentContext(params: {
    senderPrivateKey: string | BigNumber | PrivateKey;
    recipientPublicKey: string | PublicKey;
    invoiceNumber: string;
}): {
    publicKey: PublicKey;
    sharedSecret: number[];
    hmac: number[];
} 

Links: API, Interfaces, Functions


Function: getPaymentPubKey

export function getPaymentPubKey(params: {
    senderPrivateKey: string | BigNumber | PrivateKey;
    recipientPublicKey: string | PublicKey;
    invoiceNumber: string;
}): PublicKey 

Returns

The destination public key

Argument Details

  • params
    • All parameters are provided in an object
  • params.senderPrivateKey
    • The private key of the sender in WIF format
  • params.recipientPublicKey
    • The public key of the recipient in hexadecimal DER format
  • params.invoiceNumber
    • The invoice number to use

Links: API, Interfaces, Functions


Function: getPaymentPubKeyString

export function getPaymentPubKeyString(params: {
    senderPrivateKey: string | BigNumber | PrivateKey;
    recipientPublicKey: string | PublicKey;
    invoiceNumber: string;
}): string 

Returns

The destination public key Base58 string

Argument Details

  • params
    • All parameters are provided in an object
  • params.senderPrivateKey
    • The private key of the sender in WIF format
  • params.recipientPublicKey
    • The public key of the recipient in hexadecimal DER format
  • params.invoiceNumber
    • The invoice number to use

Links: API, Interfaces, Functions


Function: getPaymentAddressString

export function getPaymentAddressString(params: {
    senderPrivateKey: string | BigNumber | PrivateKey;
    recipientPublicKey: string | PublicKey;
    invoiceNumber: string;
}): string 

Returns

The destination address as Base58 string

Argument Details

  • params
    • All parameters are provided in an object
  • params.senderPrivateKey
    • The private key of the sender in WIF format
  • params.recipientPublicKey
    • The public key of the recipient in hexadecimal DER format
  • params.invoiceNumber
    • The invoice number to use

Links: API, Interfaces, Functions


Function: getPaymentPrivateKey

Returns a private key for use by the recipient, given the sender's public key, the recipient's private key and the invoice number.

export function getPaymentPrivateKey(params: {
    recipientPrivateKey: string | bsv.crypto.BN | bsv.PrivateKey;
    senderPublicKey: string | bsv.PublicKey;
    invoiceNumber: string;
    revealCounterpartyLinkage?: boolean;
    revealPaymentLinkage?: boolean;
    returnType?: "wif" | "hex" | "buffer" | "babbage-bsv";
}): string | Buffer | bsv.PrivateKey 

Returns

The incoming payment key that can unlock the money.

Argument Details

  • params
    • All parametera ere provided in an object
  • params.recipientPrivateKey
    • The private key of the recipient in WIF format
  • params.senderPublicKey
    • The public key of the sender in hexadecimal DER format
  • params.invoiceNumber
    • The invoice number that was used
  • params.revealCounterpartyLinkage
    • =false When true, reveals the root shared secret between the two counterparties rather than performing key derivation, returning it as a hex string
  • params.revealPaymentLinkage
    • =false When true, reveals the secret between the two counterparties used for this specific invoice number, rather than performing key derivation. Returns the linkage as a hex string
  • params.returnType
    • =wif The incoming payment key return type, either wif or hex

Links: API, Interfaces, Functions


Function: deriveKey

This function derives the child key given the root key.

The rootKey, identityKey, publicKey, and sharedSymmetricKey flags can be combined with counterparty, protocolID and keyID to derive the needed keys.

export function deriveKey(params: SendOverDeriveKeyParams): string 

Returns

Hex string of key to return

Links: API, Interfaces, Functions


Function: deriveKeyWithCache

Modified deriveKey function that utilizes a caching mechanism. This function first checks if the result for the given parameters is already in the cache. If so, it returns the cached result. Otherwise, it proceeds with the derivation and stores the result in the cache.

export function deriveKeyWithCache(params: SendOverDeriveKeyParams): string 

Returns

Hex string of the derived key.

Argument Details

  • params
    • The input parameters for the key derivation.

Links: API, Interfaces, Functions


Function: getProtocolInvoiceNumber

export function getProtocolInvoiceNumber(params: {
    protocolID: string | [
        number,
        string
    ];
    keyID: number | string;
}): string 

Links: API, Interfaces, Functions


Function: asArray

Coerce a value to number[]

export function asArray(val: Buffer | string | number[], encoding?: BufferEncoding): number[] {
    let a: number[];
    if (Array.isArray(val))
        a = val;
    else if (Buffer.isBuffer(val))
        a = Array.from(val);
    else
        a = Array.from(Buffer.from(val, encoding || "hex"));
    return a;
}

Returns

input val if it is a number[]; if string converts to Buffer using encoding; uses Array.from to convert buffer to number[]

Argument Details

  • val
    • Buffer or string or number[]. If string, encoding param applies.
  • encoding
    • defaults to 'hex'

Links: API, Interfaces, Functions


Credits

Credit is given to the people who have worked on making these ideas into reality. In particular, we thank Xiaohui Liu for creating the first known implementation of private addresses using this scheme, and Dr. Craig Wright for first describing it.

License

The license for the code in this repository is the Open BSV License.