Cardano Improvement Proposals


CIP 30 - Cardano dApp-Wallet Web Bridge

Contents

Abstract

This documents describes a webpage-based communication bridge allowing webpages (i.e. dApps) to interface with Cardano wallets. This is done via injected javascript code into webpages. This specification defines the manner that such code is to be accessed by the webpage/dApp, as well as defining the API for dApps to communicate with the user's wallet. This document currently concerns the Shelley-Mary era but will have a second version once Plutus is supported. This specification is intended to cover similar use cases as web3 for Ethereum or EIP-0012 for Ergo. The design of this spec was based on the latter.

Motivation

In order to facilitate future dApp development, we will need a way for dApps to communicate with the user's wallet. While Cardano does not yet support smart contracts, there are still various use cases for this, such as NFT management. This will also lay the groundwork for an updated version of the spec once the Alonzo hardfork is released which can extend it to allow for Plutus support.

Specification

Version

The API specified in this document will count as version 0.1.0 for version-checking purposes below.

Data Types

Bytes

A hex-encoded string of the corresponding bytes.

cbor\

A hex-encoded string representing CBOR corresponding to T defined via CDDL either inside of the Shelley Mult-asset binary spec or, if not present there, from the CIP-0008 signing spec. This representation was chosen when possible as it is consistent across the Cardano ecosystem and widely used by other tools, such as cardano-serialization-lib, which has support to encode every type in the binary spec as CBOR bytes.

TransactionUnspentOutput

If we have CBOR specified by the following CDDL referencing the Shelley-MA CDDL:

transaction_unspent_output = [
      input: transaction_input,
      output: transaction_output,
    ]
    

then we define

type TransactionUnspentOutput = cbor<transaction_unspent_output>
    

This allows us to use the output for constructing new transactions using it as an output as the transaction_output in the Shelley Multi-asset CDDL does not contain enough information on its own to spend it.

Paginate

type Paginate = {|
      page: number,
      limit: number,
    |};
    

Used to specify optional pagination for some API calls. Limits results to {limit} each page, and uses a 0-indexing {page} to refer to which of those pages of {limit} items each. dApps should be aware that if a wallet is modified between paginated calls that this will change the pagination, e.g. some results skipped or showing up multiple times but otherwise the wallet must respect the pagination order.

Error Types

APIError

APIErrorCode {
        InvalidRequest: -1,
        InternalError: -2,
        Refused: -3,
        AccountChange: -4,
    }
    APIError {
        code: APIErrorCode,
        info: string
    }
    

DataSignError

DataSignErrorCode {
        ProofGeneration: 1,
        AddressNotPK: 2,
        UserDeclined: 3,
        InvalidFormat: 4,
    }
    type DataSignError = {
        code: DataSignErrorCode,
        info: String
    }
    

PaginateError

type PaginateError = {|
        maxSize: number,
    |};
    

{maxSize} is the maximum size for pagination and if the dApp tries to request pages outside of this boundary this error is thrown.

TxSendError

TxSendErrorCode = {
        Refused: 1,
        Failure: 2,
    }
    type TxSendError = {
        code: TxSendErrorCode,
        info: String
    }
    

TxSignError

TxSignErrorCode = {
        ProofGeneration: 1,
        UserDeclined: 2,
    }
    type TxSignError = {
        code: TxSignErrorCode,
        info: String
    }
    

Initial API

In order to initiate communication from webpages to a user's Cardano wallet, the wallet must provide the following javascript API to the webpage. A shared, namespaced cardano object must be injected into the page if it did not exist already. Each wallet implementing this standard must then create a field in this object with a name unique to each wallet containing a wallet object with the following methods. The API is split into two stages to maintain the user's privacy, as the user will have to consent to cardano.{walletName}.enable() in order for the dApp to read any information pertaining to the user's wallet with {walletName} corresponding to the wallet's namespaced name of its choice.

cardano.{walletName}.enable(): Promise\

Errors: APIError

This is the entrypoint to start communication with the user's wallet. The wallet should request the user's permission to connect the web page to the user's wallet, and if permission has been granted, the full API will be returned to the dApp to use. The wallet can choose to maintain a whitelist to not necessarily ask the user's permission every time access is requested, but this behavior is up to the wallet and should be transparent to web pages using this API. If a wallet is already connected this function should not request access a second time, and instead just return the API object.

cardano.{walletName}.isEnabled(): Promise\

Errors: APIError

Returns true if the dApp is already connected to the user's wallet, or if requesting access would return true without user confirmation (e.g. the dApp is whitelisted), and false otherwise. If this function returns true, then any subsequent calls to wallet.enable() during the current session should succeed and return the API object.

cardano.{walletName}.apiVersion: String

The version number of the API that the wallet supports.

cardano.{walletName}.name: String

A name for the wallet which can be used inside of the dApp for the purpose of asking the user which wallet they would like to connect with.

cardano.{walletName}.icon: String

A URI image (e.g. data URI base64 or other) for img src for the wallet which can be used inside of the dApp for the purpose of asking the user which wallet they would like to connect with.

Full API

Upon successful connection via cardano.{walletName}.enable(), a javascript object we will refer to as API (type) / api (instance) is returned to the dApp with the following methods. All read-only methods (all but the signing functionality) should not require any user interaction as the user has already consented to the dApp reading information about the wallet's state when they agreed to cardano.{walletName}.enable(). The remaining methods api.signTx() and api.signData() must request the user's consent in an informative way for each and every API call in order to maintain security.

The API chosen here is for the minimum API necessary for dApp <-> Wallet interactions without convenience functions that don't strictly need the wallet's state to work. The API here is for now also only designed for Shelley's Mary hardfork and thus has NFT support. When Alonzo is released with Plutus support this API will have to be extended.

api.getNetworkId(): Promise\

Errors: APIError

Returns the network id of the currently connected account. 0 is testnet and 1 is mainnet but other networks can possibly be returned by wallets. Those other network ID values are not governed by this document. This result will stay the same unless the connected account has changed.

api.getUtxos(amount: cbor\ = undefined, paginate: Paginate = undefined): Promise\

Errors: APIError, PaginateError

If amount is undefined, this shall return a list of all UTXOs (unspent transaction outputs) controlled by the wallet. If amount is not undefined, this request shall be limited to just the UTXOs that are required to reach the combined ADA/multiasset value target specified in amount, and if this cannot be attained, undefined shall be returned. The results can be further paginated by paginate if it is not undefined.

api.getBalance(): Promise\>

Errors: APIError

Returns the total balance available of the wallet. This is the same as summing the results of api.getUtxos(), but it is both useful to dApps and likely already maintained by the implementing wallet in a more efficient manner so it has been included in the API as well.

api.getUsedAddresses(paginate: Paginate = undefined): Promise\[]>

Errors: APIError

Returns a list of all used (included in some on-chain transaction) addresses controlled by the wallet. The results can be further paginated by paginate if it is not undefined.

api.getUnusedAddresses(): Promise\[]>

Errors: APIError

Returns a list of unused addresses controlled by the wallet.

api.getChangeAddress(): Promise\>

Errors: APIError

Returns an address owned by the wallet that should be used as a change address to return leftover assets during transaction creation back to the connected wallet. This can be used as a generic receive address as well.

api.getRewardAddresses(): Promise\[]>

Errors: APIError

Returns the reward addresses owned by the wallet. This can return multiple addresses e.g. CIP-0018.

api.signTx(tx: cbor\, partialSign: bool = false): Promise\>

Errors: APIError, TxSignError

Requests that a user sign the unsigned portions of the supplied transaction. The wallet should ask the user for permission, and if given, try to sign the supplied body and return a signed transaction. If partialSign is true, the wallet only tries to sign what it can. If partialSign is false and the wallet could not sign the entire transaction, TxSignError shall be returned with the ProofGeneration code. Likewise if the user declined in either case it shall return the UserDeclined code. Only the portions of the witness set that were signed as a result of this call are returned to encourage dApps to verify the contents returned by this endpoint while building the final transaction.

api.signData(addr: cbor\
, sigStructure: cbor\): Promise\

Errors: APIError, DataSignError

This endpoint is due to be updated/finalized soon, see discussion in the initial PR.

This endpoint utilizes the CIP-0008 signing spec for standardization/safety reasons. It allows the dApp to request the user to sign data conforming to said spec. The user's consent should be requested and the details of sig_structure shown to them in an informative way. The Please refer to the CIP-0008 spec for details on how to construct the sig structure.

api.submitTx(tx: cbor\): Promise\

Errors: APIError, TxSendError

As wallets should already have this ability, we allow dApps to request that a transaction be sent through it. If the wallet accepts the transaction and tries to send it, it shall return the transaction id for the dApp to track. The wallet is free to return the TxSendError with code Refused if they do not wish to send it, or Failure if there was an error in sending it (e.g. preliminary checks failed on signatures).

Implementations

nami-wallet