This repository has been archived by the owner on Aug 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Dp did key improvements (#318)
* feat: adding option to not resolve dids for presentation * feat: Adding basic logic to support new did key method * fix: fix tests and typos * fix: work in progress * fix: cleanup * fix: preparing for publish * fix: clean up * fix: missed declaration * fix: making lint happy * fix: resolver not for prod * fix: skip tests for url resolver * fix: skip resolution for polygon cause of registry issue * fix: update HANGLEOGs * fix: trying new codeclimate version * fix: put back previous code climate version * fix: trying code climate version 3.0 * fix: playing with code climatre --------- Co-authored-by: Denys <denys.p@affinidi.com>
- Loading branch information
1 parent
4ce2efa
commit 315ee5f
Showing
44 changed files
with
564 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
common-libs/common/src/services/DidDocumentService/KeyDidDocumentLocalResolver.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Resolver } from 'did-resolver' | ||
import { DidDocument } from '../../shared/interfaces' | ||
import KeyDidDocumentService from './KeyDidDocumentService' | ||
import { decodeBase58 } from '../../utils/ethUtils' | ||
|
||
const MULTIBASE_ENCODED_BASE58_IDENTIFIER = 'z' // z represents the multibase encoding scheme of base58 encoding, https://github.com/multiformats/multibase/blob/master/multibase.csv#L18 | ||
|
||
const pubKeyFromDid = (did: string): { publicKey: Buffer; fingerprint: string } => { | ||
const fingerprint = did.split(':')[2] | ||
const encoded = fingerprint.replace(MULTIBASE_ENCODED_BASE58_IDENTIFIER, '') | ||
const decoded = decodeBase58(encoded) | ||
|
||
const publicKey = decoded.slice(2, decoded.length) | ||
|
||
return { publicKey, fingerprint } | ||
} | ||
|
||
const resolveKeyDID = async (did: string) => { | ||
const { publicKey, fingerprint } = pubKeyFromDid(did) | ||
const didDocument = KeyDidDocumentService.buildDidDocumentFromPubKey(publicKey, fingerprint) | ||
|
||
return didDocument | ||
} | ||
|
||
export const resolveKeyDIDWithParams = async (did: string, isDidJson: boolean = false) => { | ||
const { publicKey, fingerprint } = pubKeyFromDid(did) | ||
const didDocument = KeyDidDocumentService.buildDidDocumentFromPubKey(publicKey, fingerprint, isDidJson) | ||
|
||
return didDocument | ||
} | ||
|
||
export const resolveDidKeyLocal = (did: string) => { | ||
const resolver = new Resolver({ key: resolveKeyDID }) | ||
return resolver.resolve(did) as Promise<DidDocument> | ||
} |
103 changes: 103 additions & 0 deletions
103
common-libs/common/src/services/DidDocumentService/KeyDidDocumentService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { KeyVault } from './KeyVault' | ||
import { encodeBase58 } from '../../utils/ethUtils' | ||
import DidDocumentService from './index' | ||
|
||
const SECP256K1_MULTICODEC_IDENTIFIER = 0xe7 // 0xe7 indicates a Secp256k1 public key | ||
const VARIABLE_INTEGER_TRAILING_BYTE = 0x01 // 0x01 indicates the end of the leading bytes according to variable integer spec, https://github.com/multiformats/multibase/blob/master/multibase.csv#L18 | ||
const MULTIBASE_ENCODED_BASE58_IDENTIFIER = 'z' // z represents the multibase encoding scheme of base58 encoding, https://github.com/multiformats/multibase/blob/master/multibase.csv#L18 | ||
|
||
export default class KeyDidDocumentService { | ||
private _keyProvider | ||
|
||
constructor(keyProvider: KeyVault) { | ||
this._keyProvider = keyProvider | ||
} | ||
|
||
_fingerprintFromPubKey(publicKey: Buffer): string { | ||
const unitArray = new Uint8Array(2 + publicKey.length) | ||
unitArray[0] = SECP256K1_MULTICODEC_IDENTIFIER | ||
unitArray[1] = VARIABLE_INTEGER_TRAILING_BYTE | ||
unitArray.set(publicKey, 2) | ||
|
||
const buffer = Buffer.from(unitArray) | ||
|
||
return `${MULTIBASE_ENCODED_BASE58_IDENTIFIER}${encodeBase58(buffer)}` | ||
} | ||
|
||
getMyDid(): string { | ||
const publicKey = this._keyProvider.primaryPublicKey | ||
const fingerprint = this._fingerprintFromPubKey(publicKey) | ||
return `did:key:${fingerprint}` | ||
} | ||
|
||
_getFingerPrintFromDid(did: string): string { | ||
const fingerprint = did.split(':')[2] | ||
|
||
return fingerprint | ||
} | ||
|
||
getKeyId() { | ||
const did = this.getMyDid() | ||
const fingerprint = this._getFingerPrintFromDid(did) | ||
|
||
return `${did}#${fingerprint}` | ||
} | ||
|
||
static buildDidDocumentFromPubKey(pubKeyBytes: Buffer, fingerprint: string, isDidJson: boolean = false) { | ||
const did = `did:key:${fingerprint}` | ||
const keyId = `${did}#${fingerprint}` | ||
let publicKey: any = { | ||
id: keyId, | ||
type: 'Secp256k1VerificationKey2018', | ||
controller: did, | ||
publicKeyBase58: encodeBase58(pubKeyBytes), | ||
} | ||
|
||
if (isDidJson) { | ||
const publicKeyJwk = DidDocumentService.getPublicKeyJwkFromPublicKey(pubKeyBytes) | ||
publicKey = { | ||
id: keyId, | ||
type: 'JsonWebKey2020', | ||
controller: did, | ||
publicKeyJwk, | ||
} | ||
} | ||
|
||
return { | ||
'@context': ['https://www.w3.org/ns/did/v1', 'https://w3id.org/security/suites/jws-2020/v1'], | ||
id: did, | ||
// verificationMethod: [publicKey], | ||
publicKey: [publicKey], | ||
authentication: [keyId], | ||
assertionMethod: [keyId], | ||
capabilityDelegation: [keyId], | ||
capabilityInvocation: [keyId], | ||
keyAgreement: [keyId], | ||
} | ||
} | ||
|
||
async _buildDidDocumentInformation(isDidJson: boolean = false) { | ||
const did = this.getMyDid() | ||
const fingerprint = this._getFingerPrintFromDid(did) | ||
const publicKey = this._keyProvider.primaryPublicKey | ||
|
||
const didDocument = KeyDidDocumentService.buildDidDocumentFromPubKey(publicKey, fingerprint, isDidJson) | ||
|
||
return { did, didDocument, fingerprint } | ||
} | ||
|
||
async buildDidDocumentForRegister() { | ||
const { did, didDocument, fingerprint } = await this._buildDidDocumentInformation() | ||
|
||
return { | ||
did, | ||
didDocument, | ||
keyId: `${did}#${fingerprint}`, | ||
} | ||
} | ||
|
||
async getDidDocument() { | ||
const { didDocument } = await this._buildDidDocumentInformation() | ||
return didDocument | ||
} | ||
} |
Oops, something went wrong.