Unverified Commit f300844c authored by Amaury Martiny's avatar Amaury Martiny Committed by GitHub
Browse files

fix: Small bugs here and there (#3)

* Use Kusama everywhere

* Refactor: No metadataRpc in createSignedTx

* Configurable era

* Regenerate docs

* Fix test
parent 63abd36f
......@@ -20,6 +20,7 @@
* [specVersion](_balancetransfer_.txinfo.md#specversion)
* [tip](_balancetransfer_.txinfo.md#tip)
* [to](_balancetransfer_.txinfo.md#to)
* [validityPeriod](_balancetransfer_.txinfo.md#validityperiod)
## Properties
......@@ -27,7 +28,7 @@
**address**: *string*
*Defined in [src/balanceTransfer.ts:16](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/4ad0632/src/balanceTransfer.ts#L16)*
*Defined in [src/balanceTransfer.ts:22](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/6203cdc/src/balanceTransfer.ts#L22)*
The ss-58 encoded address
......@@ -37,7 +38,7 @@ ___
**amount**: *number*
*Defined in [src/balanceTransfer.ts:20](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/4ad0632/src/balanceTransfer.ts#L20)*
*Defined in [src/balanceTransfer.ts:26](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/6203cdc/src/balanceTransfer.ts#L26)*
The amount to send
......@@ -47,7 +48,7 @@ ___
**blockHash**: *string*
*Defined in [src/balanceTransfer.ts:24](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/4ad0632/src/balanceTransfer.ts#L24)*
*Defined in [src/balanceTransfer.ts:30](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/6203cdc/src/balanceTransfer.ts#L30)*
The checkpoint hash of the block, in hex
......@@ -57,7 +58,7 @@ ___
**blockNumber**: *number*
*Defined in [src/balanceTransfer.ts:28](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/4ad0632/src/balanceTransfer.ts#L28)*
*Defined in [src/balanceTransfer.ts:34](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/6203cdc/src/balanceTransfer.ts#L34)*
The checkpoint block number (u32), in hex
......@@ -67,7 +68,7 @@ ___
**genesisHash**: *string*
*Defined in [src/balanceTransfer.ts:32](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/4ad0632/src/balanceTransfer.ts#L32)*
*Defined in [src/balanceTransfer.ts:38](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/6203cdc/src/balanceTransfer.ts#L38)*
The genesis hash of the chain, in hex
......@@ -77,7 +78,7 @@ ___
**metadataRpc**: *string*
*Defined in [src/balanceTransfer.ts:37](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/4ad0632/src/balanceTransfer.ts#L37)*
*Defined in [src/balanceTransfer.ts:43](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/6203cdc/src/balanceTransfer.ts#L43)*
The SCALE-encoded metadata, as a hex string. Can be retrieved via the RPC
call `state_getMetadata`
......@@ -88,7 +89,7 @@ ___
**nonce**: *number*
*Defined in [src/balanceTransfer.ts:41](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/4ad0632/src/balanceTransfer.ts#L41)*
*Defined in [src/balanceTransfer.ts:47](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/6203cdc/src/balanceTransfer.ts#L47)*
The nonce for this transaction,
......@@ -98,7 +99,7 @@ ___
**specVersion**: *number*
*Defined in [src/balanceTransfer.ts:45](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/4ad0632/src/balanceTransfer.ts#L45)*
*Defined in [src/balanceTransfer.ts:51](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/6203cdc/src/balanceTransfer.ts#L51)*
The current spec version for the runtime
......@@ -108,7 +109,7 @@ ___
**tip**: *number*
*Defined in [src/balanceTransfer.ts:49](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/4ad0632/src/balanceTransfer.ts#L49)*
*Defined in [src/balanceTransfer.ts:55](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/6203cdc/src/balanceTransfer.ts#L55)*
The tip for this transaction, in hex
......@@ -118,6 +119,17 @@ ___
**to**: *string*
*Defined in [src/balanceTransfer.ts:53](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/4ad0632/src/balanceTransfer.ts#L53)*
*Defined in [src/balanceTransfer.ts:59](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/6203cdc/src/balanceTransfer.ts#L59)*
The recipient address, ss-58 encoded
___
### validityPeriod
**validityPeriod**: *number*
*Defined in [src/balanceTransfer.ts:64](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/6203cdc/src/balanceTransfer.ts#L64)*
The amount of time (in second) the transaction is valid for. Will be
translated into a mortal era
......@@ -19,6 +19,7 @@ JSON format for an unsigned transaction
* [blockNumber](_balancetransfer_.unsignedtransaction.md#blocknumber)
* [era](_balancetransfer_.unsignedtransaction.md#era)
* [genesisHash](_balancetransfer_.unsignedtransaction.md#genesishash)
* [metadataRpc](_balancetransfer_.unsignedtransaction.md#metadatarpc)
* [method](_balancetransfer_.unsignedtransaction.md#method)
* [nonce](_balancetransfer_.unsignedtransaction.md#nonce)
* [specVersion](_balancetransfer_.unsignedtransaction.md#specversion)
......@@ -87,6 +88,17 @@ Defined in node_modules/@polkadot/types/types.d.ts:193
___
### metadataRpc
**metadataRpc**: *string*
*Defined in [src/balanceTransfer.ts:15](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/6203cdc/src/balanceTransfer.ts#L15)*
The SCALE-encoded metadata, as a hex string. Can be retrieved via the RPC
call `state_getMetadata`
___
### method
**method**: *string*
......
......@@ -12,8 +12,7 @@
### Variables
* [BLOCKTIME](_balancetransfer_.md#const-blocktime)
* [DEFAULT_MORTAL_LENGTH](_balancetransfer_.md#const-default_mortal_length)
* [ONE_MINUTE](_balancetransfer_.md#const-one_minute)
* [ONE_SECOND](_balancetransfer_.md#const-one_second)
### Functions
......@@ -25,23 +24,15 @@
**BLOCKTIME**: *6* = 6
*Defined in [src/balanceTransfer.ts:57](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/4ad0632/src/balanceTransfer.ts#L57)*
*Defined in [src/balanceTransfer.ts:68](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/6203cdc/src/balanceTransfer.ts#L68)*
___
### `Const` DEFAULT_MORTAL_LENGTH
### `Const` ONE_SECOND
**DEFAULT_MORTAL_LENGTH**: *number* = 240 * ONE_MINUTE
**ONE_SECOND**: *number* = 1 / BLOCKTIME
*Defined in [src/balanceTransfer.ts:59](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/4ad0632/src/balanceTransfer.ts#L59)*
___
### `Const` ONE_MINUTE
**ONE_MINUTE**: *number* = 60 / BLOCKTIME
*Defined in [src/balanceTransfer.ts:58](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/4ad0632/src/balanceTransfer.ts#L58)*
*Defined in [src/balanceTransfer.ts:69](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/6203cdc/src/balanceTransfer.ts#L69)*
## Functions
......@@ -49,7 +40,7 @@ ___
**balanceTransfer**(`info`: [TxInfo](../interfaces/_balancetransfer_.txinfo.md)): *[UnsignedTransaction](../interfaces/_balancetransfer_.unsignedtransaction.md)*
*Defined in [src/balanceTransfer.ts:68](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/4ad0632/src/balanceTransfer.ts#L68)*
*Defined in [src/balanceTransfer.ts:78](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/6203cdc/src/balanceTransfer.ts#L78)*
Construct a balance transfer transaction offline. Transactions can be
constructed in such a way that it is valid for at least 240 minutes
......
......@@ -12,9 +12,9 @@
### createSignedTx
**createSignedTx**(`unsigned`: [UnsignedTransaction](../interfaces/_balancetransfer_.unsignedtransaction.md), `signature`: string, `metadataRpc`: string): *string*
**createSignedTx**(`unsigned`: [UnsignedTransaction](../interfaces/_balancetransfer_.unsignedtransaction.md), `signature`: string): *string*
*Defined in [src/createSignedTx.ts:15](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/4ad0632/src/createSignedTx.ts#L15)*
*Defined in [src/createSignedTx.ts:14](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/6203cdc/src/createSignedTx.ts#L14)*
Serialize a signed transaction in a format that can be submitted over the
Node RPC Interface from the signing payload and signature produced by the
......@@ -26,6 +26,5 @@ Name | Type | Description |
------ | ------ | ------ |
`unsigned` | [UnsignedTransaction](../interfaces/_balancetransfer_.unsignedtransaction.md) | The JSON representing the unsigned transaction |
`signature` | string | Signature of the signing payload produced by the remote signer |
`metadataRpc` | string | - |
**Returns:** *string*
......@@ -14,7 +14,7 @@
**createSigningPayload**(`unsigned`: [UnsignedTransaction](../interfaces/_balancetransfer_.unsignedtransaction.md)): *string*
*Defined in [src/createSigningPayload.ts:11](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/4ad0632/src/createSigningPayload.ts#L11)*
*Defined in [src/createSigningPayload.ts:11](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/6203cdc/src/createSigningPayload.ts#L11)*
Construct the signing payload from an unsigned transaction and export it to
a remote signer (this is often called "detached signing")
......
......@@ -14,7 +14,7 @@
**decodeTx**(`signedTx`: string, `metadataRpc`: string): *Partial‹[TxInfo](../interfaces/_balancetransfer_.txinfo.md)›*
*Defined in [src/decodeTx.ts:17](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/4ad0632/src/decodeTx.ts#L17)*
*Defined in [src/decodeTx.ts:17](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/6203cdc/src/decodeTx.ts#L17)*
Parse the transaction information from an unsigned and signed transaction
offline
......
......@@ -14,7 +14,7 @@
**deriveAddress**(`publicKey`: string | Uint8Array, `ss58Format`: number): *string*
*Defined in [src/deriveAddress.ts:11](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/4ad0632/src/deriveAddress.ts#L11)*
*Defined in [src/deriveAddress.ts:11](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/6203cdc/src/deriveAddress.ts#L11)*
Derive an address from a cryptographic public key offline
......
......@@ -18,7 +18,7 @@
**generateKeypair**(): *[KeyringPair](../interfaces/_generatekeypair_.keyringpair.md)*
*Defined in [src/generateKeypair.ts:13](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/4ad0632/src/generateKeypair.ts#L13)*
*Defined in [src/generateKeypair.ts:15](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/6203cdc/src/generateKeypair.ts#L15)*
Generate a valid public/private keypair offline
......
......@@ -15,7 +15,7 @@
**EXTRINSIC_VERSION**: *4* = 4
*Defined in [src/util/constants.ts:3](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/4ad0632/src/util/constants.ts#L3)*
*Defined in [src/util/constants.ts:3](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/6203cdc/src/util/constants.ts#L3)*
___
......@@ -23,4 +23,4 @@ ___
**KUSAMA_SS58_FORMAT**: *2* = 2
*Defined in [src/util/constants.ts:1](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/4ad0632/src/util/constants.ts#L1)*
*Defined in [src/util/constants.ts:1](https://github.com/amaurymartiny/polkadotjs-wrapper/blob/6203cdc/src/util/constants.ts#L1)*
......@@ -7,7 +7,13 @@ import { EXTRINSIC_VERSION } from './util/constants';
/**
* JSON format for an unsigned transaction
*/
export interface UnsignedTransaction extends SignerPayloadJSON {} // eslint-disable-line @typescript-eslint/no-empty-interface
export interface UnsignedTransaction extends SignerPayloadJSON {
/**
* The SCALE-encoded metadata, as a hex string. Can be retrieved via the RPC
* call `state_getMetadata`
*/
metadataRpc: string;
}
export interface TxInfo {
/**
......@@ -51,12 +57,16 @@ export interface TxInfo {
* The recipient address, ss-58 encoded
*/
to: string;
/**
* The amount of time (in second) the transaction is valid for. Will be
* translated into a mortal era
*/
validityPeriod: number;
}
// Calculting Era. The default here allows for 240min mortal eras.
// Useful constants for calculting an Era.
const BLOCKTIME = 6;
const ONE_MINUTE = 60 / BLOCKTIME;
const DEFAULT_MORTAL_LENGTH = 240 * ONE_MINUTE;
const ONE_SECOND = 1 / BLOCKTIME;
/**
* Construct a balance transfer transaction offline. Transactions can be
......@@ -77,9 +87,10 @@ export function balanceTransfer(info: TxInfo): UnsignedTransaction {
blockNumber: createType(registry, 'BlockNumber', info.blockNumber).toHex(),
era: createType(registry, 'ExtrinsicEra', {
current: info.blockNumber,
period: DEFAULT_MORTAL_LENGTH
period: ONE_SECOND * info.validityPeriod
}).toHex(),
genesisHash: info.genesisHash,
metadataRpc: info.metadataRpc,
method,
nonce: createType(registry, 'Compact<Index>', info.nonce).toHex(),
specVersion: createType(registry, 'u32', info.specVersion).toHex(),
......
import { balanceTransfer } from './balanceTransfer';
import { createSignedTx } from './createSignedTx';
import { createSigningPayload } from './createSigningPayload';
import { metadataRpc, signWithAlice, TEST_TX_INFO } from './util/testUtil';
import { signWithAlice, TEST_TX_INFO } from './util/testUtil';
describe('createSignedTx', () => {
it('should work', async done => {
......@@ -9,7 +9,7 @@ describe('createSignedTx', () => {
const signingPayload = createSigningPayload(unsigned);
const signature = await signWithAlice(signingPayload);
const tx = createSignedTx(unsigned, signature, metadataRpc);
const tx = createSignedTx(unsigned, signature);
expect(tx).toBe(
'0x2d0284ffd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d003d0fdf8b55e6712b2766d80e9a4f527c3deb3d728a815db77df52d766643cdab3170e25ccd867a4d16c0a8f648b753cd95fed0b46ace6cc4e6e5942712409908eb5808000600ff96074594cccf1cd185fa8a72ceaeefd86648f8d45514f3ce33c31bdd07e4655d30'
);
......
......@@ -6,7 +6,6 @@ import { UnsignedTransaction } from './balanceTransfer';
* Serialize a signed transaction in a format that can be submitted over the
* Node RPC Interface from the signing payload and signature produced by the
* remote signer
*
* @param unsigned - The JSON representing the unsigned transaction
* @param signature - Signature of the signing payload produced by the remote
......@@ -14,11 +13,10 @@ import { UnsignedTransaction } from './balanceTransfer';
*/
export function createSignedTx(
unsigned: UnsignedTransaction,
signature: string,
metadataRpc: string
signature: string
): string {
const registry = new TypeRegistry();
registry.setMetadata(new Metadata(registry, metadataRpc));
registry.setMetadata(new Metadata(registry, unsigned.metadataRpc));
const extrinsic = createType(
registry,
......
......@@ -10,7 +10,7 @@ describe('decodeTx', () => {
const signingPayload = createSigningPayload(unsigned);
const signature = await signWithAlice(signingPayload);
const tx = createSignedTx(unsigned, signature, metadataRpc);
const tx = createSignedTx(unsigned, signature);
const txInfo = decodeTx(tx, metadataRpc);
......
......@@ -2,6 +2,8 @@ import { Keyring } from '@polkadot/keyring';
import { KeyringPair as KeyringPairBase } from '@polkadot/keyring/types';
import { mnemonicGenerate } from '@polkadot/util-crypto';
import { KUSAMA_SS58_FORMAT } from './util/constants';
/**
* A keyring pair
*/
......@@ -12,6 +14,7 @@ export interface KeyringPair extends KeyringPairBase {} // eslint-disable-line @
*/
export function generateKeypair(): KeyringPair {
const keyring = new Keyring();
keyring.setSS58Format(KUSAMA_SS58_FORMAT);
return keyring.addFromMnemonic(mnemonicGenerate());
}
......@@ -23,7 +23,8 @@ export const TEST_TX_INFO: TxInfo = {
nonce: 2,
specVersion: 1019,
tip: 0,
to: 'Fy2rsYCoowQBtuFXqLE65ehAY9T6KWcGiNCQAyPDCkfpm4s'
to: 'Fy2rsYCoowQBtuFXqLE65ehAY9T6KWcGiNCQAyPDCkfpm4s',
validityPeriod: 240 * 60
};
/**
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment