import { SeedAdapter , TSignData } from '@waves/signature-adapter' ; import { libs , seedUtils } from '@waves/waves-transactions' ; import { concat , identity , ifElse , isNil , pipe } from 'ramda' ; export function networkByteFromAddress ( address ) { return String . fromCharCode ( libs . crypto . base58Decode ( address ) [ 1 ] ) ; } export function addressFromPublicKey ( pk , byte ) { return new seedUtils . Seed ( pk , byte ) . address ; } export function encrypt ( object , password ) { const jsonObj = JSON . stringify ( object ) ; return seedUtils . encryptSeed ( jsonObj , password ) ; } export function decrypt < T > ( encryptedText , password ) { try { const decryptedJson = seedUtils . decryptSeed ( encryptedText , password ) ; return JSON . parse ( decryptedJson ) ; } catch ( e ) { throw new Error ( 'Invalid password' ) ; } } export function getInitedAdapter ( user , seed = 'validation seed' ) { SeedAdapter . initOptions ( { networkCode : user . networkCode . charCodeAt ( 0 ) } ) ; return new SeedAdapter ( 'validation seed' , user . networkCode ) ; } export async function getTxId ( adapter , tx ) { return await adapter . makeSignable ( tx ) . getId ( ) ; } export async function getNetworkByte ( url ) { const response = await getUrl ( url , 'blocks/last' , true ) ; const { generator } = await response . json ( ) ; if ( ! generator ) { throw new Error ( 'Incorrect node url' ) ; } const networkCode = networkByteFromAddress ( generator ) ; if ( ! networkCode ) { throw new Error ( 'Incorrect node byte' ) ; } return networkCode ; } export async function getMatcherPublicKey ( url ) { const response = await getUrl ( url , '/matcher' ) ; const pk = await response . json ( ) ; const publicKeyBytes = libs . crypto . base58Decode ( pk ) ; if ( publicKeyBytes . length === 32 ) { return pk ; } throw new Error ( 'Invalid matcher public key' ) ; } function blake2b ( input ) { return libs . crypto . blake2b ( input ) ; } function keccak ( input ) { return libs . crypto . keccak ( input ) ; } function hashChain ( input ) { return keccak ( blake2b ( input ) ) ; } async function getUrl ( urlString , path = '' , required = true ) { let url ; if ( required && ! urlString ) { return Promise . reject ( ) ; } try { url = new URL ( urlString ) ; } catch ( e ) { return Promise . reject ( ) ; } url . pathname = path || url . pathname ; return fetch ( url . href ) ; } export const validateNode = async ( url , networkCode ) => { const code = await getNetworkByte ( url ) ; if ( networkCode && code !== networkCode ) { throw new Error ( 'Invalid code' ) ; } return code ; } ; export const byteArrayToString = function ( bytes ) : string { const extraByteMap = [ 1 , 1 , 1 , 1 , 2 , 2 , 3 , 0 ] ; const count = bytes . length ; let str = '' ; for ( let index = 0 ; index < count ; ) { let ch = bytes [ index ++ ] ; if ( ch & 0x80 ) { let extra = extraByteMap [ ( ch >> 3 ) & 0x07 ] ; if ( ! ( ch & 0x40 ) || ! extra || index + extra > count ) return null ; ch = ch & ( 0x3f >> extra ) ; for ( ; extra > 0 ; extra -= 1 ) { const chx = bytes [ index ++ ] ; if ( ( chx & 0xc0 ) != 0x80 ) return null ; ch = ( ch << 6 ) | ( chx & 0x3f ) ; } } str += String . fromCharCode ( ch ) ; } return str ; } ; const bytesToBase58 = libs . crypto . base58Encode ; const bytesToString = byteArrayToString ; export const bytesToSafeString = ifElse ( pipe ( identity , bytesToString , isNil ) , pipe ( identity , bytesToBase58 , concat ( 'base58:' ) ) , pipe ( identity , bytesToString ) ) ; export const readAttachment = data => { if ( ! data ) { return '' ; } if ( typeof data === 'string' ) { return data ; } return bytesToSafeString ( data ) ; } ;