@waves/signer

* [Overview](#overview) * [Restrictions](#restrictions) * [Getting Started](#getting-started) * [Constructor](#constructor) * [Methods](#methods) * [Provider Interface](#provider-interface) * [Error Codes](#error-codes)

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
@waves/signer
24401.1.02 years ago4 years agoMinified + gzip package size for @waves/signer in KB

Readme

Signer

Overview

Waves Signer is a TypeScript/JavaScript library that features signing and broadcasting transactions on behalf of users without asking them for their seed phrases or private keys.

Provider

In order to work with Signer, you need to link an external Provider library. Provider securely stores user's private data. Your web app and Signer itself do not have access to user's private key and seed phrase.
The Provider authenticates user and generates a digital signature.
Signer implements developer-friendly protocol for interacting with Provider as well as broadcasts transactions to the blockchain.
For now, you can use one of the following Providers:
  • ProviderSeed developed by Waves team creates user account from SEED. ProviderSeed can be used at the app debugging stage.
  • ProviderWeb developed by Waves.Exchange team uses an account created or imported into the Waves.Exchange web app via user's private key or seed phrase.
  • ProviderCloud developed by Waves.Exchange team uses an email-based Waves.Exchange.

You can also develop your own Provider, see the Provider Interface section below.

Signer + ProviderWeb: How It Works

When Signer requests to sign a transaction, ProviderWeb opens an iframe, where the user can review transaction details and confirm or reject it. Upon confirmation, ProviderWeb generates a digital signature.

Restrictions

Signer supports all types of transactions except Exchange transaction and Update Asset Info transaction.
Signer supports all browsers except Brave.

Getting Started

  1. Signer and Provider library installation

  • To install Signer library use
```bash
npm i @waves/signer
```
  • To install ProviderSeed developed by Waves team, use
```bash
npm i @waves/provider-seed @waves/waves-transactions
```
  • To install ProviderWeb developed by Waves.Exchange, use
```bash
npm i @waves.exchange/provider-web
```

For Windows, use the following format:

```bash
npm i '@waves.exchange/provider-web'
```
  • To install ProviderCloud developed by Waves.Exchange, use
```bash
npm i @waves.exchange/provider-cloud
```

For Windows, use the following format:

```bash
npm i '@waves.exchange/provider-cloud'
```

  1. Library initialization

Add library initialization to your app.
  • For Testnet & ProviderSeed:
```js
import { Signer } from '@waves/signer';
import { ProviderSeed } from '@waves/provider-seed';
import { libs } from '@waves/waves-transactions';

const seed = libs.crypto.randomSeed(15);
const signer = new Signer({
  // Specify URL of the node on Testnet
  NODE_URL: 'https://nodes-testnet.wavesnodes.com'
});
signer.setProvider(new ProviderSeed(seed));
```
  • For Testnet & Waves.Exchange ProviderWeb:
```js
import { Signer } from '@waves/signer';
import { ProviderWeb } from '@waves.exchange/provider-web';

const signer = new Signer({
  // Specify URL of the node on Testnet
  NODE_URL: 'https://nodes-testnet.wavesnodes.com'
});
signer.setProvider(new ProviderWeb('https://testnet.waves.exchange/signer/'))
```
  • For Testnet & Waves.Exchange ProviderCloud:
```js
import { Signer } from '@waves/signer';
import { ProviderCloud } from '@waves.exchange/provider-cloud';

const signer = new Signer({
  // Specify URL of the node on Testnet
  NODE_URL: 'https://nodes-testnet.wavesnodes.com'
});
signer.setProvider(new ProviderCloud('https://testnet.waves.exchange/signer/'))
```
  • For Mainnet & Waves.Exchange ProviderWeb:
```js
import { Signer } from '@waves/signer';
import { ProviderWeb } from '@waves.exchange/provider-web';

const signer = new Signer();
signer.setProvider(new ProviderWeb());
```
  • For Mainnet & Waves.Exchange ProviderCloud:
```js
import { Signer } from '@waves/signer';
import { ProviderCloud } from '@waves.exchange/provider-cloud';

const signer = new Signer();
signer.setProvider(new ProviderCloud());
```
After that you will be able to use Signer features in the app.

  1. Basic example

Now your application is ready to work with Waves blockchain. Let's test it by implementing basic functionality. For example, we could try to authenticate user, get his/her balances and transfer funds.
const user = await signer.login();
const balances = await signer.getBalance();
const [broadcastedTransfer] = await signer
  .transfer({amount: 100000000, recipient: 'alias:T:merry'}) // Transfer 1 WAVES to alias merry
  .broadcast(); // Promise will resolved after user sign and node response

const [signedTransfer] = await signer
  .transfer({amount: 100000000, recipient: 'alias:T:merry'}) // Transfer 1 WAVES to alias merry
  .sign(); // Promise will resolved after user sign

More examples

See example of an app that implements the donate button: .

Constructor

new Signer({
  NODE_URL: 'string',
})

Creates an object that features the following methods.
Parameters:
| Parameter | Default value | Description | | :--- | :--- | :--- | | NODEURL | https://nodes.wavesnodes.com | Node that is used to access a blockchain |

Methods

* [login](#login)
* [logout](#logout)
* [getBalance](#getbalance)
* [getSponsoredBalances](#getsponsoredbalances)
* [Common fields](#common-fields)
* [How to sign and broadcast transactions](#how-to-sign-and-broadcast-transactions)
* [alias](#alias)
* [burn](#burn)
* [cancelLease](#cancellease)
* [data](#data)
* [invoke](#invoke)
* [issue](#issue)
* [lease](#lease)
* [massTransfer](#masstransfer)
* [reissue](#reissue)
* [setAssetScript](#setassetscript)
* [setScript](#setscript)
* [sponsorship](#sponsorship)
* [transfer](#transfer)
* [batch](#batch)
* [broadcast](#broadcast)
* [getNetworkByte](#getnetworkbyte)
* [setProvider](#setprovider)
* [waitTxConfirm](#waittxconfirm)
In code you can use TypeScript types.

User Info

login

Authenticates user with his/her account; creates account if it don't exist.
login();

Returns: Promise of user data: address and public key.
Usage:
const {address, publicKey} = await signer.login();

Output example:
{
  address: '3P8pGyzZL9AUuFs9YRYPDV3vm73T48ptZxs',
  publicKey: 'FuChbN7t3gvW5esgARFytKNVuHSCZpXSYf1y3eDSruEN',
}

logout

Logs user out.
logout();

Returns: Promise\.
Usage:
await signer.logout();

getBalance

If user logged in, provides balances of assets in user's portfolio.
getBalance();

Returns: Promise of list of balances.
Usage:
const balances = await signer.getBalance();

Output example:
[{
  assetId: 'WAVES',
  assetName: 'Waves',
  decimals: 8,
  amount: 100000000,
  isMyAsset: false,
  tokens: 1,
  sponsorship: null,
  isSmart: false
},
{
  assetId: 'AcrRM9STdBu5PNiFveTCbRFTS8tADhKcsbC2KBp8A4tx',
  assetName: 'CoffeeCoin',
  decimals: 3,
  amount: 1500,
  isMyAsset: false,
  tokens: 1.5,
  isSmart: false,
  sponsorship: 500
}]

Output fields:
| Field name | Description | | :--- | :--- | | assetId | Base58 encoded ID of the asset | | assetName | Name of the asset | | decimals | Number of decimal places in the asset amount | | amount | Amount of asset multiplied by 10^decimals. For example, decimals of WAVES is 8, so the real amount is multipied by 10^8. { "WAVES": 677728840 } means 6.77728840 | | isMyAsset | true if current user is an asset issuer | | tokens | Amount of asset to display in app interface | | sponsorship | Amount of sponsored asset to be charged to users (per 0.001 WAVES) multiplied by 10^decimals
null if the asset is not sponsored | | isSmart | true for smart assets |

getSponsoredBalances

If user logged in, provides balances of sponsored assets in user's portfolio. See Sponsored Fee.
getSponsoredBalances();

Returns: Promise of list of balances.
Usage:
const sponsoredBalances = await signer.getSponsoredBalances();

Output example:
[{
  assetId: 'AcrRM9STdBu5PNiFveTCbRFTS8tADhKcsbC2KBp8A4tx',
  assetName: 'CoffeeCoin',
  decimals: 3,
  amount: 1500,
  isMyAsset: false,
  tokens: 1.5,
  isSmart: false,
  sponsorship: 500
}]

Output fields are the same as in getBalance method.

Create transactions

The following methods create transactions (but do not sign or broadcast them):

Check which of these transactions are supported by your Provider.

Common fields

Each create transaction method has optional fields that you don't specify manually in most cases:
| Field name | Description | Default value | | :--- | :--- | :--- | | chainId | 'W'.charCodeAt(0) or 87 means Mainnet
'T'.charCodeAt(0) or 84 means Testnet | Defined by configuration of Waves node that is set in Constructor | | fee | Transaction fee | Calculated automatically as described in Transaction fee section | | proofs | Array of transaction signatures | Added by sign or broadcast method (see How to Sign and Broadcast Transactions). If you specify a proof manually, it is also added to the array | | senderPublicKey | Base58 encoded public key of transaction sender | Returned by login method |

How to Sign and Broadcast Transactions

Each create transaction method returns object that features the sign and broadcast methods.
To sign transaction use sign method. For example:
signer.invoke({
   dApp: address,
   call: { function: name, args: convertedArgs },
}).sign();

To sign transaction and immediately send it to blockchain use broadcast method. For example:
signer.invoke({
   dApp: address,
   call: { function: name, args: convertedArgs },
}).broadcast();

Note: this broadcast method has the same options as the signer.broadcast method that is described below.
You can sign or broadcast several transactions at once. For example:
signer.alias({ 'new_alias', })
  .data([{ key: 'value', type: 'integer', value: 1 ])
  .transfer({ recipient: '3P8pGyzZL9AUuFs9YRYPDV3vm73T48ptZxs', amount: 10000 })
}).broadcast();

alias

Creates Create Alias transaction.
alias(data: {
  alias: 'string'
})

Parameters:
| Parameter name | Default value | Description | | :--- | :--- | :--- | | alias | | Short and easy to remember name of address. See Alias for more information |
\ Required parameter.
See Common fields
for other fields description.
Usage:
const data = {
  alias: 'new_alias',
}

const [tx] = await signer
  .alias(data)
  .broadcast();

burn

Creates Burn transaction.
burn(data: {
    assetId*: 'string',
    quantity*: LONG,
})

Parameters:
| Parameter name | Default value | Description | | :--- | :--- | :--- | | assetId | | Base58 encoded ID of the asset to burn | | quantity | | Amount of asset multiplied by 10^decimals. For example, decimals of WAVES is 8, so the real amount is multipied by 10^8. { "WAVES": 677728840 } means 6.77728840 |
\ Required parameter.
See Common fields
for other fields description.
Usage:
const data = {
  assetId: '4uK8i4ThRGbehENwa6MxyLtxAjAo1Rj9fduborGExarC',
  quantity: 100,
}

const [tx] = await signer
  .burn(data)
  .broadcast();

cancelLease

Creates Lease Cancel transaction.
cancelLease(data: {
    leaseId: 'string',
})

Parameters:
| Parameter name | Default value | Description | | :--- | :--- | :--- | | leasetId | | Base58 encoded ID of the Lease transaction |
\
Required parameter.
See Common fields for other fields description.
Usage:
const data = {
  leaseId: '69HK14PEHq2UGRfRYghVW8Kc3487uJaoUmk2ntT4kw7X',
}

const [tx] = await signer
  .cancelLease(data)
  .broadcast();

data

Creates Data transaction.
data(data: [{
  key: 'string',
  type: 'string' | 'integer' | 'binary' | 'boolean',
  value: 'string' | number | boolean,
])

Parameters:
| Parameter name | Default value | Description | | :--- | :--- | :--- | | key | | Key of a record. Maximum of 100 characters | | type | | Type of a record | | value | | Value of a record. Maximum of 5 Kbytes |
\ Required parameter.
See Common fields
for other fields description.
Usage:
const records = [
  { key: 'name', type: 'string', value: 'Lorem ipsum dolor sit amet' },
  { key: 'value', type: 'integer', value: 1234567 },
  { key: 'flag', type: 'boolean', value: true }
]

const [tx] = await signer
  .data({ data: records })
  .broadcast();