Skip to content
Snippets Groups Projects
Commit 814b71c8 authored by Hugo Trentesaux's avatar Hugo Trentesaux
Browse files

formatting and fix

addV2Account arg type
missing fields in currency in network service
parent 0cd5f384
Branches hugo-dev
No related tags found
No related merge requests found
import {Injectable} from "@angular/core";
import {ApiPromise, WsProvider} from "@polkadot/api";
import {SettingsService} from "../settings/settings.service";
import {Peer, Peers} from "./peer.model";
import {StartableService} from "@app/shared/services/startable-service.class";
import {abbreviate} from "@app/shared/currencies";
import {Currency} from "@app/network/currency.model";
import { Injectable } from '@angular/core';
import { ApiPromise, WsProvider } from '@polkadot/api';
import { SettingsService } from '../settings/settings.service';
import { Peer, Peers } from './peer.model';
import { StartableService } from '@app/shared/services/startable-service.class';
import { abbreviate } from '@app/shared/currencies';
import { Currency } from '@app/network/currency.model';
//import * as definitions from '@duniter/core-types/interfaces'
const WELL_KNOWN_CURRENCIES = Object.freeze({
'Ğdev': <Partial<Currency>>{
network: "gdev",
Ğdev: <Partial<Currency>>{
network: 'gdev',
displayName: 'Ğdev',
symbol: 'GD',
prefix: 42,
genesis: '0x9f956a87b5568f12c757bb3426897bba6123a1ef311fcd0945bd669fd0e612f8',
genesis:
'0x9f956a87b5568f12c757bb3426897bba6123a1ef311fcd0945bd669fd0e612f8',
fees: {
identity: 300, // = 3 Gdev
tx: 1 // = 0.01 Gdev
tx: 1, // = 0.01 Gdev
},
decimals: 2
decimals: 2,
},
'Ğ1': <Partial<Currency>>{
network: "g1",
Ğ1: <Partial<Currency>>{
network: 'g1',
displayName: 'Ğ1',
symbol: 'G1',
prefix: 4450,
genesis: '0x___TODO___',
fees: {
identity: 300, // = 3G1 - FIXME
tx: 1 // = 0.01 G1 - FIXME
tx: 1, // = 0.01 G1 - FIXME
},
decimals: 2 // FIXME remove for autodetection
}
decimals: 2, // FIXME remove for autodetection
},
});
@Injectable({providedIn: 'root'})
@Injectable({ providedIn: 'root' })
export class NetworkService extends StartableService<ApiPromise> {
currency = <Currency>{
network: null,
displayName: null,
symbol: null,
genesis: null
}
prefix: null,
genesis: null,
fees: { identity: null, tx: null },
decimals: null,
};
get api(): ApiPromise {
return this._data;
......@@ -51,11 +55,9 @@ export class NetworkService extends StartableService<ApiPromise> {
return this.currency.symbol || '';
}
constructor(
private settings: SettingsService
) {
constructor(private settings: SettingsService) {
super(settings, {
name: 'network-service'
name: 'network-service',
});
}
......@@ -64,12 +66,12 @@ export class NetworkService extends StartableService<ApiPromise> {
const peers = await this.filterAliveNodes(settings.preferredPeers);
if (!peers.length) {
throw {message: 'ERROR.CHECK_NETWORK_CONNECTION'};
throw { message: 'ERROR.CHECK_NETWORK_CONNECTION' };
}
const peer = this.selectRandomPeer(peers);
const wsUri = Peers.getWsUri(peer);
console.info(`${this._logPrefix}Connecting to peer {${wsUri}}...`)
console.info(`${this._logPrefix}Connecting to peer {${wsUri}}...`);
// Extract all types from definitions - fast and dirty approach, flatted on 'types'
// const types = Object.values(definitions).reduce((res: any, { types }): object => {
......@@ -80,7 +82,7 @@ export class NetworkService extends StartableService<ApiPromise> {
// Construct
const wsProvider = new WsProvider(wsUri);
const api = await ApiPromise.create({
provider: wsProvider
provider: wsProvider,
//,...types
});
......@@ -89,53 +91,77 @@ export class NetworkService extends StartableService<ApiPromise> {
const chain = '' + (await api.rpc.system.chain());
const genesis = api.genesisHash.toHex();
console.info(`${this._logPrefix}Connecting to chain {${chain}}: `, chainInfo.toHuman());
console.info(
`${this._logPrefix}Connecting to chain {${chain}}: `,
chainInfo.toHuman()
);
// Check is well known currency
if (WELL_KNOWN_CURRENCIES[chain]) {
const wellKnownCurrency = WELL_KNOWN_CURRENCIES[chain];
if (wellKnownCurrency.genesis && wellKnownCurrency.genesis !== genesis) {
console.warn(`${this._logPrefix}Invalid genesis for ${chain}! Expected ${wellKnownCurrency.genesis} but peer return ${genesis}`);
}
else {
console.warn(
`${this._logPrefix}Invalid genesis for ${chain}! Expected ${wellKnownCurrency.genesis} but peer return ${genesis}`
);
} else {
this.currency = WELL_KNOWN_CURRENCIES[chain];
}
}
this.currency.displayName = this.currency.displayName || chain;
this.currency.symbol = this.currency.symbol || chainInfo.tokenSymbol.value?.[0].toHuman() || abbreviate(this.currency.displayName);
this.currency.decimals = this.currency.decimals || +(chainInfo.tokenDecimals.value?.[0].toHuman()) || 0;
this.currency.symbol =
this.currency.symbol ||
chainInfo.tokenSymbol.value?.[0].toHuman() ||
abbreviate(this.currency.displayName);
this.currency.decimals =
this.currency.decimals ||
+chainInfo.tokenDecimals.value?.[0].toHuman() ||
0;
// Read the genesis block hash
console.debug(`${this._logPrefix}Blockchain symbol: ${this.currency.symbol}`);
console.debug(`${this._logPrefix}Blockchain decimals: ${this.currency.decimals}`);
console.debug(
`${this._logPrefix}Blockchain symbol: ${this.currency.symbol}`
);
console.debug(
`${this._logPrefix}Blockchain decimals: ${this.currency.decimals}`
);
console.debug(`${this._logPrefix}Blockchain genesis: ${genesis}`);
// Retrieve the latest header
const lastHeader = await api.rpc.chain.getHeader();
console.info(`${this._logPrefix}Last block: #${lastHeader.number} - hash ${lastHeader.hash}`);
console.info(
`${this._logPrefix}Last block: #${lastHeader.number} - hash ${lastHeader.hash}`
);
return api;
}
async filterAliveNodes(peers: string[], opts?: {
timeout?: number;
}): Promise<Peer[]> {
const result: Peer[] = [];
await Promise.all(peers
.map(peer => Peers.fromUri(peer))
.map(peer => this.isPeerAlive(peer)
.then(alive => {
async filterAliveNodes(
peers: string[],
opts?: {
timeout?: number;
}
): Promise<Peer[]> {
const result: Peer[] = [];
await Promise.all(
peers
.map((peer) => Peers.fromUri(peer))
.map((peer) =>
this.isPeerAlive(peer).then((alive) => {
if (!alive) return;
result.push(peer);
}))
);
return result;
})
)
);
return result;
}
async isPeerAlive(peer: Peer, opts?: {
timeout?: number;
}): Promise<boolean> {
// TODO
async isPeerAlive(
peer: Peer,
opts?: {
timeout?: number;
}
): Promise<boolean> {
// TODO
return Promise.resolve(true);
}
......
......@@ -303,16 +303,11 @@ export class AccountService extends StartableService {
return true;
}
async addV2Account(data: RegisterData): Promise<Account> {
// add the account, encrypt the stored JSON with an account-specific password
const { pair, json } = keyring.addUri(data.mnemonic, data.password, {
async addV2Account(data: {mnemonic: string; meta?: AccountMeta}): Promise<Account> {
const { pair, json } = keyring.addUri(data.mnemonic, this._password, {
name: data.meta?.name || 'default',
genesisHash: this.network.currency?.genesis
}, 'sr25519');
//this.debug('check pair', pair, json);
return this.addAccount({
address: json.address,
meta: {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment