Bitcoin-com / bitbox-sdk

BITBOX SDK for Bitcoin Cash
https://developer.bitcoin.com/bitbox
MIT License
87 stars 62 forks source link

Expand BITBOX with convenience methods #114

Open adrianbarwicki opened 5 years ago

adrianbarwicki commented 5 years ago

Convenience methods would make the library much easier to use and become more accessible to new developers.

At Honest, we maintain our own library wrapping BITBOX and extending it with some convenience methods. Was Bitbox to incorporate these, we could completely deprecate the simple-bitcoin-wallet wrapper and use Bitbox directly.

Proposal

In detail, it would be great to have the following methods:

Example:

const simpleWallet = BITBOX.initAccount("<mnemonic here>", {
     HdPath: "m/44'/0'/0'/1'";
});

Example:

Mark the support for OP_RETURNS:

const receivers = [
    {
        address: "bitcoincash:qp2rmj8heytjrksxm2xrjs0hncnvl08xwgkweawu9h",
        amountSat: 100000
    },
    {
        address: "bitcoincash:qp2rmj8heytjrksxm2xrjs0hncnvl08xwgkweawu9h",
        amountSat: 200000
    }, {
       opReturn: ["0x6d02", "Hello world!"]
    }
];

const tx = await simpleWallet.send(receivers);
rkalis commented 5 years ago

I like having a simple way to send BCH straight from bitbox-sdk, think it would be really valuable. But I think maybe rather than having to create a new wallet, it might integrate better with bitbox to do one of the following:

  1. Add send function to bitbox:
    
    bitbox.send(from: ECPair, receivers: ({ to: string, value: number } | { opReturn: string[] })[])

// Possible override for single send: bitbox.send(from: ECPair, to: string, value: number)


2. Extend ECPair with a `send` function:

```ts
const keypair: ECPair = ...
keypair.send(receivers: ({ to: string, value: number } | { opReturn: string[] })[])

// Again, possible override for single send:
const keypair: ECPair = ...
keypair.send(to: string, value: number)

To facilitate the simple creation of a wallet, we could instead add a simple creation of an ECPair, similar to what you're proposing.

const keypair: ECPair = ECPair.fromMnemonic("<mnemonic here>", {
     HdPath: "m/44'/0'/0'/1'";
});

That way we don't need to add any new classes such as BCHSingleAccountWalletInstance, instead using the primitives that are already in BITBOX, and extending their functionality.


These are my thoughts, what do you think @adrianbarwicki @cgcardona?

christroutner commented 4 years ago

These convenience methods are largely what I was trying to accomplish by creating bch-cli-wallet: https://github.com/christroutner/bch-cli-wallet

This library contains the next level of abstraction. BITBOX is a library for doing low-level tasks like generating mnemonics, EC key pairs, signing transactions, etc. bch-cli-wallet has high-level functions such as create-wallet, send bch, get address, update-balance etc.

rkalis commented 4 years ago

I sort of agree. But I think right now bch-cli-wallet functions more for people to look at the code on how to implement these kinds of things, and then implement similar code in their app (like we did with simple-bitcoin-wallet). Rather than people actually npm installing bch-cli-wallet and using it inside applications. If we want to keep bitbox as low-level as it is currently, it might be valuable to position bch-cli-wallet (or another project) as the logical layer on top of bitbox that can be readily used inside other applications (without requiring any situational code changes).

christroutner commented 4 years ago

Agreed. My thoughts have been going along the same line.

I'd like to extract a lot of the existing code in bch-cli-wallet as a stand-alone library, then refactor bch-cli-wallet to use that library. bch-cli-wallet is getting so complex that it's getting unwieldy trying to be both a library and a CLI app.