import { blake128 as _blake128 , blake128WithKey as _blake128WithKey , blake160 as _blake160 , blake160WithKey as _blake160WithKey , blake256 as _blake256 , blake256WithKey as _blake256WithKey , generatePrivateKey as _generatePrivateKey , getAccountIdFromPrivate as _getAccountIdFromPrivate , getAccountIdFromPublic as _getAccountIdFromPublic , getPublicFromPrivate as _getPublicFromPrivate , recoverEcdsa as _recoverEcdsa , ripemd160 as _ripemd160 , signEcdsa as _signEcdsa , toHex as _toHex , verifyEcdsa as _verifyEcdsa } from "codechain-primitives" ; import * as _ from "lodash" ; param buffer returns export const toHex = ( buffer ) : string => _toHex ( buffer ) ; param data returns export const blake256 = ( data ) : string => _blake256 ( data ) ; param data returns export const blake160 = ( data ) : string => _blake160 ( data ) ; param data returns export const blake128 = ( data ) : string => _blake128 ( data ) ; param data param key returns export const blake256WithKey = ( data , key ) : string => _blake256WithKey ( data , key ) ; param data param key returns export const blake160WithKey = ( data , key ) : string => _blake160WithKey ( data , key ) ; param data param key returns export const blake128WithKey = ( data , key ) : string => _blake128WithKey ( data , key ) ; param data returns export const ripemd160 = ( data ) : string => _ripemd160 ( data ) ; export interface SignatureTag { input : "all" | "single" ; output : "all" | number [ ] ; } hidden export const encodeSignatureTag = ( tag ) : Buffer => { const { input , output } = tag ; if ( input !== "all" && input !== "single" ) { throw Error ( `Expected the input of the tag to be either "all" or "single" but found ${ input }` ) ; } const inputMask = input === "all" ? 0b01 : 0b00 ; const outputMask = output === "all" ? 0b10 : 0b00 ; if ( Array . isArray ( output ) ) { const encoded = encodeSignatureTagOutput ( Array . from ( new Set ( output ) ) . sort ( ( a , b ) => a - b ) ) ; if ( encoded . length >= 64 ) { throw Error ( `The output length is too big` ) ; } return Buffer . from ( [ ... encoded , ( encoded . length << 2 ) | outputMask | inputMask ] ) ; } else if ( output === "all" ) { return Buffer . from ( [ outputMask | inputMask ] ) ; } else { throw Error ( `Expected the output of the tag to be either string "all" or an array of number but found ${ output }` ) ; } } ; hidden const encodeSignatureTagOutput = ( output ) => { if ( output [ 0 ] < 0 ) { throw Error ( `Invalid signature tag. Out of range: ${ output [ 0 ] }` ) ; } else if ( output [ output . length - 1 ] > 503 ) { throw Error ( `Invalid signature tag. Out of range: ${ output [ output . length - 1 ] }` ) ; } let offset = 0 ; let byte = 0 ; const bytes = [ ] ; for ( const index of output ) { if ( typeof index !== "number" ) { throw Error ( `Expected an array of number but found ${ index } at ${ output . indexOf ( index ) }` ) ; } if ( index < offset + 8 ) { byte |= 1 << ( index - offset ) ; } else { bytes . push ( byte ) ; offset += 8 ; while ( index >= offset + 8 ) { bytes . push ( 0 ) ; offset += 8 ; } byte = 1 << ( index - offset ) ; } } if ( byte !== 0 ) { bytes . push ( byte ) ; } return bytes . reverse ( ) ; } ; export type EcdsaSignature = string ; param message param priv returns export const signEcdsa = ( message , priv ) : EcdsaSignature => { return _signEcdsa ( message , priv ) ; } ; param message param signature param pub returns export const verifyEcdsa = ( message , signature , pub ) : boolean => { if ( signature . startsWith ( "0x" ) ) { signature = signature . substr ( 2 ) ; } return _verifyEcdsa ( message , signature , pub ) ; } ; param message param signature returns export const recoverEcdsa = ( message , signature ) : string => { if ( signature . startsWith ( "0x" ) ) { signature = signature . substr ( 2 ) ; } return _recoverEcdsa ( message , signature ) ; } ; returns export const generatePrivateKey = ( ) : string => _generatePrivateKey ( ) ; param priv returns export const getAccountIdFromPrivate = ( priv ) : string => _getAccountIdFromPrivate ( priv ) ; param publicKey returns export const getAccountIdFromPublic = ( publicKey ) : string => _getAccountIdFromPublic ( publicKey ) ; param priv returns export const getPublicFromPrivate = ( priv ) : string => _getPublicFromPrivate ( priv ) ;