hive-uri
v0.2.5
Published
Hive URI parser and encoder
Downloads
783
Readme
Hive URI protocol
Protocol facilitating signing of hive transactions. Meant to be implemented by secure Hive wallet applications.
This repository contains both the specification and a zero dependency reference implementation that works in node.js and most browsers.
Installation
Via npm or yarn:
npm install hive-uri
yarn add hive-uri
Manually: clone the repository and run make
, this will place the built lib in lib/index.js
.
Example usage
Encoding operations:
const hiveuri = require('hive-uri')
hiveuri.encodeOp(['vote', {voter: 'foo', author: 'bar', permlink: 'baz', weight: 10000}])
// hive://sign/op/WyJ2b3RlIix7InZvdGVyIjoiZm9vIiwiYXV0aG9yIjoiYmFyIiwicGVybWxpbmsiOiJiYXoiLCJ3ZWlnaHQiOjEwMDAwfV0.
hiveuri.encodeOps([
['vote', {voter: 'foo', author: 'bar', permlink: 'baz', weight: 10000}],
['transfer', {from: 'foo', to: 'bar', amount: '10.000 HIVE', memo: 'baz'}]
], {callback: 'https://example.com/wallet?tx={{id}}'})
// hive://sign/ops/W1sidm90ZSIseyJ2b3RlciI6ImZvbyIsImF1dGhvciI6ImJhciIsInBlcm1saW5rIjoiYmF6Iiwid2VpZ2h0IjoxMDAwMH1dLFsidHJhbnNmZXIiLHsiZnJvbSI6ImZvbyIsInRvIjoiYmFyIiwiYW1vdW50IjoiMTAuMDAwIFNURUVNIiwibWVtbyI6ImJheiJ9XV0.?cb=aHR0cHM6Ly9leGFtcGxlLmNvbS93YWxsZXQ_dHg9e3tpZH19
Decoding and resolving hive:// links (for wallet implementers):
const hiveuri = require('hive-uri')
// parse the hive:// link
const parsed = hiveuri.decode(link)
// resolve the decoded tx and params to a signable tx
let {tx, signer} = hiveuri.resolveTransaction(parsed.tx, parsed.params, {
// e.g. from a get_dynamic_global_properties call
ref_block_num: 1234,
ref_block_prefix: 5678900,
expiration: '2020-01-01T00:00:00',
// accounts we are able to sign for
signers: ['foo', 'bar'],
// selected signer if none is asked for by the params
preferred_signer: 'foo',
})
// sign broadcast the transaction to the network
let signature = signTx(tx, myKeys[signer])
let confirmation
if (!parsed.params.no_broadcast) {
tx.signatures = [signature]
confirmation = broadcastTx(tx)
}
// redirect to the callback if set
if (parsed.params.callback) {
let url = hiveuri.resolveCallback(parsed.params.callback, {
sig: signature,
id: confirmation.id,
block: confirmation.block_num,
txn: confirmation.txn_num,
})
redirectTo(url)
}
Specification
A protocol that allows Hive transactions and operations to be encoded into links that can be shared across applications and devices to sign transactions without implementers having to reveal their private key.
Actions
hive://sign/tx/<base64u(JSON-encoded tx)>
Sign an arbitrary transaction.hive://sign/op/<base64u(JSON-encoded op)>
As above but constructs a transaction around the operation before signing.hive://sign/ops/<base64u(JSON-encoded op array)>
As above but allows multiple operations as an array.hive://sign/<operation_name>[/operation_params..]
Action aliases, see the "Specialized actions" section for more info.
To facilitate re-usable signing URIs the implementation allows for a set of placeholder variables that can be used in a signing payload.
__signer
- Replaced with the username of the signer__expiration
- Replaced with current time plus some padding to allow the transaction to be broadcast*__ref_block_num
- Reference block number*__ref_block_prefix
- Reference block id*
*Reasonable values are up to the implementer, suggested expiry time is 60 seconds ahead of rpc node time and the reference block set to the current head block.
Parameters
Params are global to all actions and encoded as query string params.
s
(signer) - Preferred signer, if the implementer has multiple signing keys available it should pre-fill the correct authority. If the implementer does not have a key for the preferred signer a warning should be shown to the user. If omitted the implementer may auto select a signer based on the transaction parameters.nb
(no_broadcast) - If set the implementer should only sign the transaction and pass the signature back in the callback.cb
(callback) - Base64u encoded url that will be redirected to when the transaction has been signed. The url also allows some templating, see the callback section below for more info.
Params uses short names to save space in encoded URIs.
Callbacks
Callbacks should be redirected to once the transaction has been accepted by the network. If the callback url is a web link only https
should be allowed.
The callbacks also allow simple templating with some response parameters, the templating format is {{<param_name>}}
, e.g. https://myapp.com/wallet?tx={{id}}&included_in={{block}}
or mymobileapp://signed/{{sig}}
Callback template params:
sig
- Hex-encoded string containing the 65-byte transaction signatureid
- Hex-encoded string containing the 20-byte transaction hash*block
- The block number the transaction was included in*txn
- The block transaction index*
*Will not be available if the nb
param was set for the action.
Base64u
An URL-safe version base64 where +
is replaced by -
, /
by _
and the =
padding by .
.
base64
SGn+dGhlcmUh/k5pY2X+b2b+eW91/nRv/mRlY29kZf5tZf46KQ==
base64u
SGn-dGhlcmUh_k5pY2X-b2b-eW91_nRv_mRlY29kZf5tZf46KQ..
JavaScript implementation:
b64u_lookup = {'/': '_', '_': '/', '+': '-', '-': '+', '=': '.', '.': '='}
b64u_enc = (str) => btoa(str).replace(/(\+|\/|=)/g, (m) => b64u_lookup[m])
b64u_dec = (str) => atob(str.replace(/(-|_|\.)/g, (m) => b64u_lookup[m]))
Specialized actions
To keep the length of the URIs short, and the QR code size manageable, some common operations have aliases. See list below for supported aliases, params noted in operation payloads using the format {{param_name}}
and optional ([param_name]
) params should be filled with empty strings unless otherwise specified:
Transfer tokens
Action: hive://sign/transfer/<username>/<amount>[/memo]
Params:
username
- User that should be followed by__signer
amount
- Amount to transfer, e.g.1.000 HIVE
memo
- Base64u encoded memo, optional.
Operation:
["transfer", {
"from": "__signer",
"to": "{{username}}",
"amount": "{{amount}}",
"memo": "{{memo}}"
}]
Follow user
Action: hive://sign/follow/<username>
Params:
<username>
- User that should be followed by__signer
.
Operation:
["custom_json", {
"required_auths": [],
"required_posting_auths": ["__signer"],
"id": "follow",
"json": "[\"follow\",{\"follower\":\"__signer\",\"following\":\"{{username}}\",\"what\":[\"blog\"]}]"
}]
Examples
Example usage of the protocol along with data for every step that can be used in tests. Examples assumes a __signer
of foo
a __expiration
of 1970-01-01T00:00:00
and __ref_block_num
, __ref_block_prefix
set to 0
unless otherwise stated.
Send a limit order
Might be requested from a trading app, here we don't use any templating since we never want the transaction to be reusable.
Transaction:
{
"ref_block_num": 48872,
"ref_block_prefix": 1543858519,
"expiration": "2018-05-29T13:17:39",
"extensions": [],
"operations": [
["limit_order_create2", {
"owner": "foo",
"orderid": 1,
"amount_to_sell": "10.000 HIVE",
"fill_or_kill": false,
"exchange_rate": {"base": "1.000 HIVE", "quote": "0.420 HBD"},
"expiration": "2018-05-30T00:00:00"
}]
]
}
Parameters:
{
"signer": "foo",
"callback": "https://hive.trader/sign_callback?id={{id}}"
}
Encoded:
hive://sign/tx/eyJyZWZfYmxvY2tfbnVtIjo0ODg3MiwicmVmX2Jsb2NrX3ByZWZpeCI6MTU0Mzg1ODUxOSwiZXhwaXJhdGlvbiI6IjIwMTgtMDUtMjlUMTM6MTc6MzkiLCJleHRlbnNpb25zIjpbXSwib3BlcmF0aW9ucyI6W1sibGltaXRfb3JkZXJfY3JlYXRlMiIseyJvd25lciI6ImZvbyIsIm9yZGVyaWQiOjEsImFtb3VudF90b19zZWxsIjoiMTAuMDAwIFNURUVNIiwiZmlsbF9vcl9raWxsIjpmYWxzZSwiZXhjaGFuZ2VfcmF0ZSI6eyJiYXNlIjoiMS4wMDAgU1RFRU0iLCJxdW90ZSI6IjAuNDIwIFNCRCJ9LCJleHBpcmF0aW9uIjoiMjAxOC0wNS0zMFQwMDowMDowMCJ9XV19?s=foo&cb=aHR0cHM6Ly9zdGVlbS50cmFkZXIvc2lnbl9jYWxsYmFjaz9pZD17e2lkfX0.
Witness vote
Reusable witness vote URI, e.g. for a "Vote for me!" QR code t-shirt.
Operation:
["account_witness_vote", {
"account": "__signer",
"witness": "jesta",
"approve": true
}]
Encoded:
hive://sign/op/WyJhY2NvdW50X3dpdG5lc3Nfdm90ZSIseyJhY2NvdW50IjoiX19zaWduZXIiLCJ3aXRuZXNzIjoiamVzdGEiLCJhcHByb3ZlIjp0cnVlfV0.
Multisig
To sign for an account setup with multiple authorities a central service can act as a transaction facilitator using the nb
(no_broadcast) option.
In the following scenario the account foo
is setup with an active authority that has three account auths belonging to bob
, alice
and picard
, the weights are setup so that two of those three accounts needs to sign.
bob
wants to transfer 150.000 HIVE
from the foo
account to himself so he submits an operation to the signing service:
["transfer", {
"from": "foo",
"to": "bob",
"amount": "150.000 HIVE",
"memo": "Bob's boat needs plastic padding"
}]
The service then generates a signing URI with that operation and the following options:
{
"no_broadcast": true,
"callback": "https://sign.hive.vc/collect?id=123&sig={{sig}}"
}
hive://sign/op/WyJ0cmFuc2ZlciIseyJmcm9tIjoiZm9vIiwidG8iOiJib2IiLCJhbW91bnQiOiIxNTAuMDAwIFNURUVNIiwibWVtbyI6IkJvYidzIGJvYXQgbmVlZHMgcGxhc3RpYyBwYWRkaW5nIn1d?nb=&cb=aHR0cHM6Ly9zaWduLnN0ZWVtLnZjL2NvbGxlY3Q_aWQ9MTIzJnNpZz17e3NpZ319
bob
then signs the transaction using the URI, the service callback is pinged and the service now has his signature. Then he sends the URI to alice
and picard
and when one of them signs it the service has enough signatures it broadcasts the transaction.
The UX of a service like this can be excellent with the help of QR codes and collecting emails for signers so they can be notified when a signature is needed and when the transaction is broadcast.