Skip to content

Commit

Permalink
fix: Ethr-did debug
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat authored and mirceanis committed Sep 7, 2020
1 parent fb58ddc commit 688595f
Showing 1 changed file with 31 additions and 20 deletions.
51 changes: 31 additions & 20 deletions packages/daf-ethr-did/src/identity-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ interface IContext {
agent: IAgentBase & IAgentKeyManager
}


export function toEthereumAddress(hexPublicKey: string): string {
return `0x${Buffer.from(keccak_256.arrayBuffer(Buffer.from(hexPublicKey.slice(2), 'hex')))
.slice(-20)
Expand Down Expand Up @@ -47,14 +46,13 @@ export class EthrIdentityProvider extends AbstractIdentityProvider {
{ kms, options }: { kms: string; options?: any },
context: IContext,
): Promise<Omit<IIdentity, 'provider'>> {

const key = await context.agent.keyManagerCreateKey({ kms: kms || this.defaultKms, type: 'Secp256k1' })
const address = toEthereumAddress(key.publicKeyHex)
const identity: Omit<IIdentity, 'provider'> = {
did: 'did:ethr:' + (this.network !== 'mainnet' ? this.network + ':' : '') + address,
controllerKeyId: key.kid,
keys: [key],
services: []
services: [],
}
debug('Created', identity.did)
return identity
Expand All @@ -73,19 +71,24 @@ export class EthrIdentityProvider extends AbstractIdentityProvider {
const web3Provider =
this.web3Provider ||
new SignerProvider(this.rpcUrl, {
signTransaction: (transaction: object, callback: (error: string | null, signature: string) => void) => {
context.agent.keyManagerSignEthTX({kid: controllerKeyId, transaction})
signTransaction: (
transaction: object,
callback: (error: string | null, signature: string) => void,
) => {
context.agent
.keyManagerSignEthTX({ kid: controllerKeyId, transaction })
.then(signature => callback(null, signature))
.catch(error => callback(error, null))
},
})
return web3Provider
}

async addKey({ identity, key, options }: { identity: IIdentity; key: IKey; options?: any }, context: IContext): Promise<any> {

async addKey(
{ identity, key, options }: { identity: IIdentity; key: IKey; options?: any },
context: IContext,
): Promise<any> {
const address = identity.did.split(':').pop()
debug('addKey', { identity, key, options, address})
const ethrDid = new EthrDID({
address,
provider: this.getWeb3Provider(identity, context),
Expand All @@ -95,17 +98,20 @@ export class EthrIdentityProvider extends AbstractIdentityProvider {
const usg = 'veriKey'
const attribute = 'did/pub/' + key.type + '/' + usg + '/hex'
const value = '0x' + key.publicKeyHex
const ttl = options?.ttl || this.ttl
const gas = options?.gas || this.gas
debug('ethrDid.setAttribute', { attribute, value, ttl, gas })
const ttl = options?.ttl || this.ttl
const gas = options?.gas || this.gas

debug('ethrDid.setAttribute %o', { attribute, value, ttl, gas })

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

async addService({ identity, service, options }: { identity: IIdentity; service: IService; options?: any }, context: IContext): Promise<any> {
async addService(
{ identity, service, options }: { identity: IIdentity; service: IService; options?: any },
context: IContext,
): Promise<any> {
const ethrDid = new EthrDID({
address: identity.did.split(':').pop(),
provider: this.getWeb3Provider(identity, context),
Expand All @@ -114,22 +120,27 @@ export class EthrIdentityProvider extends AbstractIdentityProvider {

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

debug('ethrDid.setAttribute %o', { attribute, value, ttl, gas })

debug('ethrDid.setAttribute', { attribute, value, ttl, gas })

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

async removeKey(args: { identity: IIdentity; kid: string; options?: any }, context: IContext): Promise<any> {
async removeKey(
args: { identity: IIdentity; kid: string; options?: any },
context: IContext,
): Promise<any> {
throw Error('Not implemented')
}

async removeService(args: { identity: IIdentity; id: string; options?: any }, context: IContext): Promise<any> {
async removeService(
args: { identity: IIdentity; id: string; options?: any },
context: IContext,
): Promise<any> {
throw Error('Not implemented')
}

}

0 comments on commit 688595f

Please sign in to comment.