altangent / lightnode-invoice

Lightning Network invoice protocol library
MIT License
12 stars 4 forks source link

Typescript Typings #5

Open cavanmflynn opened 5 years ago

cavanmflynn commented 5 years ago

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,
  };
}
bmancini55 commented 5 years ago

Thanks for writing the type defs! A PR against DefinitelyTyped would be my preference.

One other note is that this repo and package are going to be deprecated soon. The code is migrating over to the lightnode monorepo https://github.com/altangent/lightnode/tree/master/packages/lightnode-invoice and will be published under @lightnode/invoice.

With that, we have plans to make invoice building easier so there will be some additions to the API.

bmancini55 commented 5 years ago

@cavanmflynn the new repo is available at: https://github.com/altangent/lightnode/tree/master/packages/lightnode-invoice and is now published under @lightnode/invoice.

I'm going to archive this repo and deprecate the existing package.

Thanks!