Skip to content
This repository has been archived by the owner on May 13, 2021. It is now read-only.

Add support for substrate following uos #75

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"standard-react"
],
"env": {
"es6": true
"es6": true,
"jest": true
},
"plugins": [
"react"
Expand Down
78 changes: 66 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

A React Component that handles generating and scanning QR codes compatible with Parity Signer mobile app ([iOS](https://itunes.apple.com/de/app/parity-signer/id1218174838?l=en&mt=8), [Android](https://play.google.com/store/apps/details?id=com.nativesigner)).

The generated QR code follows the [UOS](https://github.com/maciejhirsz/uos) specification.

## Install

```bash
Expand All @@ -13,24 +15,65 @@ npm install --save @parity/qr-signer
- `scan` - boolean, required - Whether to show the QR scanner or a QR code.
- `onScan` - function, required - Callback that will be executed with the data scanned from the QR code.
- `size` - number - Display width and height in pixels, QR code will be scaled if necessary.
- `account` - string, required if `scan === false` - Ethereum address, `0x` prefixed.
- `rlp` - string, required if `scan === false` unless `data` is present - RLP-encoded Ethereum transaction, `0x` prefixed.
- `data` - string, required if `scan === false` unless `rlp` is present - arbitrary byte data to sign, `0x` prefixed.
- `network` - `'ethereumLegacy' | 'substrate'`, required if `scan === false` - The network on which we want to show the QR code. This determines the shape of `payload`.
- `payload` - object, required if `scan === false` - The payload to sign, depending on network.
- `onError` - (err: string) => void - A callback function which is called when there's an error encoding the payload.

The `payload` object's shape depends on the network. Below are defined how the payload should look in each network `'ethereumLegacy'` or `'substrate'`:

#### Ethereum Legacy

The `payload` object must be one of the two following variants:

```
{
action: "signTransaction",
data: {
account: ADDRESS - string - Ethereum address, `0x` prefixed.
rlp: RLP - RLP-encoded Ethereum transaction, `0x` prefixed.
}
}
```

```
{
action: "signData",
data: {
account: ADDRESS - string - Ethereum address, `0x` prefixed.
data: RLP - Arbitrary byte data to sign, `0x` prefixed.
}
}
```

#### Substrate

The `payload` object must have the following fields:

- `account` - 32-byte Uint8Array - The account with which we wish to sign the payload.
- `crypto` - `'ed25519' | 'sr25519'` - Cryptographic algorithm for accounts, either Ed25519 or Schnorr/Ristretto x25519.
- `action` - `'signTransaction' | 'signTransactionHash' | 'signMessage'` - The action to perform when signing. Determines the shape of `data`.
- `data` - Uint8Array - Data accompanying the action, depends on `action.`

| `action` | `data` Type | `data` description |
| ----------------------- | ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `'signTransaction'` | Uint8Array | SCALE encoding of the tuple of transaction items `(nonce, call, era_description, era_header)`. Note: PolkadotJS Api's `SignaturePayload` type might be useful, see [docs](https://polkadot.js.org/api/types/#substrate-types). |
| `'signTransactionHash'` | 32-byte Uint8Array | Blake2s 32-byte hash of the SCALE encoding of the tuple of transaction items `(nonce, call, era_description, era_header)`. |
| `'signMessage'` | Uint8Array | Arbitrary message to sign. |

## Example

```jsx
import React, { Component } from 'react'
import React, { Component } from 'react';

import QrSigner from '@parity/qr-signer'
import QrSigner from '@parity/qr-signer';

class Example extends Component {
state = {
scan: false,
signature: ''
}
};

render () {
render() {
const { scan, signature } = this.state;

if (signature) {
Expand All @@ -42,17 +85,28 @@ class Example extends Component {
<QrSigner
size={300}
scan={scan}
account='0x007311b88A03af17dbb37B47ab7C9Ab556708D56'
rlp='0xeb808504a817c8008252089400255cf193f1ba6dd3ec08ebe62e393030f4dd34872386f26fc10000802a8080'
onScan={(signature) => this.setState({ signature })}
network="ethereumLegacy"
payload={{
action: 'signTransaction',
data: {
account: '0x007311b88A03af17dbb37B47ab7C9Ab556708D56',
rlp:
'0xeb808504a817c8008252089400255cf193f1ba6dd3ec08ebe62e393030f4dd34872386f26fc10000802a8080'
}
}}
onScan={signature => this.setState({ signature })}
/>
<button onClick={() => this.setState({ scan: !this.state.scan })}>Toggle Scan</button>
<button onClick={() => this.setState({ scan: !this.state.scan })}>
Toggle Scan
</button>
</div>
)
);
}
}
```

See [`<Substrate />` example](./example/src/Substrate.js) for an example with Substrate.

## License

GPLv3
Loading