ecadlabs / taquito

A library for building dApps on the Tezos Blockchain - JavaScript / TypeScript
https://taquito.io
Apache License 2.0
297 stars 115 forks source link

Wallet API #179

Closed jevonearth closed 3 years ago

jevonearth commented 4 years ago

Introduction

Taquito currently has a signer API that supports the signing of raw bytes. For a richer interaction with wallets (such as TezBridge), by using raw bytes the wallet would have to “un-forge” the operation in order to display amount, destination, parameters and so forth. This may be undesirable or unnecessary for many use-cases.

Different wallets offer different APIs for “pre-forged” operations or “compound” operations. TezBridge’s has an API named “inject_operation”, which is a composition of forging, signing, broadcasting and estimation.

Goal

We are proposing a new "wallet API" in Taquito that aims to provide a common abstraction for dApp/App developers building with Taquito, that want to prompt a user for actions from the users wallet.

The Wallet API is for user-facing applications. In contrast, the existing Taquito "contract" API's intended scope can be defined as being suitable for backend functionality that does not require interaction with a user's wallet.

The Wallet API is defined as an interface, and can have different concrete implementations of providers.

For the prototype of this Wallet API we have made two implementations that satisfy the Wallet API interface:

Assumptions for BeaconWallet provider

For the tzip-10 wallet provider, the Wallet API assumes that the wallet in use (on the other end of the dApp <--> Wallet relationship) provides functionalities outlined in the tzip-10 spec.

The wallet API assumes that the wallet is responsible for the following actions:

Returned data from BeaconWallet provider:

Example usage from a Taquito user perspective

Setting up using Beacon SDK

const beaconWallet = new BeaconWallet()
await beaconWallet.init() // Request permissions
Tezos.setProvider({wallet: beaconWallet})
Tezos.wallet.transfer({to: "tz1.....", amount: 0.001})

Setting up using TezBridge

Tezos.setProvider({wallet: new TezBridgeWallet()})
Tezos.wallet.transfer({to: "tz1.....", amount: 0.001})

Discover PKH

const pkh = await Tezos.wallet.pkh()

Originate

const op = Tezos.wallet.originate({params});
await op.confirmation()

console.log(op.fees)

Transaction / Smart contract call

Tezos.wallet.transfer({params})
const op = contract.methods.myEntrypoint(myEntypointParameter).send()
await op.confirmation()

console.log(op.fees)

Delegation

const op = Tezos.wallet.delegation({params})
await op.confirmation()

console.log(op.fees)

Each of these method calls would do an operation request using the interaction standard API

Notes

We also aim to expose our current implementation through the wallet API. The current implementation only delegates the signing to a signer (wallet). In this context Taquito would take care of:

const wallet = new LegacyWalletProvider()

Tezos.setProvider({wallet})

Beacon SDK implementation details

In the context of beacon-sdk and tzip-10, most of Taquito calls will rely on the the operationRequest message

sequenceDiagram
    participant dApp
    participant Taquito
    participant BeaconSDK
    participant Wallet

    dApp ->> BeaconSDK: Setup BeaconSDK
    activate BeaconSDK
    Note over dApp,BeaconSDK: Step above includes requesting permission
    dApp ->> Taquito: Setup Taquito
    activate Taquito

    dApp ->> Taquito: Tezos.wallet.transfer(params)
    Taquito ->> BeaconSDK: operationRequest
    BeaconSDK ->> Wallet: operationRequest
    Wallet ->> Wallet: Prompt user
    Wallet ->> Wallet: Prepare, Forge, Sign, Inject
    Wallet ->> BeaconSDK: operation hash
    BeaconSDK ->> Taquito: operation hash
    Taquito ->> dApp: Operation Object
    deactivate Taquito
    deactivate BeaconSDK

TezBridge implementation details

In the context of TezBridge most of Taquito calls will rely on the inject_operations request

Taquito specific concern

Right now operation object returned by Taquito assume that a dry run has been done locally and expect the result of this dry run in their constructor

See: https://github.com/ecadlabs/taquito/blob/master/packages/taquito/src/operations/operations.ts#L100

This is a current limitation of the operation object and will need to be reworked (or we provide a different implementation) in order to create an operation object only from the operation hash.

jevonearth commented 4 years ago

Tentative/Draft blog post: https://docs.google.com/document/d/13jYbTLcrnobRzhufJjr9hA1jlR4zuJH5dk9M_TEteaQ/edit

Innkst commented 3 years ago

This was complete and released in June/July 2020