Skip to content

Commit

Permalink
fix: Support cc3 (#481)
Browse files Browse the repository at this point in the history
* bump deps and metadata

* remove debugger

* update with polkadot/metadata

* fix test errors with new api

* fix sign message in cc3

* test fix

* fix warnings

* fix tests
  • Loading branch information
Tbaut authored and hanwencheng committed Dec 2, 2019
1 parent bfd67b3 commit bd3d927
Show file tree
Hide file tree
Showing 10 changed files with 214 additions and 223 deletions.
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"build-rust-ios": "cd rust/signer && make ios",
"build-rust-android": "cd rust/signer && make android",
"clean": "watchman watch-del-all && rm -rf /tmp/metro-bundler-cache-* && rm -rf /tmp/haste-map-react-native-packager-* && rm -rf node_modules/ && yarn cache clean --force && rm -rf ./android/app/build && rm -rf ./ios/build && yarn clean-rust",
"clean-rust" : "cd rust/signer && cargo clean && cd ../..",
"clean-rust": "cd rust/signer && cargo clean && cd ../..",
"commit": "commit-wizard",
"ios": "yarn run build-rust-ios && react-native run-ios",
"lint": "npx eslint . --ext .js,.jsx --ignore-path .gitignore",
Expand All @@ -37,10 +37,12 @@
"dependencies": {
"@plugnet/util": "^1.1.100",
"@plugnet/wasm-crypto-js": "^0.11.102",
"@polkadot/api": "^0.95.2",
"@polkadot/api": "^0.98.0-beta.0",
"@polkadot/metadata": "^0.97.1",
"@polkadot/reactnative-identicon": "^0.46.1",
"@polkadot/util": "^1.6.1",
"@polkadot/util-crypto": "1.6.1",
"@polkadot/types": "^0.98.0-beta.1",
"@polkadot/util": "^1.7.1",
"@polkadot/util-crypto": "1.7.1",
"@polkadot/wasm-crypto": "^0.14.1",
"@react-native-community/netinfo": "^4.1.5",
"@tradle/react-native-http": "^2.0.1",
Expand Down Expand Up @@ -111,8 +113,7 @@
},
"android.emu.debug": {
"binaryPath": "android/app/build/outputs/apk/debug/app-debug.apk",
"build":
"cd android && ./gradlew assembleDebug assembleAndroidTest -DtestBuildType=debug && cd ..",
"build": "cd android && ./gradlew assembleDebug assembleAndroidTest -DtestBuildType=debug && cd ..",
"type": "android.emulator",
"device": {
"avdName": "Nexus_5_API_28"
Expand Down
32 changes: 18 additions & 14 deletions specs/util/decoders.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

'use strict';
import '@polkadot/types/injector';

import {
createType,
GenericExtrinsicPayload,
GenericCall,
Metadata
TypeRegistry
} from '@polkadot/types';
import MetaData from '@polkadot/metadata';
import Call from '@polkadot/types/primitive/Generic/Call';
import { u8aConcat } from '@polkadot/util';
import { checkAddress, decodeAddress } from '@polkadot/util-crypto';
Expand All @@ -46,13 +45,12 @@ const SUBSTRATE_ID = new Uint8Array([0x53]);
const CRYPTO_SR25519 = new Uint8Array([0x01]);
const CMD_SIGN_MORTAL = new Uint8Array([0]);
const CMD_SIGN_MSG = new Uint8Array([3]);
const registry = new TypeRegistry();
new MetaData(registry, kusamaData);

const KUSAMA_ADDRESS = 'FF42iLDmp7JLeySMjwWWtYQqfycJvsJFBYrySoMvtGfvAGs';
const TEST_MESSAGE = 'THIS IS SPARTA!';

const metadata = new Metadata(kusamaData);
GenericCall.injectMetadata(metadata);

const RN_TX_REQUEST_RAW_DATA =
'4' + // indicates data is binary encoded
'37' + // byte length of data
Expand Down Expand Up @@ -82,7 +80,7 @@ const SIGNER_PAYLOAD_TEST = {
blockHash:
'0xde8f69eeb5e065e18c6950ff708d7e551f68dc9bf59a07c52367c0280f805ec7',
blockNumber: '0x231d30',
era: createType('ExtrinsicEra', { current: 2301232, period: 200 }),
era: createType(registry, 'ExtrinsicEra', { current: 2301232, period: 200 }),
genesisHash: SubstrateNetworkKeys.KUSAMA,
method:
'0x0600ffd7568e5f0a7eda67a82691ff379ac4bba4f9c9b859fe779b5d46363b61ad2db9e56c',
Expand All @@ -97,7 +95,9 @@ const SIGN_TX_TEST = u8aConcat(
CRYPTO_SR25519,
CMD_SIGN_MORTAL,
decodeAddress(KUSAMA_ADDRESS),
new GenericExtrinsicPayload(SIGNER_PAYLOAD_TEST, { version: 4 }).toU8a()
new GenericExtrinsicPayload(registry, SIGNER_PAYLOAD_TEST, {
version: 4
}).toU8a()
);

/* eslint-disable-next-line jest/no-disabled-tests */
Expand All @@ -107,10 +107,10 @@ describe.skip('sanity check', () => {
});

it('sanity check payload encodes as expected', () => {
const payload = new GenericExtrinsicPayload(SIGNER_PAYLOAD_TEST, {
const payload = new GenericExtrinsicPayload(registry, SIGNER_PAYLOAD_TEST, {
version: 4
});
const fromBytes = new GenericExtrinsicPayload(payload.toU8a(), {
const fromBytes = new GenericExtrinsicPayload(registry, payload.toU8a(), {
version: 4
});

Expand Down Expand Up @@ -213,11 +213,15 @@ describe('decoders', () => {
});

it('decodes Payload Method to something human readable with Kusama metadata', () => {
const payload = new GenericExtrinsicPayload(SIGNER_PAYLOAD_TEST, {
version: 4
});
const payload = new GenericExtrinsicPayload(
registry,
SIGNER_PAYLOAD_TEST,
{
version: 4
}
);

const call = new Call(payload.method);
const call = new Call(registry, payload.method);

expect(call).toBeDefined();
expect(call.args).toBeDefined();
Expand Down
10 changes: 5 additions & 5 deletions specs/util/identitiesUtils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ const accountIdFunding1 = 'address1',
accountIdEthereum = 'address6',
accountIdDefault = 'addressDefault',
paths = [
'//kusama_CC2//default',
'//kusama_CC2//funding/1',
'//kusama_CC2/softKey1',
'//kusama_CC2//funding/2',
'//kusama_CC2//staking/1',
'//kusama//default',
'//kusama//funding/1',
'//kusama/softKey1',
'//kusama//funding/2',
'//kusama//staking/1',
'//polkadot//default',
'1'
],
Expand Down
15 changes: 8 additions & 7 deletions specs/util/units.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@

'use strict';

import '@polkadot/types/injector';

import { GenericCall, Metadata } from '@polkadot/types';
import { TypeRegistry } from '@polkadot/types';
import MetaData from '@polkadot/metadata';
import Call from '@polkadot/types/primitive/Generic/Call';
import { formatBalance } from '@polkadot/util';

import kusamaData from '../../src/util/static-kusama';
import { fromWei } from '../../src/util/units';
const registry = new TypeRegistry();

new MetaData(registry, kusamaData);

describe('units', () => {
describe('ethereum', () => {
Expand All @@ -48,22 +50,21 @@ describe('units', () => {
let method_3;

beforeAll(() => {
const metadata = new Metadata(kusamaData);

GenericCall.injectMetadata(metadata);

formatBalance.setDefaults({
decimals: 12,
unit: 'KSM'
});

method_1 = new Call(
registry,
'0x0400ffd541fa133def7268cc0e5213aebf10ec04b822d59fb7556341f4e49911fc110a0b00b04e2bde6f'
);
method_2 = new Call(
registry,
'0x0400ffd541fa133def7268cc0e5213aebf10ec04b822d59fb7556341f4e49911fc110ae2d45a1d'
);
method_3 = new Call(
registry,
'0x0400ffd9d249ea49e9ae53fc0df3df40d3b070c88e387c265841fe2f3362970d864fdf1f0000606b82534ae05e4508'
);
});
Expand Down
2 changes: 0 additions & 2 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

import '../shim';

import '@polkadot/types/injector';

import React, { Component, PureComponent } from 'react';
import { Platform, StatusBar, View, YellowBox } from 'react-native';
import {
Expand Down
34 changes: 11 additions & 23 deletions src/components/PayloadDetailsCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

'use strict';

import { GenericCall, getTypeRegistry, Metadata } from '@polkadot/types';
import { TypeRegistry } from '@polkadot/types';
import MetaData from '@polkadot/metadata';
import Call from '@polkadot/types/primitive/Generic/Call';
import { formatBalance } from '@polkadot/util';
import { decodeAddress, encodeAddress } from '@polkadot/util-crypto';
Expand All @@ -35,6 +36,8 @@ import { shortString } from '../util/strings';
import fontStyles from '../fontStyles';
import { alertDecodeError } from '../util/alertUtils';

const registry = new TypeRegistry();

export default class PayloadDetailsCard extends React.PureComponent {
static propTypes = {
description: PropTypes.string,
Expand All @@ -44,13 +47,8 @@ export default class PayloadDetailsCard extends React.PureComponent {
style: ViewPropTypes.style
};

state = {
fallback: false
};

constructor(props) {
super(props);

// KUSAMA and KUSAMA_DEV have the same metadata and Defaults values
const isKusama =
this.props.prefix ===
Expand All @@ -62,33 +60,23 @@ export default class PayloadDetailsCard extends React.PureComponent {

let metadata;
if (isKusama) {
metadata = new Metadata(kusamaMetadata);

metadata = new MetaData(registry, kusamaMetadata);
// registry.setMetadata(metaData);
formatBalance.setDefaults({
decimals: SUBSTRATE_NETWORK_LIST[SubstrateNetworkKeys.KUSAMA].decimals,
unit: SUBSTRATE_NETWORK_LIST[SubstrateNetworkKeys.KUSAMA].unit
});
} else if (__DEV__ && isSubstrateDev) {
metadata = new Metadata(substrateDevMetadata);

metadata = new MetaData(registry, substrateDevMetadata);
formatBalance.setDefaults({
decimals:
SUBSTRATE_NETWORK_LIST[SubstrateNetworkKeys.SUBSTRATE_DEV].decimals,
unit: SUBSTRATE_NETWORK_LIST[SubstrateNetworkKeys.SUBSTRATE_DEV].unit
});
}

if (!metadata) {
this.setState({
fallback: true
});
}

getTypeRegistry().register({
Keys: 'SessionKeysPolkadot'
});

GenericCall.injectMetadata(metadata);
this.state = {
fallback: !metadata
};
}

render() {
Expand Down Expand Up @@ -153,7 +141,7 @@ function ExtrinsicPart({ label, fallback, prefix, value }) {
useEffect(() => {
if (label === 'Method' && !fallback) {
try {
const call = new Call(value);
const call = new Call(registry, value);

let methodArgs = {};

Expand Down
8 changes: 4 additions & 4 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ export const EthereumNetworkKeys = Object.freeze({

// genesisHash is used as Network key for Substrate networks
export const SubstrateNetworkKeys = Object.freeze({
KUSAMA: '0xe3777fa922cafbff200cadeaea1a76bd7898ad5b89f7848999058b50e715f636', // https://polkascan.io/pre/kusama-cc2/block/0
KUSAMA: '0xb0a8d493285c2df73290dfb7e61f870f17b41801197a149ca93654499ea3dafe', // https://polkascan.io/pre/kusama-cc2/block/0
KUSAMA_DEV:
'0xd0861d14ebd971da92298f729e46706216377356b0a196978d054972620f298d',
'0x17fcbdfd61fa90f732628a6fa9c416a011d58a2c7a980472e6a0868c87b60e44',
SUBSTRATE_DEV:
'0x0d667fd278ec412cd9fccdb066f09ed5b4cfd9c9afa9eb747213acb02b1e70bc' // substrate --dev commit ac6a2a783f0e1f4a814cf2add40275730cd41be1 hosted on wss://dev-node.substrate.dev .
});
Expand All @@ -66,9 +66,9 @@ const substrateNetworkBase = {
decimals: 12,
genesisHash: SubstrateNetworkKeys.KUSAMA,
logo: require('../res/img/logos/kusama.png'),
pathId: 'kusama_CC2',
pathId: 'kusama',
prefix: 2,
title: 'Kusama CC2',
title: 'Kusama',
unit: 'KSM'
},
[SubstrateNetworkKeys.KUSAMA_DEV]: {
Expand Down
15 changes: 10 additions & 5 deletions src/util/decoders.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

'use strict';

import { GenericExtrinsicPayload } from '@polkadot/types';
import { GenericExtrinsicPayload, TypeRegistry } from '@polkadot/types';
import {
hexStripPrefix,
hexToU8a,
Expand Down Expand Up @@ -52,6 +52,8 @@ import {
--- SQRC Filler Bytes
ec11ec11ec11ec // SQRC filler bytes
*/

const registry = new TypeRegistry();
export function rawDataToU8A(rawData) {
if (!rawData) {
return null;
Expand Down Expand Up @@ -170,10 +172,13 @@ export async function constructDataFromBytes(bytes, multipartComplete = false) {
switch (secondByte) {
case '00': // sign mortal extrinsic
case '02': // sign immortal extrinsic
extrinsicPayload = new GenericExtrinsicPayload(rawPayload, {
version: 4
});

extrinsicPayload = new GenericExtrinsicPayload(
registry,
rawPayload,
{
version: 4
}
);
data.action = isOversized ? 'signData' : 'signTransaction';
data.oversized = isOversized;
data.isHash = isOversized;
Expand Down
2 changes: 1 addition & 1 deletion src/util/static-kusama.js

Large diffs are not rendered by default.

Loading

0 comments on commit bd3d927

Please sign in to comment.