Skip to content

Commit

Permalink
Merge pull request #10 from cheqd/utils
Browse files Browse the repository at this point in the history
feat(utils): Added utils & interoperability improvements
  • Loading branch information
Eengineer1 authored Aug 3, 2022
2 parents 07da1ed + d0f5f4d commit 11d8936
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { GeneratedType, OfflineSigner, Registry } from '@cosmjs/proto-signing'
import { OfflineSigner, Registry } from '@cosmjs/proto-signing'
import { DIDModule, MinimalImportableDIDModule } from './modules/did'
import { MinimalImportableResourcesModule, ResourcesModule } from './modules/resources'
import { AbstractCheqdSDKModule, applyMixins, instantiateCheqdSDKModule, instantiateCheqdSDKModuleRegistryTypes, } from './modules/_'
import { createDefaultCheqdRegistry } from './registry'
import { CheqdSigningStargateClient } from './signer'
import { CheqdNetwork, IContext, IModuleMethodMap } from './types'
import { createSignInputsFromImportableEd25519Key } from './utils'

export interface ICheqdSDKOptions {
modules: AbstractCheqdSDKModule[]
Expand Down Expand Up @@ -101,3 +102,4 @@ export async function createCheqdSDK(options: ICheqdSDKOptions): Promise<CheqdSD
}

export { DIDModule, ResourcesModule }
export { createSignInputsFromImportableEd25519Key }
50 changes: 50 additions & 0 deletions src/utils.ts
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')
}
15 changes: 10 additions & 5 deletions tests/testutils.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { MsgCreateDidPayload } from "@cheqd/ts-proto/cheqd/v1/tx"
import { CheqdNetwork, IKeyPair, IKeyValuePair, IVerificationKeys, MethodSpecificIdAlgo, TMethodSpecificId, TVerificationKey, TVerificationKeyPrefix, VerificationMethods } from "../src/types"
import { CheqdNetwork, IKeyPair, IVerificationKeys, MethodSpecificIdAlgo, TMethodSpecificId, TVerificationKey, TVerificationKeyPrefix, VerificationMethods } from "../src/types"
import { bases } from 'multiformats/basics'
import { base64ToBytes } from "did-jwt"
import { fromString, toString } from 'uint8arrays'
import { generateKeyPair, KeyPair } from '@stablelib/ed25519'
import { GasPrice } from "@cosmjs/stargate"
import { v4 } from 'uuid'
import { VerificationMethod } from "@cheqd/ts-proto/cheqd/v1/did"
import { parseToKeyValuePair } from '../src/utils'

export const faucet = {
prefix: 'cheqd',
Expand All @@ -33,6 +34,14 @@ export function createKeyPairBase64(): IKeyPair {
}
}

export function createKeyPairHex(): IKeyPair {
const keyPair = generateKeyPair()
return {
publicKey: toString(keyPair.publicKey, 'hex'),
privateKey: toString(keyPair.secretKey, 'hex'),
}
}

export function createVerificationKeys(keyPair: IKeyPair, algo: MethodSpecificIdAlgo, key: TVerificationKey<TVerificationKeyPrefix, number>, length: number = 32, network: CheqdNetwork = CheqdNetwork.Testnet): IVerificationKeys {
let methodSpecificId: TMethodSpecificId
let didUrl: IVerificationKeys['didUrl']
Expand Down Expand Up @@ -103,8 +112,4 @@ export function createDidPayload(verificationMethods: VerificationMethod[], veri
authentication: verificationKeys.map(key => key.keyId)
}
)
}

export function parseToKeyValuePair(object: { [key: string]: any }): IKeyValuePair[] {
return Object.entries(object).map(([key, value]) => ({ key, value }))
}
26 changes: 26 additions & 0 deletions tests/utils.test.ts
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 })
})
})

0 comments on commit 11d8936

Please sign in to comment.