Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Copyright 2017-2019 @polkadot/client-cli authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { Config, ConfigKeys } from '@polkadot/client/types';
import wrtc from 'wrtc';
import Client from '@polkadot/client';
import { logger } from '@polkadot/util';
import getArgv from './argv';
import getExternalIp from './getExternalIp';
import keyToCamel from './keyToCamel';
const l = logger('client/cli');
function cli (params?: string): Config {
const argv = getArgv(params);
return Object
.keys(argv)
.reduce((config: any, key): Config => {
if (/^(db|dev|p2p|rpc|signal|telemetry|wasm)-/.test(key)) {
const section = key.substr(0, key.indexOf('-')) as ConfigKeys;
const name = keyToCamel(key, 1);
config[section] = config[section] || {};
// ummm... no index, not Map-ing this one
config[section][name] = (argv as any)[key];
} else if (!/^(db|dev|p2p|rpc|signal|telemetry|wasm)[A-Z]/.test(key)) {
const name = keyToCamel(key) as ConfigKeys;
// Copyright 2017-2019 @polkadot/db authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { BaseDb, TxDb, ProgressCb } from '../types';
import { assert, isNull, logger } from '@polkadot/util';
interface OverlayItem {
key: Uint8Array;
value: Uint8Array | null;
}
const l = logger('db/transact');
export default class TransactionDb implements TxDb {
private backing: BaseDb;
private txOverlay: Map = new Map();
private txStarted: boolean;
constructor (backing: BaseDb) {
this.backing = backing;
this.txStarted = false;
}
public transaction (fn: () => T): T {
l.debug((): string[] => ['transaction']);
// Copyright 2017-2018 @polkadot/rpc-provider authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import { MockState, MockState$Db, MockState$Subscriptions } from './types';
import E3 from 'eventemitter3';
import interfaces from '@polkadot/jsonrpc/index';
import { logger, u8aToHex } from '@polkadot/util';
const l = logger('api-mock');
const SUBSCRIPTIONS: string[] = Array.prototype.concat.apply(
[], Object.values(interfaces).map((area) =>
Object
.values(area.methods)
.filter((method) =>
method.isSubscription
)
.map(({ method, section }) =>
`${section}_${method}`
)
)
);
const REQUESTS = {
'state_getStorage': (storage: MockState$Db, params: Array): string => {
// Copyright 2017-2019 @polkadot/db authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { Key, NibbleBuffer, Slot, Value } from './types';
import fs from 'fs';
import { logger } from '@polkadot/util';
import Cache from './Cache';
import defaults from './defaults';
const l = logger('db/flat');
export default class Impl extends Cache {
protected _getKeyValue (keyAt: number): Buffer {
return this._getCachedData(keyAt, defaults.KEY_TOTAL_SIZE);
}
protected _retrieveBranch (doCreate: boolean, branch: Buffer, entryIndex: number, keyIndex: number, key: NibbleBuffer): Key | null {
const nextBranchAt = branch.readUIntBE(entryIndex + 1, defaults.UINT_SIZE);
l.debug(() => ['findKey/isBranch', { nextBranchAt }]);
return this._findKey(key, doCreate, keyIndex + 1, nextBranchAt);
}
protected _retrieveEmpty (doCreate: boolean, branch: Buffer, branchAt: number, entryIndex: number, key: NibbleBuffer): Key | null {
l.debug(() => ['findKey/isEmpty']);
// of the Apache-2.0 license. See the LICENSE file for details.
import { Observable } from 'rxjs';
import { catchError, distinctUntilChanged, publishReplay, tap, refCount } from 'rxjs/operators';
import { logger } from '@polkadot/util';
import { refCountDelay } from './refCountDelay';
export type DrrResult = (source$: Observable) => Observable;
interface Options {
skipChange?: boolean;
skipTimeout?: boolean;
}
const l = logger('drr');
const CMP = (a: any, b: any): boolean =>
JSON.stringify({ t: a }) === JSON.stringify({ t: b });
const ERR = (error: Error): Observable => {
l.error(error);
throw error;
};
const NOOP = (): void => undefined;
/**
* Shorthand for distinctUntilChanged(), publishReplay(1) and refCount().
*
* @ignore
// of the Apache-2.0 license. See the LICENSE file for details.
import { BaseDb, ProgressCb } from '../types';
import fs from 'fs';
import mkdirp from 'mkdirp';
import { bufferToU8a, logger, u8aToBuffer, u8aToHex } from '@polkadot/util';
type FilePath = {
directory: string,
file: string
};
const DIR_DEPTH = 1;
const l = logger('db/tree');
export default class FileTreeDb implements BaseDb {
_location: string;
constructor (location: string) {
this._location = location;
}
private _getFilePath (key: Uint8Array): FilePath {
// NOTE We want to limit the number of entries in any specific directory. Split the
// key into parts and use this to construct the path and the actual filename. We want
// to limit the entries per directory, but at the same time minimize the number of
// directories we need to create (when non-existent as well as the size overhead)
const parts = u8aToHex(key).match(/.{1,6}/g) || [];
const directory = `${this._location}/${parts.slice(0, DIR_DEPTH).join('/')}`;
const file = `${directory}/${parts.slice(DIR_DEPTH).join('')}`;
// Copyright 2017-2019 @polkadot/trie-db authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { TxDb, ProgressCb } from '@polkadot/db/types';
import { Codec } from '@polkadot/trie-codec/types';
import { TrieDb, Node, TrieEntry } from './types';
import MemoryDb from '@polkadot/db/Memory';
import { toNibbles } from '@polkadot/trie-codec/util';
import { isNull, logger, u8aToHex } from '@polkadot/util';
import Impl from './Impl';
import constants from './constants';
const l = logger('trie/db');
/**
* # @polkadot/trie-db
*
* ## Overview
*
* @name Trie
* @summary Re-implementation of a Patricia Trie
* @example See [Polkadot-JS Common Trie-DB Examples](https://polkadot.js.org/api/common/examples/trie-db/)
*/
export default class Trie extends Impl implements TrieDb {
constructor (db: TxDb = new MemoryDb(), rootHash?: Uint8Array, codec?: Codec) {
super(db, rootHash, codec);
l.debug((): string => `Created with ${this.codec.type} codec, root ${u8aToHex(this.rootHash, 64)}`);
}
const SUBSCRIPTIONS: string[] = Array.prototype.concat.apply(
[], Object.values(interfaces).map((area): string[] =>
Object
.values(area.methods)
.filter((method): boolean =>
method.isSubscription
)
.map(({ method, section }): string =>
`${section}_${method}`
)
.concat('chain_subscribeNewHead')
)
);
const keyring = testKeyring({ type: 'ed25519' });
const l = logger('api-mock');
/**
* A mock provider mainly used for testing.
* @return {ProviderInterface} The mock provider
*/
export default class Mock implements ProviderInterface {
private db: MockStateDb = {};
private emitter = new EventEmitter();
public isUpdating = true;
private registry: Registry;
private requests: Record any> = {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
/* eslint-disable @typescript-eslint/camelcase */
// Copyright 2017-2019 @polkadot/client-runtime authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
/* eslint camelcase: 0 */
import { logger } from '@polkadot/util';
import index from '.';
const l = logger('test');
describe('ed25519Verify', () => {
let heap;
let ed25519_verify;
beforeEach(() => {
const uint8 = new Uint8Array([
0x61, 0x62, 0x63, 0x64,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// publicKey, offset 4 + 6 = 10
180, 114, 93, 155, 165, 255, 217, 82, 16, 250, 209, 11, 193, 10, 88, 218, 190, 190, 41, 193, 236, 252, 1, 152, 216, 214, 0, 41, 45, 138, 13, 53,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// signature, offset 32 + 8 = 50
209, 234, 164, 44, 182, 218, 103, 16, 205, 238, 97, 222, 123, 112, 2, 240, 24, 192, 26, 134, 11, 170, 167, 153, 141, 108, 187, 171, 241, 125, 226, 179, 244, 232, 131, 61, 44, 68, 87, 41, 141, 131, 88, 36, 175, 173, 57, 29, 12, 112, 26, 200, 247, 89, 14, 64, 224, 188, 211, 198, 233, 119, 158, 6
]);
import { ChainInterface } from '@polkadot/client-chains/types';
import { BlockDb } from '@polkadot/client-db/types';
import { SyncStatus } from '@polkadot/client-sync/types';
import { TelemetryInterface } from './types';
import './polyfill';
import { logger } from '@polkadot/util';
import Base from './messages/Base';
import BlockImport from './messages/BlockImport';
import Connected from './messages/Connected';
import Interval from './messages/Interval';
import Started from './messages/Started';
const l = logger('telemetry');
export default class Telemetry implements TelemetryInterface {
private blocks: BlockDb;
private isActive = false;
private chain: string;
private name: string;
private url: string;
private websocket: WebSocket | null = null;
public constructor ({ telemetry }: Config, { blocks, chain }: ChainInterface) {
const name = telemetry.name ? telemetry.name.trim() : '';