diff --git a/src/index.ts b/src/index.ts index 24132dc1..a583d338 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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[] @@ -101,3 +102,4 @@ export async function createCheqdSDK(options: ICheqdSDKOptions): Promise ({ 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') +} \ No newline at end of file diff --git a/tests/testutils.test.ts b/tests/testutils.test.ts index 51417b4e..3aad8f5d 100644 --- a/tests/testutils.test.ts +++ b/tests/testutils.test.ts @@ -1,5 +1,5 @@ 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' @@ -7,6 +7,7 @@ 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', @@ -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, length: number = 32, network: CheqdNetwork = CheqdNetwork.Testnet): IVerificationKeys { let methodSpecificId: TMethodSpecificId let didUrl: IVerificationKeys['didUrl'] @@ -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 })) } \ No newline at end of file diff --git a/tests/utils.test.ts b/tests/utils.test.ts new file mode 100644 index 00000000..68211d3d --- /dev/null +++ b/tests/utils.test.ts @@ -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 }) + }) +}) \ No newline at end of file