import { ICardCrypto } from '../types' ; import { IRawSignature , RawSignedModel } from './RawSignedModel' ; import { ICard , ICardSignature , IExtraData , INewCardParams , IRawCardContent } from './ICard' ; import { getUnixTimestamp } from '../Lib/timestamp' ; import { CardIdByteLength , CardVersion } from './constants' ; /**
 * Converts an  ICard to a  RawSignedModel .
 *
 *  hidden param { ICard } card - The  ICard to convert.
 *  returns { RawSignedModel } export function cardToRawSignedModel ( card ) { return new RawSignedModel ( card . contentSnapshot , card . signatures . slice ( ) ) ; } /**
 * Generates a  RawSignedModel from the given `params`.
 *
 *  hidden param { ICardCrypto } crypto - Object implementing the  ICardCrypto * interface.
 *  param { INewCardParams } params returns { RawSignedModel } export function generateRawSigned ( crypto , params ) { const { identity , publicKey , previousCardId } = params ; const now = getUnixTimestamp ( new Date ) ; const details = { identity : identity ! , previous_card_id : previousCardId , created_at : now , version : CardVersion , public_key : crypto . exportPublicKey ( publicKey ) . toString ( 'base64' ) , } ; return new RawSignedModel ( JSON . stringify ( details ) , [ ] ) ; } /**
 * Converts the  RawSignedModel into the  ICard .
 *
 *  hidden param { ICardCrypto } crypto - Object implementing the  ICardCrypto * interface.
 *  param { RawSignedModel } model param { boolean } isOutdated returns { ICard } export function parseRawSignedModel ( crypto , model , isOutdated = false ) { const content = JSON . parse ( model . contentSnapshot ) as IRawCardContent ; const signatures = model . signatures . map ( rawSignToCardSign ) ; return { id : generateCardId ( crypto , model . contentSnapshot ) , publicKey : crypto . importPublicKey ( { value : content . public_key , encoding : 'base64' } ) , contentSnapshot : model . contentSnapshot , identity : content . identity , version : content . version , createdAt : new Date ( content . created_at * 1000 ) , previousCardId : content . previous_card_id , signatures , isOutdated } ; } hidden param { ICard [ ] } cards returns { ICard [ ] } export function linkedCardList ( cards ) { const unsorted = Object . create ( null ) ; for ( const card of cards ) { unsorted [ card . id ] = card ; } for ( const card of cards ) { if ( card . previousCardId == null ) continue ; if ( unsorted [ card . previousCardId ] == null ) continue ; unsorted [ card . previousCardId ] . isOutdated = true ; card . previousCard = unsorted [ card . previousCardId ] ; delete unsorted [ card . previousCardId ] ; } return Object . keys ( unsorted ) . map ( key => unsorted [ key ] ) ; } hidden param { ICardCrypto } crypto - Object implementing the  ICardCrypto * interface.
 *  param { string } snapshot returns { string } function generateCardId ( crypto , snapshot ) { const fingerprint = crypto . generateSha512 ( { value : snapshot , encoding : 'utf8' } ) . slice ( 0 , CardIdByteLength ) ; return fingerprint . toString ( 'hex' ) ; } function rawSignToCardSign ( { snapshot , signature , signer } ) { return { signer , signature , snapshot , extraFields : tryParseExtraFields ( snapshot ) } ; } function tryParseExtraFields ( snapshot ? ) { if ( snapshot ) { try { return JSON . parse ( snapshot ) as IExtraData ; } catch ( ignored ) { } } return { } ; }