import { CeramicApi , CreateOpts , Stream , StreamConstructor , StreamStatic , SyncOptions , LoadOpts , UpdateOpts , RawCommit , GenesisCommit , } from '@ceramicnetwork/common' ; import type { AuthProvider , LinkProof } from '@ceramicnetwork/blockchain-utils-linking' ; import { CommitID , StreamID , StreamRef } from '@ceramicnetwork/streamid' ; import { AccountID } from 'caip' ; import type { DID } from 'dids' ; import { parse } from 'did-resolver' ; const throwReadOnlyError = ( ) : Promise < void > => { throw new Error ( 'Historical stream commits cannot be modified. Load the stream without specifying a commit to make updates.' ) ; } ; const DEFAULT_CREATE_OPTS = { anchor : false , publish : true , sync : SyncOptions . PREFER_CACHE } ; const DEFAULT_UPDATE_OPTS = { anchor : true , publish : true , throwOnInvalidCommit : true } ; const DEFAULT_LOAD_OPTS = { sync : SyncOptions . PREFER_CACHE } ; /**
 * Caip10Link stream implementation
 */ @ StreamStatic < StreamConstructor < Caip10Link > > ( ) export class Caip10Link extends Stream { static STREAM_TYPE_NAME = 'caip10-link' ; static STREAM_TYPE_ID = 1 ; private _isReadOnly = false ; /**
     * Returns the DID linked to the CAIP10 address this object represents.
     */ get did ( ) : string | null { return this . content ; } param ceramic param accountId param opts static async fromAccount ( ceramic , accountId , opts = { } ) { opts = { ... DEFAULT_CREATE_OPTS , ... opts } ; const normalizedAccountId = new AccountID ( accountId ) ; const genesisCommit = Caip10Link . makeGenesis ( normalizedAccountId ) ; return Caip10Link . fromGenesis ( ceramic , genesisCommit , opts ) ; } param ceramic param genesisCommit param opts static async fromGenesis ( ceramic , genesisCommit , opts = { } ) { opts = { ... DEFAULT_CREATE_OPTS , ... opts } ; return ceramic . createStreamFromGenesis < Caip10Link > ( Caip10Link . STREAM_TYPE_ID , genesisCommit , opts ) ; } param did param authProvider param opts async setDid ( did , authProvider , opts = { } ) { opts = { ... DEFAULT_UPDATE_OPTS , ... opts } ; const didStr = typeof did == 'string' ? did . trim ( ) : did . id ; const parsedDid = parse ( didStr ) ; if ( parsedDid ?. did !== didStr ) { throw new Error ( `DID is not valid: '${ didStr }'` ) ; } const linkProof = await authProvider . createLink ( didStr ) ; return this . setDidProof ( linkProof , opts ) ; } param proof param opts async setDidProof ( proof , opts = { } ) { opts = { ... DEFAULT_UPDATE_OPTS , ... opts } ; const commit = this . makeCommit ( proof ) ; const updated = await this . api . applyCommit ( this . id , commit , opts ) ; this . state$ . next ( updated . state ) ; } param authProvider param opts async clearDid ( authProvider , opts = { } ) { opts = { ... DEFAULT_UPDATE_OPTS , ... opts } ; const linkProof = await authProvider . createLink ( '' ) ; return this . setDidProof ( linkProof , opts ) ; } param ceramic param streamId param opts static async load ( ceramic , streamId , opts = { } ) { opts = { ... DEFAULT_LOAD_OPTS , ... opts } ; const streamRef = StreamRef . from ( streamId ) ; if ( streamRef . type != Caip10Link . STREAM_TYPE_ID ) { throw new Error ( `StreamID ${ streamRef . toString ( ) } does not refer to a '${ Caip10Link . STREAM_TYPE_NAME }' stream, but to a ${ streamRef . typeName }` ) ; } return ceramic . loadStream < Caip10Link > ( streamRef , opts ) ; } param accountId static makeGenesis ( accountId ) { if ( accountId . chainId . namespace === 'eip155' ) { accountId . address = accountId . address . toLowerCase ( ) ; } return { header : { controllers : [ accountId . toString ( ) ] , family : `caip10-${ accountId . chainId . toString ( ) }` , } , } ; } param proof makeCommit ( proof ) { return { data : proof , prev : this . tip , id : this . state$ . id . cid } ; } /**
     * Makes this stream read-only. After this has been called any future attempts to call
     * mutation methods on the instance will throw.
     */ makeReadOnly ( ) { this . setDidProof = throwReadOnlyError ; this . setDid = throwReadOnlyError ; this . sync = throwReadOnlyError ; this . _isReadOnly = true ; } get isReadOnly ( ) : boolean { return this . _isReadOnly ; } }