Skip to content

Commit

Permalink
feat(core): ledger module registerPublicDid implementation (#398)
Browse files Browse the repository at this point in the history
Co-authored-by: Timo Glastra <timo@animo.id>
  • Loading branch information
genaris and TimoGlastra authored Jul 27, 2021
1 parent 600eccf commit 5f2d512
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/actions/setup-indy-pool/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ runs:
shell: bash

- name: Register DID on ledger
run: docker exec indy-pool add-did-from-seed ${{ inputs.seed }}
run: docker exec indy-pool add-did-from-seed ${{ inputs.seed }} TRUSTEE
shell: bash

branding:
Expand Down
4 changes: 2 additions & 2 deletions DEVREADME.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ docker run -d --rm --name indy-pool -p 9701-9708:9701-9708 indy-pool
# Setup CLI. This creates a wallet, connects to the ledger and sets the Transaction Author Agreement
docker exec indy-pool indy-cli-setup

# DID and Verkey from seed
docker exec indy-pool add-did-from-seed 000000000000000000000000Trustee9
# DID and Verkey from seed. Set 'Trustee' role in order to be able to register public DIDs
docker exec indy-pool add-did-from-seed 000000000000000000000000Trustee9 TRUSTEE

# If you want to register using the DID/Verkey you can use
# docker exec indy-pool add-did "NkGXDEPgpFGjQKMYmz6SyF" "CrSA1WbYYWLJoHm16Xw1VEeWxFvXtWjtsfEzMsjB5vDT"
Expand Down
5 changes: 3 additions & 2 deletions network/add-did-from-seed.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/bash

export SEED=${1?"Seed missing\nUsage: $0 SEED"}
export SEED=${1?"Seed missing\nUsage: $0 SEED ROLE"}
export ROLE=$2

echo "
wallet open afj-wallet key=password
Expand All @@ -15,4 +16,4 @@ IFS='"' read -r -a DID_PARTS <<<"$DID_STRING"
export DID=${DID_PARTS[1]}
export VERKEY=${DID_PARTS[3]}

add-did "$DID" "$VERKEY"
add-did "$DID" "$VERKEY" "$ROLE"
11 changes: 8 additions & 3 deletions network/add-did.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
#!/bin/bash

export DID=${1?"Did missing\nUsage: $0 DID VERKEY"}
export VERKEY=${2?"Verkey missing\nUsage: $0 DID VERKEY"}
export DID=${1?"Did missing\nUsage: $0 DID VERKEY [ROLE]"}
export VERKEY=${2?"Verkey missing\nUsage: $0 DID VERKEY [ROLE]"}
export ROLE=$3

if [ -z "$ROLE" ]; then
ROLE=TRUST_ANCHOR
fi

echo "
wallet open afj-wallet key=password
pool connect afj-pool
did use V4SGRU86Z58d6TV7PBUe6f
ledger nym did=${DID} verkey=${VERKEY} role=TRUST_ANCHOR" >/etc/indy/command.txt
ledger nym did=${DID} verkey=${VERKEY} role=${ROLE}" >/etc/indy/command.txt

indy-cli --config /etc/indy/indy-cli-config.json /etc/indy/command.txt
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"prepublishOnly": "yarn run build"
},
"dependencies": {
"@types/indy-sdk": "^1.16.5",
"@types/indy-sdk": "^1.16.6",
"abort-controller": "^3.0.0",
"bn.js": "^5.2.0",
"borc": "^3.0.0",
Expand Down
12 changes: 9 additions & 3 deletions packages/core/src/modules/ledger/LedgerModule.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { SchemaTemplate, CredentialDefinitionTemplate } from './services'
import type { CredDefId, Did, SchemaId } from 'indy-sdk'
import type { CredDefId, Did, NymRole, SchemaId } from 'indy-sdk'

import { inject, scoped, Lifecycle } from 'tsyringe'

Expand All @@ -19,8 +19,14 @@ export class LedgerModule {
this.wallet = wallet
}

public async registerPublicDid() {
throw new AriesFrameworkError('registerPublicDid not implemented.')
public async registerPublicDid(did: Did, verkey: string, alias: string, role?: NymRole) {
const myPublicDid = this.wallet.publicDid?.did

if (!myPublicDid) {
throw new AriesFrameworkError('Agent has no public DID.')
}

return this.ledgerService.registerPublicDid(myPublicDid, did, verkey, alias, role)
}

public async getPublicDid(did: Did) {
Expand Down
29 changes: 29 additions & 0 deletions packages/core/src/modules/ledger/services/LedgerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
SchemaId,
LedgerReadReplyResponse,
LedgerWriteReplyResponse,
NymRole,
} from 'indy-sdk'

import { inject, scoped, Lifecycle } from 'tsyringe'
Expand Down Expand Up @@ -110,6 +111,34 @@ export class LedgerService {
return this._poolHandle
}

public async registerPublicDid(submitterDid: Did, targetDid: Did, verkey: string, alias: string, role?: NymRole) {
try {
this.logger.debug(`Register public did on ledger '${targetDid}'`)

const request = await this.indy.buildNymRequest(submitterDid, targetDid, verkey, alias, role || null)

const response = await this.submitWriteRequest(request, submitterDid)

this.logger.debug(`Registered public did '${targetDid}' on ledger`, {
response,
})

return targetDid
} catch (error) {
this.logger.error(`Error registering public did '${targetDid}' on ledger`, {
error,
submitterDid,
targetDid,
verkey,
alias,
role,
poolHandle: await this.getPoolHandle(),
})

throw error
}
}

public async getPublicDid(did: Did) {
try {
this.logger.debug(`Get public did '${did}' from ledger`)
Expand Down
19 changes: 15 additions & 4 deletions packages/core/tests/ledger.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type { SchemaId } from 'indy-sdk'

import { promises } from 'fs'
import * as indy from 'indy-sdk'

Expand All @@ -14,7 +12,7 @@ const { config: faberConfig, agentDependencies: faberDependencies } = getBaseCon

describe('ledger', () => {
let faberAgent: Agent
let schemaId: SchemaId
let schemaId: indy.SchemaId

beforeAll(async () => {
faberAgent = new Agent(faberConfig, faberDependencies)
Expand Down Expand Up @@ -57,11 +55,24 @@ describe('ledger', () => {
expect.objectContaining({
did: faberAgent.publicDid.did,
verkey: verkey,
role: '101',
role: '0',
})
)
})

test('register public DID on ledger', async () => {
if (!faberAgent.publicDid) {
throw new Error('Agent does not have public did.')
}

const targetDid = 'PNQm3CwyXbN5e39Rw3dXYx'
const targetVerkey = '~AHtGeRXtGjVfXALtXP9WiX'

const result = await faberAgent.ledger.registerPublicDid(targetDid, targetVerkey, 'alias', 'TRUST_ANCHOR')

expect(result).toEqual(targetDid)
})

test('register schema on ledger', async () => {
if (!faberAgent.publicDid) {
throw new Error('Agent does not have public did.')
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
"devDependencies": {
"@types/react-native": "^0.64.10",
"@types/rn-indy-sdk": "npm:@types/indy-sdk@^1.16.5",
"@types/rn-indy-sdk": "npm:@types/indy-sdk@^1.16.6",
"react": "17.0.1",
"react-native": "0.64.2",
"rimraf": "~3.0.2",
Expand Down
14 changes: 7 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2154,6 +2154,13 @@
dependencies:
"@types/node" "*"

"@types/indy-sdk@^1.16.6", "@types/rn-indy-sdk@npm:@types/indy-sdk@^1.16.6":
version "1.16.6"
resolved "https://registry.yarnpkg.com/@types/indy-sdk/-/indy-sdk-1.16.6.tgz#ec8df3dd70cb939c85caa6ebcc32d851e2f3c454"
integrity sha512-BhmqsM2z65aOrg6Hum7YICX02dQA2OS05BjEypdoScmPO3ySsZ5QXngeh6pAi+se5yGYp+cL5msoTqldAhlOGA==
dependencies:
buffer "^6.0.0"

"@types/hoist-non-react-statics@^3.3.0":
version "3.3.1"
resolved "https://registry.yarnpkg.com/@types/hoist-non-react-statics/-/hoist-non-react-statics-3.3.1.tgz#1124aafe5118cb591977aeb1ceaaed1070eb039f"
Expand All @@ -2162,13 +2169,6 @@
"@types/react" "*"
hoist-non-react-statics "^3.3.0"

"@types/indy-sdk@^1.16.5", "@types/rn-indy-sdk@npm:@types/indy-sdk@^1.16.5":
version "1.16.5"
resolved "https://registry.yarnpkg.com/@types/indy-sdk/-/indy-sdk-1.16.5.tgz#e9b6b917f8a95b5782d9eb7bc8be713c0f971b21"
integrity sha512-eDY2RIUdHIhVytx1k9u2mQgXfw1Fs3sW05kP8h3TDXl8mVXsxQyGs336z2GIApUux8vftsQOVEhrX13OinUmwQ==
dependencies:
buffer "^6.0.0"

"@types/istanbul-lib-coverage@*", "@types/istanbul-lib-coverage@^2.0.0", "@types/istanbul-lib-coverage@^2.0.1":
version "2.0.3"
resolved "https://registry.yarnpkg.com/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-2.0.3.tgz#4ba8ddb720221f432e443bd5f9117fd22cfd4762"
Expand Down

0 comments on commit 5f2d512

Please sign in to comment.