Skip to content

Commit

Permalink
Add type information to keeping module (#251)
Browse files Browse the repository at this point in the history
* implement types for keepers and hab state

* add test coverage for external module

* add missing coverage for randy
  • Loading branch information
lenkan committed May 10, 2024
1 parent 014b2bc commit 7d6ae9f
Show file tree
Hide file tree
Showing 12 changed files with 523 additions and 190 deletions.
23 changes: 16 additions & 7 deletions examples/integration-scripts/modules/bip39_shim.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { mnemonicToSeedSync, generateMnemonic } from 'bip39';
import { Diger, Signer, MtrDex } from 'signify-ts';
import { Diger, Signer, MtrDex, Keeper, KeeperResult, Algos } from 'signify-ts';

export class BIP39Shim {
export class BIP39Shim implements Keeper {
private icount: number;
private ncount: number;
private dcode: string | undefined;
Expand All @@ -10,6 +10,8 @@ export class BIP39Shim {
private transferable: boolean;
private stem: string;
private mnemonics: string = '';
algo: Algos = Algos.extern;
signers: Signer[] = [];

constructor(pidx: number, kargs: any) {
this.icount = kargs.icount ?? 1;
Expand Down Expand Up @@ -47,7 +49,7 @@ export class BIP39Shim {
return keys;
}

incept(transferable: boolean) {
async incept(transferable: boolean): Promise<KeeperResult> {
const signers = this.keys(this.icount, this.kidx, transferable);
const verfers = signers.map((signer) => signer.verfer.qb64);

Expand All @@ -63,7 +65,12 @@ export class BIP39Shim {
return [verfers, digers];
}

rotate(ncount: number, transferable: boolean) {
async rotate(
// TODO: This signature is incompatible with Keeper
// eslint-disable-next-line @typescript-eslint/no-explicit-any
count: any, //number,
transferable: boolean
): Promise<KeeperResult> {
const signers = this.keys(
this.ncount,
this.kidx + this.icount,
Expand All @@ -73,7 +80,9 @@ export class BIP39Shim {

this.kidx = this.kidx + this.icount;
this.icount = this.ncount;
this.ncount = ncount;

// TODO: Due to incompatible signature.
this.ncount = count as number;

const nsigners = this.keys(
this.ncount,
Expand All @@ -88,7 +97,7 @@ export class BIP39Shim {
return [verfers, digers];
}

sign(
async sign(
ser: Uint8Array,
indexed = true,
indices: number[] | undefined = undefined,
Expand Down Expand Up @@ -133,7 +142,7 @@ export class BIP39Shim {
return sigers.map((siger) => siger.qb64);
} else {
const cigars = [];
for (const [_, signer] of signers.entries()) {
for (const [, signer] of signers.entries()) {
cigars.push(signer.sign(ser));
}
return cigars.map((cigar) => cigar.qb64);
Expand Down
72 changes: 33 additions & 39 deletions examples/integration-scripts/multisig-vlei-issuance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
waitForNotifications,
} from './utils/test-util';
import { getOrCreateClients, getOrCreateContact } from './utils/test-setup';
import { HabState } from '../../src/keri/core/state';

const { vleiServerUrl, witnessIds } = resolveEnvironment();

Expand Down Expand Up @@ -66,12 +67,6 @@ const ECR_RULES = Saider.saidify({
},
})[1];

interface Aid {
name: string;
prefix: string;
state: any;
}

test('multisig-vlei-issuance', async function run() {
/**
* The abbreviations used in this script follows GLEIF vLEI
Expand Down Expand Up @@ -190,7 +185,7 @@ test('multisig-vlei-issuance', async function run() {

// Create a multisig AID for the GEDA.
// Skip if a GEDA AID has already been incepted.
let aidGEDAbyGAR1, aidGEDAbyGAR2: Aid;
let aidGEDAbyGAR1, aidGEDAbyGAR2: HabState;
try {
aidGEDAbyGAR1 = await clientGAR1.identifiers().get('GEDA');
aidGEDAbyGAR2 = await clientGAR2.identifiers().get('GEDA');
Expand Down Expand Up @@ -291,7 +286,7 @@ test('multisig-vlei-issuance', async function run() {

// Create a multisig AID for the QVI.
// Skip if a QVI AID has already been incepted.
let aidQVIbyQAR1, aidQVIbyQAR2, aidQVIbyQAR3: Aid;
let aidQVIbyQAR1, aidQVIbyQAR2, aidQVIbyQAR3: HabState;
try {
aidQVIbyQAR1 = await clientQAR1.identifiers().get('QVI');
aidQVIbyQAR2 = await clientQAR2.identifiers().get('QVI');
Expand Down Expand Up @@ -655,7 +650,7 @@ test('multisig-vlei-issuance', async function run() {

// Create a multisig AID for the LE.
// Skip if a LE AID has already been incepted.
let aidLEbyLAR1, aidLEbyLAR2, aidLEbyLAR3: Aid;
let aidLEbyLAR1, aidLEbyLAR2, aidLEbyLAR3: HabState;
try {
aidLEbyLAR1 = await clientLAR1.identifiers().get('LE');
aidLEbyLAR2 = await clientLAR2.identifiers().get('LE');
Expand Down Expand Up @@ -1249,31 +1244,30 @@ async function getOrCreateAID(
client: SignifyClient,
name: string,
kargs: CreateIdentiferArgs
): Promise<Aid> {
let aid: Aid;
): Promise<HabState> {
try {
aid = await client.identifiers().get(name);
return await client.identifiers().get(name);
} catch {
const result: EventResult = await client
.identifiers()
.create(name, kargs);

await waitOperation(client, await result.op());
aid = await client.identifiers().get(name);
const aid = await client.identifiers().get(name);

const op = await client
.identifiers()
.addEndRole(name, 'agent', client!.agent!.pre);
await waitOperation(client, await op.op());
console.log(name, 'AID:', aid.prefix);
return aid;
}
return aid;
}

async function createAIDMultisig(
client: SignifyClient,
aid: Aid,
otherMembersAIDs: Aid[],
aid: HabState,
otherMembersAIDs: HabState[],
groupName: string,
kargs: CreateIdentiferArgs,
isInitiator: boolean = false
Expand Down Expand Up @@ -1311,9 +1305,9 @@ async function createAIDMultisig(

async function interactMultisig(
client: SignifyClient,
aid: Aid,
otherMembersAIDs: Aid[],
multisigAID: Aid,
aid: HabState,
otherMembersAIDs: HabState[],
multisigAID: HabState,
anchor: { i: string; s: string; d: string },
isInitiator: boolean = false
) {
Expand Down Expand Up @@ -1351,9 +1345,9 @@ async function interactMultisig(

async function addEndRoleMultisig(
client: SignifyClient,
aid: Aid,
otherMembersAIDs: Aid[],
multisigAID: Aid,
aid: HabState,
otherMembersAIDs: HabState[],
multisigAID: HabState,
timestamp: string,
isInitiator: boolean = false
) {
Expand Down Expand Up @@ -1411,9 +1405,9 @@ async function addEndRoleMultisig(

async function createRegistryMultisig(
client: SignifyClient,
aid: Aid,
otherMembersAIDs: Aid[],
multisigAID: Aid,
aid: HabState,
otherMembersAIDs: HabState[],
multisigAID: HabState,
registryName: string,
nonce: string,
isInitiator: boolean = false
Expand Down Expand Up @@ -1456,8 +1450,8 @@ async function createRegistryMultisig(

async function getIssuedCredential(
issuerClient: SignifyClient,
issuerAID: Aid,
recipientAID: Aid,
issuerAID: HabState,
recipientAID: HabState,
schemaSAID: string
) {
const credentialList = await issuerClient.credentials().list({
Expand All @@ -1473,8 +1467,8 @@ async function getIssuedCredential(

async function issueCredentialMultisig(
client: SignifyClient,
aid: Aid,
otherMembersAIDs: Aid[],
aid: HabState,
otherMembersAIDs: HabState[],
multisigAIDName: string,
kargsIss: CredentialData,
isInitiator: boolean = false
Expand Down Expand Up @@ -1516,10 +1510,10 @@ async function issueCredentialMultisig(

async function grantMultisig(
client: SignifyClient,
aid: Aid,
otherMembersAIDs: Aid[],
multisigAID: Aid,
recipientAID: Aid,
aid: HabState,
otherMembersAIDs: HabState[],
multisigAID: HabState,
recipientAID: HabState,
credential: any,
timestamp: string,
isInitiator: boolean = false
Expand Down Expand Up @@ -1568,10 +1562,10 @@ async function grantMultisig(

async function admitMultisig(
client: SignifyClient,
aid: Aid,
otherMembersAIDs: Aid[],
multisigAID: Aid,
recipientAID: Aid,
aid: HabState,
otherMembersAIDs: HabState[],
multisigAID: HabState,
recipientAID: HabState,
timestamp: string
// numGrantMsgs: number
) {
Expand Down Expand Up @@ -1617,8 +1611,8 @@ async function admitMultisig(

async function admitSinglesig(
client: SignifyClient,
aid: Aid,
recipientAid: Aid
aid: HabState,
recipientAid: HabState
) {
const grantMsgSaid = await waitAndMarkNotification(
client,
Expand Down
9 changes: 5 additions & 4 deletions src/keri/app/aiding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { MtrDex } from '../core/matter';
import { Serder } from '../core/serder';
import { parseRangeHeaders } from '../core/httping';
import { KeyManager } from '../core/keeping';
import { HabState } from '../core/state';

/** Arguments required to create an identfier */
export interface CreateIdentiferArgs {
Expand All @@ -25,9 +26,9 @@ export interface CreateIdentiferArgs {
rstates?: any[];
prxs?: any[];
nxts?: any[];
mhab?: any;
keys?: any[];
ndigs?: any[];
mhab?: HabState;
keys?: string[];
ndigs?: string[];
bran?: string;
count?: number;
ncount?: number;
Expand Down Expand Up @@ -111,7 +112,7 @@ export class Identifier {
* @param {string} name Name or alias of the identifier
* @returns {Promise<any>} A promise to the identifier information
*/
async get(name: string): Promise<any> {
async get(name: string): Promise<HabState> {
const path = `/identifiers/${encodeURIComponent(name)}`;
const data = null;
const method = 'GET';
Expand Down
8 changes: 4 additions & 4 deletions src/keri/app/credentialing.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { SignifyClient } from './clienting';
import { Salter } from '../core/salter';
import { interact, messagize } from '../core/eventing';
import { vdr } from '../core/vdring';
import {
Expand All @@ -21,6 +20,7 @@ import {
serializeIssExnAttachment,
} from '../core/utils';
import { Operation } from './coring';
import { HabState } from '../core/state';

/** Types of credentials */
export class CredentialTypes {
Expand Down Expand Up @@ -412,7 +412,7 @@ export class Credentials {

const keeper = this.client!.manager!.get(hab);

const sig = keeper.sign(b(exn.raw), true);
const sig = await keeper.sign(b(exn.raw), true);

const siger = new Siger({ qb64: sig[0] });
const seal = ['SealLast', { i: pre }];
Expand Down Expand Up @@ -638,7 +638,7 @@ export class Registries {
}

createFromEvents(
hab: Dict<any>,
hab: HabState,
name: string,
registryName: string,
vcp: Dict<any>,
Expand Down Expand Up @@ -747,7 +747,7 @@ export class Ipex {

let atc = args.ancAttachment;
if (atc === undefined) {
const keeper = this.client.manager?.get(hab);
const keeper = this.client.manager!.get(hab);
const sigs = await keeper.sign(b(args.anc.raw));
const sigers = sigs.map((sig: string) => new Siger({ qb64: sig }));
const ims = d(messagize(args.anc, sigers));
Expand Down
5 changes: 3 additions & 2 deletions src/keri/app/exchanging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { nowUTC } from '../core/utils';
import { Pather } from '../core/pather';
import { Counter, CtrDex } from '../core/counter';
import { Saider } from '../core/saider';
import { HabState } from '../core/state';

/**
* Exchanges
Expand Down Expand Up @@ -33,7 +34,7 @@ export class Exchanges {
* @param dig
*/
async createExchangeMessage(
sender: Dict<any>,
sender: HabState,
route: string,
payload: Dict<any>,
embeds: Dict<any>,
Expand Down Expand Up @@ -72,7 +73,7 @@ export class Exchanges {
async send(
name: string,
topic: string,
sender: Dict<any>,
sender: HabState,
route: string,
payload: Dict<any>,
embeds: Dict<any>,
Expand Down

0 comments on commit 7d6ae9f

Please sign in to comment.