Skip to content

Commit

Permalink
feat: context class
Browse files Browse the repository at this point in the history
  • Loading branch information
shuffledex committed Feb 10, 2020
1 parent 2e573d2 commit 038873f
Show file tree
Hide file tree
Showing 4 changed files with 590 additions and 25 deletions.
6 changes: 6 additions & 0 deletions package.json
Expand Up @@ -19,6 +19,7 @@
"@commitlint/config-conventional": "^7.6.0",
"@semantic-release/git": "^8.0.0",
"@types/jest": "^23.3.10",
"@types/json-stable-stringify": "^1.0.32",
"@types/node": "^13.1.8",
"@typescript-eslint/eslint-plugin": "^2.17.0",
"@typescript-eslint/parser": "^2.17.0",
Expand Down Expand Up @@ -52,5 +53,10 @@
},
"publishConfig": {
"access": "public"
},
"dependencies": {
"@polymathnetwork/polkadot": "^0.101.0-beta.8",
"bn.js": "^5.1.1",
"json-stable-stringify": "^1.0.1"
}
}
81 changes: 81 additions & 0 deletions src/Context.ts
@@ -0,0 +1,81 @@
import { Identity } from './api/entities/Identity';
import { ApiPromise, Keyring } from '@polymathnetwork/polkadot/api';
import { KeyringPair, KeyringPair$Meta } from '@polkadot/keyring/types';
import stringToU8a from '@polkadot/util/string/toU8a';
import { LinkedKeyInfo } from '@polymathnetwork/polkadot/types/interfaces';
import { Option } from '@polymathnetwork/polkadot/types/codec';

interface BuildParams {
polymeshApi: ApiPromise;
seed: string | undefined;
}

interface ConstructorParams {
polymeshApi: ApiPromise;
currentPair?: KeyringPair;
did?: Option<LinkedKeyInfo>;
}

interface AddressPair {
address: string;
meta: KeyringPair$Meta;
}

/**
* Context in which the SDK is being used
*
* - Holds the current low level API
* - Holds the current keyring pair
* - Holds the current Identity
*/
export class Context {
private static keyring: Keyring = new Keyring({ type: 'sr25519' });

public polymeshApi: ApiPromise;

public currentPair: KeyringPair | undefined;

public currentIdentity: Identity | undefined;

private constructor(params: ConstructorParams) {
const { polymeshApi, currentPair, did } = params;

this.polymeshApi = polymeshApi;

if (currentPair && did) {
this.currentPair = currentPair;
this.currentIdentity = new Identity({ did: did.toString() }, this);
}
}

static async create(params: BuildParams) {
const { polymeshApi, seed } = params;

if (seed) {
if (seed.length != 32) {
throw new Error('Seed must be 32 length size');
}

const currentPair = Context.keyring.addFromSeed(stringToU8a(seed));
const did = await polymeshApi.query.identity.keyToIdentityIds(currentPair.publicKey);
return new Context({ polymeshApi, currentPair, did });
}
return new Context({ polymeshApi });
}

public getAddresses = (): Array<AddressPair> => {
const { keyring } = Context;
return keyring.getPairs().map(({ address, meta }) => {
return { address, meta };
});
};

public setPair = (address: string) => {
const { keyring } = Context;
try {
this.currentPair = keyring.getPair(address);
} catch ({}) {
throw new Error('The address is not present in the keyring set');
}
};
}
4 changes: 2 additions & 2 deletions src/api/entities/Identity.ts
Expand Up @@ -13,7 +13,7 @@ export interface UniqueIdentifiers {
/**
* Constructor parameters
*/
export type Params = UniqueIdentifiers
export type Params = UniqueIdentifiers;

function isUniqueIdentifiers(identifier: any): identifier is UniqueIdentifiers {
const { did } = identifier;
Expand Down Expand Up @@ -80,6 +80,6 @@ export class Identity extends Entity {
*/
public getPolyBalance = async (): Promise<Balance> => {
const { context, did } = this;
return context.api.query.balances.identityBalance(did);
return context.polymeshApi.query.balances.identityBalance(did);
};
}

0 comments on commit 038873f

Please sign in to comment.