Newer
Older
import {
Agent,
AutoAcceptCredential,
AutoAcceptProof,
ConnectionsModule,
CredentialsModule,
DidsModule,
Key,
KeyType,
ProofsModule,
TypedArrayEncoder,
V2CredentialProtocol,
V2ProofProtocol,
} from "@aries-framework/core";
import {
AnonCredsCredentialFormatService,
AnonCredsModule,
AnonCredsProofFormatService,
} from "@aries-framework/anoncreds";
import {
IndyVdrAnonCredsRegistry,
IndyVdrIndyDidRegistrar,
IndyVdrIndyDidResolver,
IndyVdrModule,
} from "@aries-framework/indy-vdr";
import { AnonCredsRsModule } from "@aries-framework/anoncreds-rs";
import { anoncreds } from "@hyperledger/anoncreds-nodejs";
import { indyVdr } from "@hyperledger/indy-vdr-nodejs";
import { AskarModule } from "@aries-framework/askar";
import { ariesAskar } from "@hyperledger/aries-askar-nodejs";
import { Key as C, KeyAlgs } from "@hyperledger/aries-askar-shared";
export const importDidsToWallet = async (
agent: Agent,
dids: Array<string>,
): Promise<void> => {
for (const did in dids) {
try {
await agent.dids.import({
did: dids[did],
overwrite: false,
});
} catch (e) {
console.log((e as Error).message);
}
}
};
export const generateKey = async ({
seed,
agent,
}: {
seed: string;
agent: Agent;
}): Promise<Key> => {
if (!seed) {
throw Error("No seed provided");
}
const privateKey = TypedArrayEncoder.fromString(seed);
try {
return await agent.wallet.createKey({
privateKey,
keyType: KeyType.Ed25519,
});
} catch (e) {
if (e instanceof WalletKeyExistsError) {
const c = C.fromSecretBytes({
algorithm: KeyAlgs.Ed25519,
secretKey: TypedArrayEncoder.fromString(seed),
});
return Key.fromPublicKey(c.publicBytes, KeyType.Ed25519);
}
console.log(JSON.stringify(e, null, 2));
throw new Error("Failed to create key");
}
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
};
export const generateDidFromKey = (key: Key): string => {
if (!key) {
throw new Error("Key not provided");
}
return TypedArrayEncoder.toBase58(key.publicKey.slice(0, 16));
};
//eslint-disable-next-line
export const getAskarAnonCredsIndyModules = (networks: any) => {
return {
connections: new ConnectionsModule({
autoAcceptConnections: true,
}),
credentials: new CredentialsModule({
autoAcceptCredentials: AutoAcceptCredential.ContentApproved,
credentialProtocols: [
new V2CredentialProtocol({
credentialFormats: [new AnonCredsCredentialFormatService()],
}),
],
}),
proofs: new ProofsModule({
autoAcceptProofs: AutoAcceptProof.ContentApproved,
proofProtocols: [
new V2ProofProtocol({
proofFormats: [new AnonCredsProofFormatService()],
}),
],
}),
anoncreds: new AnonCredsModule({
registries: [new IndyVdrAnonCredsRegistry()],
}),
anoncredsRs: new AnonCredsRsModule({
anoncreds,
}),
indyVdr: new IndyVdrModule({
indyVdr,
networks,
}),
dids: new DidsModule({
registrars: [new IndyVdrIndyDidRegistrar()],
resolvers: [new IndyVdrIndyDidResolver()],
}),
askar: new AskarModule({
ariesAskar,
}),
} as const;
};