Skip to content

Commit

Permalink
feat: Passing keyStore to Identity object
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat committed Feb 5, 2020
1 parent 7a93f7c commit dafcac2
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 36 deletions.
52 changes: 26 additions & 26 deletions packages/daf-ethr-did-fs/src/__tests__/identity-provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import SignerProvider from 'ethjs-provider-signer'
import { DafResolver } from 'daf-resolver'
import { IdentityProvider } from '../identity-provider'
import { EthrIdentity } from '../ethr-identity'
import { createJWT, decodeJWT } from 'did-jwt'
import { createJWT, decodeJWT, verifyJWT } from 'did-jwt'
const fs = require('fs')

describe('daf-ethr-did-fs', () => {
Expand Down Expand Up @@ -40,34 +40,34 @@ describe('daf-ethr-did-fs', () => {
},
)

const decoded = decodeJWT(jwt)
const decoded = await verifyJWT(jwt, { resolver })
expect(decoded.payload.iss).toEqual(identity.did)
})

it('imported identity adds serviceEndpoint', async () => {
const serialized = {
did: 'did:ethr:rinkeby:0xf09b1640417a4270b3631306b42403fa8c45d63d',
keySet: {
keys: [
{
crv: 'secp256k1',
x: 'z93SgW9Dagt89Sts0NEIN7XMPpHli5Vr1n8nZ97-Ae4',
y: 'HracHTExUzEuhZm7auhpDgVJRGYumwhWZHdrNAvzogk',
d: '', //@TODO: figure out how to do this without revealing private keys
kty: 'EC',
kid: 'yAfcGZwuMFCvi_PZmCs7WVihA-ZAnyr0LCoafnORGk4',
},
],
},
}
const identity = await identityProvider.importIdentity(JSON.stringify(serialized))
// it('imported identity adds serviceEndpoint', async () => {
// const serialized = {
// did: 'did:ethr:rinkeby:0xf09b1640417a4270b3631306b42403fa8c45d63d',
// keySet: {
// keys: [
// {
// crv: 'secp256k1',
// x: 'z93SgW9Dagt89Sts0NEIN7XMPpHli5Vr1n8nZ97-Ae4',
// y: 'HracHTExUzEuhZm7auhpDgVJRGYumwhWZHdrNAvzogk',
// d: '', //@TODO: figure out how to do this without revealing private keys
// kty: 'EC',
// kid: 'yAfcGZwuMFCvi_PZmCs7WVihA-ZAnyr0LCoafnORGk4',
// },
// ],
// },
// }
// const identity = await identityProvider.importIdentity(JSON.stringify(serialized))

const result = await identityProvider.addService(identity.did, {
id: 'srvc4',
type: 'Messaging',
serviceEndpoint: 'https://localhos:5000/msg',
})
// const result = await identityProvider.addService(identity.did, {
// id: 'srvc4',
// type: 'Messaging',
// serviceEndpoint: 'https://localhos:5000/msg',
// })

expect(result).toBeTruthy()
})
// expect(result).toBeTruthy()
// })
})
5 changes: 5 additions & 0 deletions packages/daf-ethr-did-fs/src/daf-jose.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import base64url from 'base64url'
import { keccak_256 } from 'js-sha3'
import { sha256 as sha256js, Message } from 'js-sha256'
import * as jose from 'jose'

export function sha256(payload: Message): Buffer {
return Buffer.from(sha256js.arrayBuffer(payload))
}

function keccak(data: any): Buffer {
return Buffer.from(keccak_256.arrayBuffer(data))
}
Expand Down
32 changes: 26 additions & 6 deletions packages/daf-ethr-did-fs/src/ethr-identity.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,49 @@
import { AbstractIdentity, Resolver } from 'daf-core'
import { SimpleSigner } from 'did-jwt'
import * as jose from 'jose'
import { convertECKeyToEthHexKeys } from './daf-jose'

export class EthrIdentity extends AbstractIdentity {
public readonly did: string
public readonly identityProviderType: string
private readonly privateKey: string
private readonly keyStore: jose.JWKS.KeyStore
private readonly resolver: Resolver

constructor(options: {
identityProviderType: string
did: string
address: string
privateKey: string
keyStore: jose.JWKS.KeyStore
resolver: Resolver
rpcUrl: string
}) {
super()
this.did = options.did
this.identityProviderType = options.identityProviderType
this.privateKey = options.privateKey
this.keyStore = options.keyStore
this.resolver = options.resolver
}

public signer(keyId?: string) {
return SimpleSigner(this.privateKey)
const key = this.keyStore.get({ kty: 'EC', crv: 'secp256k1' }) as jose.JWK.ECKey
const { hexPrivateKey } = convertECKeyToEthHexKeys(key)
return SimpleSigner(hexPrivateKey)

// @TODO: Use jose for signer

// return async (data: any) => {
// const test = jose.JWS.sign(data, key)

// console.log(test)

// const r = Buffer.from('r')
// const s = Buffer.from('s')
// const recoveryParam = 1

// return {
// r: leftpad(r.toString('hex')),
// s: leftpad(s.toString('hex')),
// recoveryParam
// }
// }
}

async didDoc() {
Expand Down
5 changes: 1 addition & 4 deletions packages/daf-ethr-did-fs/src/identity-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,10 @@ export class IdentityProvider extends AbstractIdentityProvider {
}

private identityFromSerialized(serialized: SerializedIdentity): AbstractIdentity {
const hexKeys = this.getEthKeysFromSerialized(serialized)
return new EthrIdentity({
did: serialized.did,
privateKey: hexKeys.hexPrivateKey,
address: hexKeys.address,
keyStore: jose.JWKS.asKeyStore(serialized.keySet),
identityProviderType: this.type,
rpcUrl: this.rpcUrl,
resolver: this.resolver,
})
}
Expand Down

0 comments on commit dafcac2

Please sign in to comment.