generated from cheqd/.github
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from cheqd/utils
feat(utils): Added utils & interoperability improvements
- Loading branch information
Showing
4 changed files
with
89 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { VerificationMethod } from "@cheqd/ts-proto/cheqd/v1/did" | ||
import { IKeyValuePair, ISignInputs, VerificationMethods } from "./types" | ||
import { fromString, toString } from 'uint8arrays' | ||
import { bases } from "multiformats/basics" | ||
|
||
|
||
export type TImportableEd25519Key = { | ||
publicKeyHex: string | ||
privateKeyHex: string | ||
kid: string | ||
type: "Ed25519" | ||
} | ||
|
||
export function parseToKeyValuePair(object: { [key: string]: any }): IKeyValuePair[] { | ||
return Object.entries(object).map(([key, value]) => ({ key, value })) | ||
} | ||
|
||
export function createSignInputsFromImportableEd25519Key(key: TImportableEd25519Key, verificationMethod: VerificationMethod[]): ISignInputs { | ||
if (verificationMethod?.length === 0) throw new Error('No verification methods provided') | ||
|
||
const publicKey = fromString(key.publicKeyHex, 'hex') | ||
|
||
for(const method of verificationMethod) { | ||
switch (method?.type) { | ||
case VerificationMethods.Base58: | ||
const publicKeyMultibase = bases['base58btc'].encode(publicKey) | ||
if (method.publicKeyMultibase === publicKeyMultibase) { | ||
return { | ||
verificationMethodId: method.id, | ||
privateKeyHex: key.privateKeyHex | ||
} | ||
} | ||
|
||
case VerificationMethods.JWK: | ||
const publicKeyJWK = parseToKeyValuePair({ | ||
crv: 'Ed25519', | ||
kty: 'OKP', | ||
x: toString( publicKey, 'base64url' ) | ||
}) | ||
if (method.publicKeyJwk === publicKeyJWK) { | ||
return { | ||
verificationMethodId: method.id, | ||
privateKeyHex: key.privateKeyHex | ||
} | ||
} | ||
} | ||
} | ||
|
||
throw new Error('No verification method type provided') | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { TImportableEd25519Key, createSignInputsFromImportableEd25519Key } from '../src/utils' | ||
import { createDidVerificationMethod, createVerificationKeys, createKeyPairRaw } from './testutils.test' | ||
import { toString } from 'uint8arrays' | ||
import { IKeyPair, MethodSpecificIdAlgo, VerificationMethods } from '../src/types' | ||
|
||
describe('createSignInputsFromImportableEd25519Key', () => { | ||
it('should create a sign input from an importable ed25519 key', async () => { | ||
const keyPair = createKeyPairRaw() | ||
const importableEd25519Key: TImportableEd25519Key = { | ||
publicKeyHex: toString(keyPair.publicKey, 'hex'), | ||
privateKeyHex: toString(keyPair.secretKey, 'hex'), | ||
kid: toString(keyPair.publicKey, 'hex'), | ||
type: 'Ed25519' | ||
} | ||
const keyPairBase64: IKeyPair = { | ||
publicKey: toString(keyPair.publicKey, 'base64'), | ||
privateKey: toString(keyPair.secretKey, 'base64'), | ||
} | ||
|
||
const verificationKeys = createVerificationKeys(keyPairBase64, MethodSpecificIdAlgo.Base58, 'key-1', 16) | ||
const verificationMethod = createDidVerificationMethod([VerificationMethods.Base58], [verificationKeys]) | ||
const signInput = createSignInputsFromImportableEd25519Key(importableEd25519Key, verificationMethod) | ||
|
||
expect(signInput).toEqual({ verificationMethodId: verificationKeys.keyId, privateKeyHex: importableEd25519Key.privateKeyHex }) | ||
}) | ||
}) |