Skip to content

Commit

Permalink
feat: add removeKey, removeService methods to ethr-did-provider (#310)
Browse files Browse the repository at this point in the history
closes #144
  • Loading branch information
simonas-notcat authored Dec 17, 2020
1 parent 172c908 commit faccca6
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 134 deletions.
10 changes: 5 additions & 5 deletions packages/daf-cli/default/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,39 +116,39 @@ agent:
- defaultKms: local
network: mainnet
rpcUrl: https://mainnet.infura.io/v3/5ffc47f65c4042ce847ef66a3fa70d4c
gas: 10001
gas: 1000001
ttl: 31104001
did:ethr:rinkeby:
$require: daf-ethr-did#EthrDIDProvider
$args:
- defaultKms: local
network: rinkeby
rpcUrl: https://rinkeby.infura.io/v3/5ffc47f65c4042ce847ef66a3fa70d4c
gas: 10001
gas: 1000001
ttl: 31104001
did:ethr:ropsten:
$require: daf-ethr-did#EthrDIDProvider
$args:
- defaultKms: local
network: ropsten
rpcUrl: https://ropsten.infura.io/v3/5ffc47f65c4042ce847ef66a3fa70d4c
gas: 10001
gas: 1000001
ttl: 31104001
did:ethr:kovan:
$require: daf-ethr-did#EthrDIDProvider
$args:
- defaultKms: local
network: kovan
rpcUrl: https://kovan.infura.io/v3/5ffc47f65c4042ce847ef66a3fa70d4c
gas: 10001
gas: 1000001
ttl: 31104001
did:ethr:goerli:
$require: daf-ethr-did#EthrDIDProvider
$args:
- defaultKms: local
network: goerli
rpcUrl: https://goerli.infura.io/v3/5ffc47f65c4042ce847ef66a3fa70d4c
gas: 10001
gas: 1000001
ttl: 31104001
did:web:
$require: daf-web-did#WebDIDProvider
Expand Down
66 changes: 66 additions & 0 deletions packages/daf-cli/src/did.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,39 @@ did
}
})

did
.command('remove-service')
.description('remove a service endpoint from did document')
.action(async (cmd) => {
const agent = getAgent(program.config)

try {
const identifiers = await agent.didManagerFind()
const answers = await inquirer.prompt([
{
type: 'list',
name: 'did',
choices: identifiers.map((item) => item.did),
message: 'Select DID',
},
{
type: 'text',
name: 'id',
message: 'ID',
},
])

const result = await agent.didManagerRemoveService({
did: answers.did,
id: answers.id,
})

console.log('Success:', result)
} catch (e) {
console.error(e)
}
})

did
.command('add-key')
.description('create and add a public key to did document')
Expand Down Expand Up @@ -197,6 +230,39 @@ did
}
})

did
.command('remove-key')
.description('remove a key from did document')
.action(async (cmd) => {
const agent = getAgent(program.config)

try {
const identifiers = await agent.didManagerFind()
const answers = await inquirer.prompt([
{
type: 'list',
name: 'did',
choices: identifiers.map((item) => item.did),
message: 'Select DID',
},
{
type: 'text',
name: 'id',
message: 'ID',
},
])

const result = await agent.didManagerRemoveKey({
did: answers.did,
kid: answers.id,
})

console.log('Success:', result)
} catch (e) {
console.error(e)
}
})

did
.command('export')
.description('export an identifier')
Expand Down
2 changes: 1 addition & 1 deletion packages/daf-ethr-did/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"daf-identity-manager": "^7.0.0-beta.57",
"debug": "^4.1.1",
"ethjs-provider-signer": "^0.1.4",
"ethr-did": "^1.1.0",
"ethr-did": "^1.3.0",
"js-sha3": "^0.8.0"
},
"devDependencies": {
Expand Down
42 changes: 37 additions & 5 deletions packages/daf-ethr-did/src/identifier-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { IIdentifier, IKey, IService, IAgentContext, IKeyManager } from 'daf-cor
import { AbstractIdentifierProvider } from 'daf-identity-manager'
import { keccak_256 } from 'js-sha3'
import Debug from 'debug'
const EthrDID = require('ethr-did')
import EthrDID from 'ethr-did'
const SignerProvider = require('ethjs-provider-signer')
const debug = Debug('daf:ethr-did:identifier-provider')

Expand Down Expand Up @@ -97,7 +97,7 @@ export class EthrDIDProvider extends AbstractIdentifierProvider {
{ identifier, key, options }: { identifier: IIdentifier; key: IKey; options?: any },
context: IContext,
): Promise<any> {
const address = identifier.did.split(':').pop()
const address = identifier.did.split(':').pop() as string
const ethrDid = new EthrDID({
address,
provider: this.getWeb3Provider(identifier, context),
Expand All @@ -122,7 +122,7 @@ export class EthrDIDProvider extends AbstractIdentifierProvider {
context: IContext,
): Promise<any> {
const ethrDid = new EthrDID({
address: identifier.did.split(':').pop(),
address: identifier.did.split(':').pop() as string,
provider: this.getWeb3Provider(identifier, context),
registry: this.registry,
})
Expand All @@ -143,13 +143,45 @@ export class EthrDIDProvider extends AbstractIdentifierProvider {
args: { identifier: IIdentifier; kid: string; options?: any },
context: IContext,
): Promise<any> {
throw Error('Not implemented')
const address = args.identifier.did.split(':').pop() as string
const ethrDid = new EthrDID({
address,
provider: this.getWeb3Provider(args.identifier, context),
registry: this.registry,
})

const key = args.identifier.keys.find((k) => k.kid === args.kid)
if (!key) throw Error('Key not found')

const usg = 'veriKey'
const attribute = 'did/pub/' + key.type + '/' + usg + '/hex'
const value = '0x' + key.publicKeyHex
const gas = args.options?.gas || this.gas

debug('ethrDid.revokeAttribute', { attribute, value, gas })
const txHash = await ethrDid.revokeAttribute(attribute, value, gas)
return txHash
}

async removeService(
args: { identifier: IIdentifier; id: string; options?: any },
context: IContext,
): Promise<any> {
throw Error('Not implemented')
const ethrDid = new EthrDID({
address: args.identifier.did.split(':').pop() as string,
provider: this.getWeb3Provider(args.identifier, context),
registry: this.registry,
})

const service = args.identifier.services.find((s) => s.id === args.id)
if (!service) throw Error('Service not found')

const attribute = 'did/svc/' + service.type
const value = service.serviceEndpoint
const gas = args.options?.gas || this.gas

debug('ethrDid.revokeAttribute', { attribute, value, gas })
const txHash = await ethrDid.revokeAttribute(attribute, value, gas)
return txHash
}
}
Loading

0 comments on commit faccca6

Please sign in to comment.