Skip to content

Commit

Permalink
feat: Add createDidPayloadWithSignInputs util [DEV-1587] (#49)
Browse files Browse the repository at this point in the history
* feat: Add createDidPayloadWithSignInputs util

Signed-off-by: DaevMithran <daevmithran1999@gmail.com>

* feat: Add updateDidPayloadWithSignInputs util

Signed-off-by: DaevMithran <daevmithran1999@gmail.com>

* feat:Add createSignInputsFromKeyPair util

Signed-off-by: DaevMithran <daevmithran1999@gmail.com>
Co-authored-by: Ankur Banerjee <ankurdotb@users.noreply.github.com>
  • Loading branch information
DaevMithran and ankurdotb committed Oct 26, 2022
1 parent 6193ca9 commit e332f56
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,7 @@ export {
createKeyPairHex,
createVerificationKeys,
createDidVerificationMethod,
createDidPayload
createDidPayload,
createDidPayloadWithSignInputs,
createSignInputsFromKeyPair
} from './utils'
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export interface ISignInputs {
export interface IKeyPair {
publicKey: string
privateKey: string
algo?: MethodSpecificIdAlgo
}

export interface IKeyValuePair {
Expand Down
82 changes: 73 additions & 9 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ import {
import { fromString, toString } from 'uint8arrays'
import { bases } from "multiformats/basics"
import { base64ToBytes } from "did-jwt"
import { generateKeyPair, KeyPair } from '@stablelib/ed25519'
import { generateKeyPair, generateKeyPairFromSeed, KeyPair } from '@stablelib/ed25519'
import { v4 } from 'uuid'
import { MsgCreateDidPayload } from "@cheqd/ts-proto/cheqd/v1/tx"

import { MsgCreateDidPayload, MsgUpdateDidPayload } from "@cheqd/ts-proto/cheqd/v1/tx"

export type TImportableEd25519Key = {
publicKeyHex: string
Expand All @@ -26,6 +25,8 @@ export type TImportableEd25519Key = {
type: "Ed25519"
}

export type IdentifierPayload = Partial<MsgCreateDidPayload> | Partial<MsgUpdateDidPayload>

export function parseToKeyValuePair(object: { [key: string]: any }): IKeyValuePair[] {
return Object.entries(object).map(([key, value]) => ({ key, value }))
}
Expand Down Expand Up @@ -68,20 +69,20 @@ export function createSignInputsFromImportableEd25519Key(key: TImportableEd25519
throw new Error('No verification method type provided')
}

export function createKeyPairRaw(): KeyPair {
return generateKeyPair()
export function createKeyPairRaw(seed?: string): KeyPair {
return seed ? generateKeyPairFromSeed(Buffer.from(seed)) : generateKeyPair()
}

export function createKeyPairBase64(): IKeyPair {
const keyPair = generateKeyPair()
export function createKeyPairBase64(seed?: string): IKeyPair {
const keyPair = seed ? generateKeyPairFromSeed(Buffer.from(seed)) : generateKeyPair()
return {
publicKey: toString(keyPair.publicKey, 'base64'),
privateKey: toString(keyPair.secretKey, 'base64'),
}
}

export function createKeyPairHex(): IKeyPair {
const keyPair = generateKeyPair()
export function createKeyPairHex(seed?: string): IKeyPair {
const keyPair = seed ? generateKeyPairFromSeed(Buffer.from(seed)) : generateKeyPair()
return {
publicKey: toString(keyPair.publicKey, 'hex'),
privateKey: toString(keyPair.secretKey, 'hex'),
Expand Down Expand Up @@ -158,4 +159,67 @@ export function createDidPayload(verificationMethods: VerificationMethod[], veri
authentication: verificationKeys.map(key => key.keyId)
}
)
}

export function createDidPayloadWithSignInputs(seed?: string, keys?: IKeyPair[]) {
if(seed && keys) throw new Error ('Only one of seed or keys should be passed as an argument')

if(!keys) {
keys = [seed ? createKeyPairBase64(seed) : createKeyPairBase64()]
}

const verificationMethodTypes = keys.map((key) => !key.algo || key.algo == MethodSpecificIdAlgo.Base58 ? VerificationMethods.Base58 : VerificationMethods.JWK)
const verificationKeys = keys.map((key, i) => createVerificationKeys(key, key.algo || MethodSpecificIdAlgo.Base58, `key-${i}`))
const verificationMethod = createDidVerificationMethod(verificationMethodTypes, verificationKeys)

let payload : Partial<MsgCreateDidPayload> = {
id: verificationKeys[0].didUrl,
controller: verificationKeys.map(key => key.didUrl),
verificationMethod: verificationMethod,
authentication: verificationKeys.map(key => key.keyId),
}

const keyHexs = keys.map((key)=>convertKeyPairtoTImportableEd25519Key(key))
const signInputs = keyHexs.map((key)=>createSignInputsFromImportableEd25519Key(key, verificationMethod))

return { didPayload: MsgCreateDidPayload.fromPartial(payload), keys, signInputs }
}

export function convertKeyPairtoTImportableEd25519Key(keyPair: IKeyPair) : TImportableEd25519Key {
return {
type: 'Ed25519',
privateKeyHex: toString(fromString(keyPair.privateKey, 'base64'), 'hex'),
kid: 'kid',
publicKeyHex: toString(fromString(keyPair.publicKey, 'base64'), 'hex')
}
}

export function createSignInputsFromKeyPair(didDocument: IdentifierPayload, keys: IKeyPair[]) {
const keyHexs = keys.map((key)=>convertKeyPairtoTImportableEd25519Key(key))
const signInputs = keyHexs.map((key)=>createSignInputsFromImportableEd25519Key(key, didDocument.verificationMethod!))
return signInputs
}

export enum DidDocumentOperation {
Set = 'setDidDocument',
Add = 'addToDidDocument',
Remove = 'removeFromDidDocument'
}

export function jsonConcat(o1: any, o2:any) {
for (var key in o2) {
if(Array.isArray(o1[key])) {
o1[key].push(...o2[key])
} else {
o1[key] = o2[key]
}}
return o1
}

export function jsonSubtract(o1: any, o2: any) {
for (var key in o2) {
if(o2[key] == o1[key]) {
delete(o1[key])
}
}
}

0 comments on commit e332f56

Please sign in to comment.