Skip to content

Commit 4ca020b

Browse files
authored
fix: return valid schema in create schema method (openwallet-foundation#193)
Signed-off-by: Timo Glastra <[email protected]>
1 parent 1c5bfbd commit 4ca020b

File tree

15 files changed

+69
-63
lines changed

15 files changed

+69
-63
lines changed

.nvmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v12

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"@types/cors": "^2.8.7",
3939
"@types/debug": "^4.1.5",
4040
"@types/express": "^4.17.7",
41-
"@types/indy-sdk": "^1.15.0",
41+
"@types/indy-sdk": "^1.15.1",
4242
"@types/jest": "^26.0.10",
4343
"@types/node-fetch": "^2.5.7",
4444
"@types/uuid": "^8.3.0",

src/lib/__tests__/credentials.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,12 @@ describe('credentials', () => {
9090
schema: ledgerSchema,
9191
tag: 'TAG',
9292
signatureType: 'CL',
93-
config: { support_revocation: false },
93+
config: { supportRevocation: false },
9494
};
9595
const [ledgerCredDefId] = await registerDefinition(faberAgent, definitionTemplate);
9696
credDefId = ledgerCredDefId;
9797

98-
const publicDid = faberAgent.getPublicDid()?.did;
98+
const publicDid = faberAgent.publicDid?.did;
9999
await ensurePublicDidIsOnLedger(faberAgent, publicDid!);
100100
const { agentAConnection, agentBConnection } = await makeConnection(faberAgent, aliceAgent);
101101
faberConnection = agentAConnection;

src/lib/__tests__/helpers.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ export async function makeConnection(agentA: Agent, agentB: Agent) {
154154
}
155155

156156
export async function registerSchema(agent: Agent, schemaTemplate: SchemaTemplate): Promise<[SchemaId, Schema]> {
157-
const [schemaId] = await agent.ledger.registerCredentialSchema(schemaTemplate);
157+
const [schemaId] = await agent.ledger.registerSchema(schemaTemplate);
158158
const ledgerSchema = await agent.ledger.getSchema(schemaId);
159159
logger.logJson(`created schema with id ${schemaId}`, ledgerSchema);
160160
return [schemaId, ledgerSchema];

src/lib/__tests__/ledger.test.ts

+17-13
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('ledger', () => {
3535
});
3636

3737
test(`initialization of agent's public DID`, async () => {
38-
faberAgentPublicDid = faberAgent.getPublicDid();
38+
faberAgentPublicDid = faberAgent.publicDid;
3939
logger.logJson('faberAgentPublicDid', faberAgentPublicDid!);
4040

4141
expect(faberAgentPublicDid).toEqual(
@@ -48,7 +48,7 @@ describe('ledger', () => {
4848

4949
test('get public DID from ledger', async () => {
5050
if (!faberAgentPublicDid) {
51-
throw new Error('Agent does not have publid did.');
51+
throw new Error('Agent does not have public did.');
5252
}
5353

5454
const result = await faberAgent.ledger.getPublicDid(faberAgentPublicDid.did);
@@ -71,7 +71,7 @@ describe('ledger', () => {
7171

7272
test('register schema on ledger', async () => {
7373
if (!faberAgentPublicDid) {
74-
throw new Error('Agent does not have publid did.');
74+
throw new Error('Agent does not have public did.');
7575
}
7676

7777
const schemaName = `test-schema-${Date.now()}`;
@@ -81,32 +81,36 @@ describe('ledger', () => {
8181
version: '1.0',
8282
};
8383

84-
[schemaId] = await faberAgent.ledger.registerCredentialSchema(schemaTemplate);
84+
const schemaResponse = await faberAgent.ledger.registerSchema(schemaTemplate);
85+
schemaId = schemaResponse[0];
86+
const schema = schemaResponse[1];
87+
8588
const ledgerSchema = await faberAgent.ledger.getSchema(schemaId);
8689

8790
expect(schemaId).toBe(`${faberAgentPublicDid.did}:2:${schemaName}:1.0`);
91+
8892
expect(ledgerSchema).toEqual(
8993
expect.objectContaining({
90-
attrNames: expect.arrayContaining(['name', 'age']),
94+
attrNames: expect.arrayContaining(schemaTemplate.attributes),
9195
id: `${faberAgentPublicDid.did}:2:${schemaName}:1.0`,
9296
name: schemaName,
93-
seqNo: expect.any(Number),
94-
ver: '1.0',
95-
version: '1.0',
97+
seqNo: schema.seqNo,
98+
ver: schemaTemplate.version,
99+
version: schemaTemplate.version,
96100
})
97101
);
98102
});
99103

100104
test('register definition on ledger', async () => {
101105
if (!faberAgentPublicDid) {
102-
throw new Error('Agent does not have publid did.');
106+
throw new Error('Agent does not have public did.');
103107
}
104108
const schema = await faberAgent.ledger.getSchema(schemaId);
105109
const credentialDefinitionTemplate = {
106110
schema: schema,
107111
tag: 'TAG',
108112
signatureType: 'CL',
109-
config: { support_revocation: true },
113+
config: { supportRevocation: true },
110114
};
111115

112116
const [credDefId] = await faberAgent.ledger.registerCredentialDefinition(credentialDefinitionTemplate);
@@ -117,9 +121,9 @@ describe('ledger', () => {
117121
expect(ledgerCredDef).toEqual(
118122
expect.objectContaining({
119123
id: expect.stringMatching(credDefIdRegExp),
120-
schemaId: expect.any(String),
121-
type: 'CL',
122-
tag: 'TAG',
124+
schemaId: String(schema.seqNo),
125+
type: credentialDefinitionTemplate.signatureType,
126+
tag: credentialDefinitionTemplate.tag,
123127
ver: '1.0',
124128
value: expect.objectContaining({
125129
primary: expect.anything(),

src/lib/__tests__/proofs.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,12 @@ describe('Present Proof', () => {
9696
schema: ledgerSchema,
9797
tag: 'TAG',
9898
signatureType: 'CL',
99-
config: { support_revocation: false },
99+
config: { supportRevocation: false },
100100
};
101101
const [ledgerCredDefId] = await registerDefinition(faberAgent, definitionTemplate);
102102
credDefId = ledgerCredDefId;
103103

104-
const publicDid = faberAgent.getPublicDid()?.did;
104+
const publicDid = faberAgent.publicDid?.did;
105105
await ensurePublicDidIsOnLedger(faberAgent, publicDid!);
106106
const { agentAConnection, agentBConnection } = await makeConnection(faberAgent, aliceAgent);
107107

src/lib/agent/Agent.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ export class Agent {
156156
return this.inboundTransporter.start(this);
157157
}
158158

159-
public getPublicDid() {
160-
return this.wallet.getPublicDid();
159+
public get publicDid() {
160+
return this.wallet.publicDid;
161161
}
162162

163163
public getMediatorUrl() {

src/lib/modules/credentials/__tests__/StubWallet.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@ import type {
2525
WalletQuery,
2626
LedgerRequest,
2727
} from 'indy-sdk';
28-
import { Wallet, DidInfo } from '../../../wallet/Wallet';
28+
import { Wallet } from '../../../wallet/Wallet';
2929
import { UnpackedMessageContext } from '../../../types';
3030

3131
export class StubWallet implements Wallet {
3232
public get walletHandle() {
3333
return 0;
3434
}
35+
public get publicDid() {
36+
return undefined;
37+
}
3538
public init(): Promise<void> {
3639
return Promise.resolve();
3740
}
@@ -59,9 +62,6 @@ export class StubWallet implements Wallet {
5962
public initPublicDid(didConfig: DidConfig): Promise<void> {
6063
throw new Error('Method not implemented.');
6164
}
62-
public getPublicDid(): DidInfo | undefined {
63-
throw new Error('Method not implemented.');
64-
}
6565
public createDid(didConfig?: DidConfig | undefined): Promise<[string, string]> {
6666
throw new Error('Method not implemented.');
6767
}

src/lib/modules/ledger/LedgerModule.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,20 @@ export class LedgerModule {
1616
}
1717

1818
public async registerPublicDid() {
19-
// TODO: handle ping response message
19+
throw new Error('registerPublicDid not implemented.');
2020
}
2121

2222
public async getPublicDid(did: Did) {
2323
return this.ledgerService.getPublicDid(did);
2424
}
2525

26-
public async registerCredentialSchema(schema: SchemaTemplate) {
27-
const did = this.wallet.getPublicDid()?.did;
26+
public async registerSchema(schema: SchemaTemplate) {
27+
const did = this.wallet.publicDid?.did;
28+
2829
if (!did) {
2930
throw new Error('Agent has no public DID.');
3031
}
32+
3133
return this.ledgerService.registerSchema(did, schema);
3234
}
3335

@@ -36,10 +38,12 @@ export class LedgerModule {
3638
}
3739

3840
public async registerCredentialDefinition(credentialDefinitionTemplate: CredDefTemplate) {
39-
const did = this.wallet.getPublicDid()?.did;
41+
const did = this.wallet.publicDid?.did;
42+
4043
if (!did) {
4144
throw new Error('Agent has no public DID.');
4245
}
46+
4347
return this.ledgerService.registerCredentialDefinition(did, credentialDefinitionTemplate);
4448
}
4549

src/lib/modules/ledger/LedgerService.ts

+19-23
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,22 @@ import { Wallet } from '../../wallet/Wallet';
77
export class LedgerService {
88
private wallet: Wallet;
99
private indy: typeof Indy;
10-
private poolHandle?: PoolHandle;
10+
private _poolHandle?: PoolHandle;
1111
private authorAgreement?: AuthorAgreement | null;
1212

1313
public constructor(wallet: Wallet, indy: typeof Indy) {
1414
this.wallet = wallet;
1515
this.indy = indy;
1616
}
1717

18+
private get poolHandle() {
19+
if (!this._poolHandle) {
20+
throw new Error('Pool has not been initialized yet.');
21+
}
22+
23+
return this._poolHandle;
24+
}
25+
1826
public async connect(poolName: string, poolConfig: PoolConfig) {
1927
try {
2028
logger.log(`Creating pool config with name "${poolName}".`);
@@ -31,13 +39,10 @@ export class LedgerService {
3139
await this.indy.setProtocolVersion(2);
3240

3341
logger.log('Opening pool');
34-
this.poolHandle = await this.indy.openPoolLedger(poolName);
42+
this._poolHandle = await this.indy.openPoolLedger(poolName);
3543
}
3644

3745
public async getPublicDid(did: Did) {
38-
if (!this.poolHandle) {
39-
throw new Error('Pool has not been initialized.');
40-
}
4146
const request = await this.indy.buildGetNymRequest(null, did);
4247
logger.log('request', request);
4348

@@ -51,9 +56,6 @@ export class LedgerService {
5156
}
5257

5358
public async registerSchema(did: Did, schemaTemplate: SchemaTemplate): Promise<[SchemaId, Schema]> {
54-
if (!this.poolHandle) {
55-
throw new Error('Pool has not been initialized.');
56-
}
5759
const { name, attributes, version } = schemaTemplate;
5860
const [schemaId, schema] = await this.indy.issuerCreateSchema(did, name, version, attributes);
5961
logger.log(`Register schema with ID = ${schemaId}:`, schema);
@@ -67,13 +69,15 @@ export class LedgerService {
6769
const response = await this.indy.submitRequest(this.poolHandle, signedRequest);
6870
logger.log('Register schema response', response);
6971

72+
const seqNo = response.result.txnMetadata?.seqNo;
73+
74+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
75+
schema.seqNo = seqNo!;
76+
7077
return [schemaId, schema];
7178
}
7279

7380
public async getCredentialSchema(schemaId: SchemaId) {
74-
if (!this.poolHandle) {
75-
throw new Error('Pool has not been initialized.');
76-
}
7781
const request = await this.indy.buildGetSchemaRequest(null, schemaId);
7882
logger.log('Get schema request', request);
7983

@@ -90,12 +94,11 @@ export class LedgerService {
9094
did: Did,
9195
credentialDefinitionTemplate: CredDefTemplate
9296
): Promise<[CredDefId, CredDef]> {
93-
if (!this.poolHandle) {
94-
throw new Error('Pool has not been initialized.');
95-
}
9697
const { schema, tag, signatureType, config } = credentialDefinitionTemplate;
9798

98-
const [credDefId, credDef] = await this.wallet.createCredentialDefinition(did, schema, tag, signatureType, config);
99+
const [credDefId, credDef] = await this.wallet.createCredentialDefinition(did, schema, tag, signatureType, {
100+
support_revocation: config.supportRevocation,
101+
});
99102
logger.log(`Register credential definition with ID = ${credDefId}:`, credDef);
100103

101104
const request = await this.indy.buildCredDefRequest(did, credDef);
@@ -111,9 +114,6 @@ export class LedgerService {
111114
}
112115

113116
public async getCredentialDefinition(credDefId: CredDefId) {
114-
if (!this.poolHandle) {
115-
throw new Error('Pool has not been initialized.');
116-
}
117117
const request = await this.indy.buildGetCredDefRequest(null, credDefId);
118118
logger.log('Get credential definition request:', request);
119119

@@ -154,10 +154,6 @@ export class LedgerService {
154154
return this.authorAgreement;
155155
}
156156

157-
if (!this.poolHandle) {
158-
throw new Error('Pool has not been initialized.');
159-
}
160-
161157
const taaRequest = await this.indy.buildGetTxnAuthorAgreementRequest(null);
162158
const taaResponse = await this.indy.submitRequest(this.poolHandle, taaRequest);
163159
const acceptanceMechanismRequest = await this.indy.buildGetAcceptanceMechanismsRequest(null);
@@ -195,7 +191,7 @@ export interface CredDefTemplate {
195191
schema: Schema;
196192
tag: string;
197193
signatureType: string;
198-
config: { support_revocation: boolean };
194+
config: { supportRevocation: boolean };
199195
}
200196

201197
interface AuthorAgreement {

src/lib/wallet/IndyWallet.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ export class IndyWallet implements Wallet {
5050
this.indy = indy;
5151
}
5252

53+
public get publicDid() {
54+
return this.publicDidInfo;
55+
}
56+
5357
private get walletHandle() {
5458
if (!this._walletHandle) {
5559
throw new Error('Wallet has not been initialized yet');
@@ -107,10 +111,6 @@ export class IndyWallet implements Wallet {
107111
};
108112
}
109113

110-
public getPublicDid() {
111-
return this.publicDidInfo;
112-
}
113-
114114
public async createDid(didConfig?: DidConfig): Promise<[Did, Verkey]> {
115115
return this.indy.createAndStoreMyDid(this.walletHandle, didConfig || {});
116116
}

src/lib/wallet/Wallet.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('Wallet', () => {
99

1010
await wallet.initPublicDid({ seed: '00000000000000000000000Forward01' });
1111

12-
expect(wallet.getPublicDid()).toEqual({
12+
expect(wallet.publicDid).toEqual({
1313
did: 'DtWRdd6C5dN5vpcN6XRAvu',
1414
verkey: '82RBSn3heLgXzZd74UsMC8Q8YRfEEhQoAM7LUqE6bevJ',
1515
});

src/lib/wallet/Wallet.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ import type {
3030
import { UnpackedMessageContext } from '../types';
3131

3232
export interface Wallet {
33+
publicDid: DidInfo | undefined;
34+
3335
init(): Promise<void>;
3436
close(): Promise<void>;
3537
delete(): Promise<void>;
3638
initPublicDid(didConfig: DidConfig): Promise<void>;
37-
getPublicDid(): DidInfo | undefined;
3839
createDid(didConfig?: DidConfig): Promise<[Did, Verkey]>;
3940
createCredentialDefinition(
4041
issuerDid: Did,

src/samples/mediator.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ const messageReceiver = new HttpInboundTransporter(app);
7171
const agent = new Agent(config, messageReceiver, messageSender, indy, messageRepository);
7272

7373
app.get('/', async (req, res) => {
74-
const agentDid = agent.getPublicDid();
74+
const agentDid = agent.publicDid;
7575
res.send(agentDid);
7676
});
7777

yarn.lock

+4-4
Original file line numberDiff line numberDiff line change
@@ -601,10 +601,10 @@
601601
dependencies:
602602
"@types/node" "*"
603603

604-
"@types/indy-sdk@^1.15.0":
605-
version "1.15.0"
606-
resolved "https://registry.yarnpkg.com/@types/indy-sdk/-/indy-sdk-1.15.0.tgz#44b49978fc8c4892f2c90ec580d3c957b9a88805"
607-
integrity sha512-xkxmHpxiCnw28URfP6xWtB93KAarfKPRDA3U1dv9ejkw4rLAXPeWkmnDmvmtoY2/QLCyq20hqP/ZEAV188okiw==
604+
"@types/indy-sdk@^1.15.1":
605+
version "1.15.1"
606+
resolved "https://registry.yarnpkg.com/@types/indy-sdk/-/indy-sdk-1.15.1.tgz#7baf9eab749ddac643126d8553ce5295ba256952"
607+
integrity sha512-H8ZvxwpR0L+I9Was0Mcx4POmC5DCVaMMAVM7feEX1u7DFdMNoK7jWruP0sT8IT/UaeQ+ie3dArYis72i4wuVTg==
608608
dependencies:
609609
"@types/node" "*"
610610

0 commit comments

Comments
 (0)