Skip to content

Commit

Permalink
perf(helpers): Added env-specific utils exports
Browse files Browse the repository at this point in the history
  • Loading branch information
Eengineer1 committed Aug 9, 2022
1 parent aec6284 commit 4957f44
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 2 deletions.
109 changes: 108 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
import { VerificationMethod } from "@cheqd/ts-proto/cheqd/v1/did"
import { IKeyValuePair, ISignInputs, VerificationMethods } from "./types"
import {
IKeyPair,
IKeyValuePair,
ISignInputs,
VerificationMethods,
TMethodSpecificId,
MethodSpecificIdAlgo,
TVerificationKey,
TVerificationKeyPrefix,
CheqdNetwork,
IVerificationKeys
} from "./types"
import { fromString, toString } from 'uint8arrays'
import { bases } from "multiformats/basics"
import { base64ToBytes } from "did-jwt"
import { generateKeyPair, KeyPair } from '@stablelib/ed25519'
import { v4 } from 'uuid'
import { MsgCreateDidPayload } from "@cheqd/ts-proto/cheqd/v1/tx"


export type TImportableEd25519Key = {
Expand Down Expand Up @@ -47,4 +62,96 @@ export function createSignInputsFromImportableEd25519Key(key: TImportableEd25519
}

throw new Error('No verification method type provided')
}

export function createKeyPairRaw(): KeyPair {
return generateKeyPair()
}

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

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']
switch (algo) {
case MethodSpecificIdAlgo.Base58:
methodSpecificId = bases['base58btc'].encode(base64ToBytes(keyPair.publicKey))
didUrl = `did:cheqd:${network}:${methodSpecificId.substring(0, length)}`
return {
methodSpecificId,
didUrl,
keyId: `${didUrl}#${key}`,
publicKey: keyPair.publicKey,
}
case MethodSpecificIdAlgo.Uuid:
methodSpecificId = bases['base58btc'].encode(base64ToBytes(keyPair.publicKey))
didUrl = `did:cheqd:${network}:${v4()}`
return {
methodSpecificId,
didUrl,
keyId: `${didUrl}#${key}`,
publicKey: keyPair.publicKey,
}
}
}

export function createDidVerificationMethod(verificationMethodTypes: VerificationMethods[], verificationKeys: IVerificationKeys[]): VerificationMethod[] {
return verificationMethodTypes.map((type, _) => {
switch (type) {
case VerificationMethods.Base58:
return {
id: verificationKeys[_].keyId,
type: type,
controller: verificationKeys[_].didUrl,
publicKeyMultibase: verificationKeys[_].methodSpecificId,
publicKeyJwk: []
}

case VerificationMethods.JWK:
return {
id: verificationKeys[_].keyId,
type: type,
controller: verificationKeys[_].didUrl,
publicKeyJwk: parseToKeyValuePair(
{
crv: 'Ed25519',
kty: 'OKP',
x: toString( fromString( verificationKeys[_].publicKey, 'base64pad' ), 'base64url' )
}
),
publicKeyMultibase: ''
}
}
}) ?? []
}

export function createDidPayload(verificationMethods: VerificationMethod[], verificationKeys: IVerificationKeys[]): MsgCreateDidPayload {
if (!verificationMethods || verificationMethods.length === 0)
throw new Error('No verification methods provided')
if (!verificationKeys || verificationKeys.length === 0)
throw new Error('No verification keys provided')

const did = verificationKeys[0].didUrl
return MsgCreateDidPayload.fromPartial(
{
id: did,
controller: verificationKeys.map(key => key.didUrl),
verificationMethod: verificationMethods,
authentication: verificationKeys.map(key => key.keyId)
}
)
}
10 changes: 9 additions & 1 deletion tests/modules/did.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { DIDModule } from "../../src"
import { createDefaultCheqdRegistry } from "../../src/registry"
import { CheqdSigningStargateClient } from "../../src/signer"
import { DidStdFee, ISignInputs, MethodSpecificIdAlgo, VerificationMethods } from "../../src/types"
import { createDidPayload, createDidVerificationMethod, createKeyPairBase64, createVerificationKeys, exampleCheqdNetwork, faucet } from "../testutils.test"
import { createDidPayload, createDidVerificationMethod, createKeyPairBase64, createKeyPairHex, createVerificationKeys, exampleCheqdNetwork, faucet } from "../testutils.test"

const defaultAsyncTxTimeout = 20000

Expand All @@ -30,6 +30,14 @@ describe('DIDModule', () => {
const verificationKeys = createVerificationKeys(keyPair, MethodSpecificIdAlgo.Base58, 'key-1', 16)
const verificationMethods = createDidVerificationMethod([VerificationMethods.Base58], [verificationKeys])
const didPayload = createDidPayload(verificationMethods, [verificationKeys])

const keyPairHex = { publicKeyHex: toString(fromString(keyPair.publicKey, 'base64'), 'hex'), privateKeyHex: toString(fromString(keyPair.privateKey, 'base64'), 'hex') }

console.warn('Keypair:', JSON.stringify(keyPairHex, null, 2))
console.warn('Payload:', JSON.stringify(didPayload, null, 2))

throw new Error('h3h3')

const signInputs: ISignInputs[] = [
{
verificationMethodId: didPayload.verificationMethod[0].id,
Expand Down
30 changes: 30 additions & 0 deletions tests/testutils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,20 @@ export const exampleCheqdNetwork = {
gasPrice: GasPrice.fromString( `25${faucet.minimalDenom}` )
}

/**
*? General test utils. Look for src/utils.ts for stable utils exports.
*? Used for testing purposes.
** NOTE: The following utils are stable but subject to change at any given moment.
*/
export function createKeyPairRaw(): KeyPair {
return generateKeyPair()
}

/**
*? General test utils. Look for src/utils.ts for stable utils exports.
*? Used for testing purposes.
** NOTE: The following utils are stable but subject to change at any given moment.
*/
export function createKeyPairBase64(): IKeyPair {
const keyPair = generateKeyPair()
return {
Expand All @@ -34,6 +44,11 @@ export function createKeyPairBase64(): IKeyPair {
}
}

/**
*? General test utils. Look for src/utils.ts for stable utils exports.
*? Used for testing purposes.
** NOTE: The following utils are stable but subject to change at any given moment.
*/
export function createKeyPairHex(): IKeyPair {
const keyPair = generateKeyPair()
return {
Expand All @@ -42,6 +57,11 @@ export function createKeyPairHex(): IKeyPair {
}
}

/**
*? General test utils. Look for src/utils.ts for stable utils exports.
*? Used for testing purposes.
** NOTE: The following utils are stable but subject to change at any given moment.
*/
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 All @@ -67,6 +87,11 @@ export function createVerificationKeys(keyPair: IKeyPair, algo: MethodSpecificId
}
}

/**
*? General test utils. Look for src/utils.ts for stable utils exports.
*? Used for testing purposes.
** NOTE: The following utils are stable but subject to change at any given moment.
*/
export function createDidVerificationMethod(verificationMethodTypes: VerificationMethods[], verificationKeys: IVerificationKeys[]): VerificationMethod[] {
return verificationMethodTypes.map((type, _) => {
switch (type) {
Expand Down Expand Up @@ -97,6 +122,11 @@ export function createDidVerificationMethod(verificationMethodTypes: Verificatio
}) ?? []
}

/**
*? General test utils. Look for src/utils.ts for stable utils exports.
*? Used for testing purposes.
** NOTE: The following utils are stable but subject to change at any given moment.
*/
export function createDidPayload(verificationMethods: VerificationMethod[], verificationKeys: IVerificationKeys[]): MsgCreateDidPayload {
if (!verificationMethods || verificationMethods.length === 0)
throw new Error('No verification methods provided')
Expand Down

0 comments on commit 4957f44

Please sign in to comment.