import { Keypair , PublicKey } from '@solana/web3.js' ; import { addKey as addKeyToDID } from '../../lib/did/addKey' ; import { AddKeyRequest , currentCluster , debug , isKeypair , pubkeyOf , PublicKeyBase58 , toSolanaKeyMaterial , } from '../../lib/util' ; import { DIDDocument } from 'did-resolver' ; import { keyToIdentifier } from '@identity.com/sol-did-client' ; import { updateCEKAccount } from '../../service/updateCEKAccount' ; const didFromKey = ( request ) : Promise < string > => { if ( request . signer ) return keyToIdentifier ( new PublicKey ( request . signer ) , currentCluster ( request . cluster ) ) ; return keyToIdentifier ( pubkeyOf ( toSolanaKeyMaterial ( request . payer ) ) , currentCluster ( request . cluster ) ) ; } ; const isEmpty = < T > ( array ) : boolean => ! array || array . length === 0 ; param request export const addKey = async ( request ) : Promise < DIDDocument > => { const did = request . ownerDID || ( await didFromKey ( request ) ) ; const payer = toSolanaKeyMaterial ( request . payer ) ; const signer = toSolanaKeyMaterial ( request . signer ) ; const payerOrUndefined = isKeypair ( payer ) ? payer : undefined ; if ( ! isEmpty ( request . channelsToUpdate ) && ! isKeypair ( signer ) ) { throw new Error ( 'A decryption key is required when updating channels' ) ; } const newKey = new PublicKey ( request . newKey ) ; const didDocument = await addKeyToDID ( did , request . keyIdentifier , newKey , signer , payerOrUndefined , request . signCallback , request . cluster ) ; const updateChannel = ( channelAddress ) : Promise < void > => updateCEKAccount ( did , signer as Keypair , payer , new PublicKey ( channelAddress ) , newKey , request . signCallback , request . cluster ) ; const channelUpdateResults = await Promise . allSettled ( request . channelsToUpdate . map ( updateChannel ) ) ; debug ( channelUpdateResults ) ; return didDocument ; } ;