Coder Social home page Coder Social logo

lightnode-invoice's People

Contributors

bmancini55 avatar maximilliangeorge avatar uiur avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

lightnode-invoice's Issues

Typescript Typings

I wrote some Typescript typings for this library, which I am using in my local project. I would be more than happy to open a PR against DefinitelyTyped so they can be published under the npm @types org. Alternatively, they could live in this project.

Let me know your thoughts. Thanks!

Types:

declare module 'lightnode-invoice' {
  /**
   * Decodes a bech32 encoded lightning invoice. Exceptions are thrown for invalid invoices.
   * @param invoice The bech32 encoded lightning invoice.
   */
  function decode(invoice: string): Invoice;

  /**
   * Encodes an invoice into a bech32 encoded lightning invoice.
   * @param invoice The invoice class instance.
   * @param privKey The private key used to sign the invoice.
   */
  function encode(invoice: Invoice, privKey: string): string;

  /**
   * Represents a payment invoice.
   */
  class Invoice {
    /**
     * Network prefix.
     */
    public network: 'bc' | 'tb' | 'crt' | 'sm';

    /**
     * Amount in bitcoin.
     */
    public amount: number;

    /**
     * Timestamp of the invoice.
     */
    public timestamp: number;

    /**
     * Raw fields that are known in BOLT 11.
     */
    public fields: any[];

    /**
     * Raw fields that are unknown in BOLT 11.
     */
    public unknownFields: any[];

    /**
     * Signature that was used to sign the invoice.
     */
    public signature: Signature;

    /**
     * Pubkey that was recovered from the signature or provided in an n field.
     */
    public pubkey: Pubkey

    /**
     * SHA256 of the data that was signed.
     */
    public hashData: Buffer;

    /**
     * Expiry time in seconds, defaults to 3600 (per BOLT 11).
     */
    public readonly expiry: number;

    /**
     * SHA256 of the payment_preimage provided in return for payment.
     */
    public paymentHash: Buffer;

    /**
     * Short description.
     */
    public shortDesc: string;

    /**
     * Hash of the long description.
     */
    public hashDesc: Buffer;

    /**
     * Optional pubkey of the payee node.
     */
    public payeeNode: Buffer;

    /**
     * min_final_cltv_expiry to use for the last node, defaults to 9 (per BOLT 11).
     */
    public minFinalCltvExpiry: number;

    /**
     * List of on-chain addresses to fall back if payment fails. Supports version 0, 17, 18 addresses.
     */
    public readonly fallbackAddresses: FallbackAddress[];

    /**
     * List of routes that should be used.
     */
    public readonly routes: Route[];

    /**
     * Add a P2PKH or P2SH address in base58check or bech32 encoding.
     * @param addrStr The address string.
     */
    public addFallbackAddress(addrStr: string): void;

    /**
     * Adds a new private route
     * @param routes The private routes
     */
    public addRoute(routes: Route[]): void;
  }

  export interface Signature {
    r: Buffer;
    s: Buffer;
    recoveryFlag: number;
  }

  export interface Pubkey {
    x: Buffer;
    y: Buffer;
  }

  export interface FallbackAddress {
    version: number;
    address: Buffer;
  }

  export interface Route {
    pubkey: Buffer;
    short_channel_id: Buffer;
    fee_base_msat: number;
    fee_proportional_millionths: number;
    cltv_expiry_delta: number;
  }

  export {
    decode,
    encode,
    Invoice,
  };
}

Encode lightning invoice

Ability to encode lightning invoices needs to be added. Will create a lightning invoice object and add a encode method that will serialize it into bech32. Test vectors should follow the reverse decode vectors.

Deal with amounts as strings instead of numbers

JS floats are imprecise and are limited to 32 bits. Numbers should probably be dealt with in satoshis as strings rather than bitcoins as numbers, and internally it should probably be managed using a library like bn.js.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.