import { makeErrorWithCode } from '@neo-one/utils' ; import BigNumber from 'bignumber.js' ; import { BN } from 'bn.js' ; import { Wildcard } from './models' ; export const InvalidFormatError = makeErrorWithCode ( 'INVALID_FORMAT' , ( reason ? ) => `Invalid format${ reason === undefined ? '.' : `: ${ reason }` }` ) ; const add0x = ( value ) => ( value . startsWith ( '0x' ) ? value : `0x${ value }` ) ; const strip0x = ( value ) => ( value . startsWith ( '0x' ) ? value . substring ( 2 ) : value ) ; const reverse = ( src ) : Buffer => { const mutableOut = Buffer . allocUnsafe ( src . length ) ; for ( let i = 0 , j = src . length - 1 ; i <= j ; i += 1 , j -= 1 ) { mutableOut [ i ] = src [ j ] ; mutableOut [ j ] = src [ i ] ; } return mutableOut ; } ; export type UInt160 = Buffer & { readonly __uint160 : undefined ; } ; export type UInt160Hex = string ; const UINT160_BUFFER_BYTES = 20 ; const isUInt160 = ( value ) : value is UInt160 => value instanceof Buffer && value . length === UINT160_BUFFER_BYTES ; const stringIsValidUInt160 = ( value ) : boolean => { try { stringToUInt160 ( value ) ; } catch { return false ; } return true ; } ; const asUInt160 = ( value ) : UInt160 => { if ( isUInt160 ( value ) ) { return value ; } throw new InvalidFormatError ( 'Invalid UInt160' ) ; } ; const bufferToUInt160 = asUInt160 ; const uInt160ToHex = ( value ) : UInt160Hex => add0x ( reverse ( value ) . toString ( 'hex' ) ) ; const hexToUInt160 = ( value ) : UInt160 => typeof value === 'string' ? asUInt160 ( reverse ( Buffer . from ( strip0x ( value ) , 'hex' ) ) ) : asUInt160 ( value ) ; const uInt160ToBuffer = ( value ) : Buffer => typeof value === 'string' ? hexToUInt160 ( value ) : value ; const uInt160ToString = ( value ) : string => typeof value === 'string' ? value : uInt160ToHex ( value ) ; const stringToUInt160 = hexToUInt160 ; const uInt160Equal = ( a , b ) => a . equals ( b ) ; const ZERO_UINT160 = Buffer . alloc ( 20 , 0 ) as UInt160 ; export type UInt256 = Buffer & { readonly __uint256 : undefined ; } ; export type UInt256Hex = string ; const UINT256_BUFFER_BYTES = 32 ; const ZERO_UINT256 = Buffer . alloc ( UINT256_BUFFER_BYTES , 0 ) as UInt256 ; const MAX_UINT256 = Buffer . alloc ( UINT256_BUFFER_BYTES , 0xff ) as UInt256 ; const isUInt256 = ( value ) : value is UInt256 => value instanceof Buffer && value . length === UINT256_BUFFER_BYTES ; const stringIsValidUInt256 = ( value ) : boolean => { try { stringToUInt256 ( value ) ; } catch { return false ; } return true ; } ; const asUInt256 = ( value ) : UInt256 => { if ( isUInt256 ( value ) ) { return value ; } throw new InvalidFormatError ( 'Invalid UInt256' ) ; } ; const bufferToUInt256 = asUInt256 ; const uInt256ToHex = ( value ) : UInt256Hex => add0x ( reverse ( value ) . toString ( 'hex' ) ) ; const hexToUInt256 = ( value ) : UInt256 => typeof value === 'string' ? bufferToUInt256 ( reverse ( Buffer . from ( strip0x ( value ) , 'hex' ) ) ) : bufferToUInt256 ( value ) ; const uInt256ToBuffer = ( value ) : Buffer => typeof value === 'string' ? hexToUInt256 ( value ) : value ; const uInt256ToString = ( value ) : string => typeof value === 'string' ? value : uInt256ToHex ( value ) ; const stringToUInt256 = hexToUInt256 ; const uInt256Equal = ( a , b ) => a . equals ( b ) ; const toUInt32LE = ( bytes ) : number => new BN ( uInt256ToBuffer ( bytes ) . slice ( 0 , 4 ) , 'le' ) . toNumber ( ) ; export type ECPointBase = Buffer & { readonly __ecpoint : undefined ; } ; export type ECPointInfinity = Buffer & { readonly __ecpoint : undefined ; } ; export type ECPoint = ECPointBase | ECPointInfinity ; export type ECPointHex = string ; const ECPOINT_BUFFER_BYTES = 33 ; const ECPOINT_INFINITY_BYTE = 0x00 ; const ECPOINT_INFINITY = Buffer . from ( [ ECPOINT_INFINITY_BYTE ] ) as ECPointInfinity ; const isECPoint = ( value ) : value is ECPoint => value instanceof Buffer && ( value . length === ECPOINT_BUFFER_BYTES || value . equals ( ECPOINT_INFINITY ) ) ; const stringIsValidECPoint = ( value ) : boolean => { try { stringToECPoint ( value ) ; } catch { return false ; } return true ; } ; const asECPoint = ( value ) : ECPoint => { if ( isECPoint ( value ) ) { return value ; } throw new InvalidFormatError ( 'Invalid ECPoint' ) ; } ; const bufferToECPoint = asECPoint ; const ecPointToBase64 = ( value ) : ECPointHex => typeof value === 'string' ? value : value . toString ( 'base64' ) ; const base64ToECPoint = ( value ) : ECPoint => bufferToECPoint ( typeof value === 'string' ? Buffer . from ( value , 'base64' ) : value ) ; const ecPointToHex = ( value ) : ECPointHex => typeof value === 'string' ? value : value . toString ( 'hex' ) ; const hexToECPoint = ( value ) : ECPoint => bufferToECPoint ( typeof value === 'string' ? Buffer . from ( value , 'hex' ) : value ) ; const ecPointToBuffer = ( value ) : Buffer => typeof value === 'string' ? hexToECPoint ( value ) : value ; const ecPointToString = ( value ) : string => typeof value === 'string' ? value : ecPointToHex ( value ) ; const stringToECPoint = hexToECPoint ; const ecPointEqual = ( a , b ) : boolean => a . equals ( b ) ; const ecPointCompare = ( a , b ) => { const aHex = ecPointToHex ( a ) ; const bHex = ecPointToHex ( b ) ; if ( aHex < bHex ) { return - 1 ; } if ( aHex > bHex ) { return 1 ; } return 0 ; } ; const ecPointIsInfinity = ( value ) : boolean => value . equals ( ECPOINT_INFINITY ) ; export type PrivateKey = Buffer & { readonly __privatekey : undefined ; } ; export type PrivateKeyHex = string ; const PRIVATE_KEY_BUFFER_BYTES = 32 ; const isPrivateKey = ( value ) : value is PrivateKey => value instanceof Buffer && value . length === PRIVATE_KEY_BUFFER_BYTES ; const asPrivateKey = ( value ) : PrivateKey => { if ( isPrivateKey ( value ) ) { return value ; } throw new InvalidFormatError ( `Invalid Private Key, found: ${ value }` ) ; } ; const bufferToPrivateKey = asPrivateKey ; const privateKeyToHex = ( value ) : PrivateKeyHex => typeof value === 'string' ? value : value . toString ( 'hex' ) ; const hexToPrivateKey = ( value ) : PrivateKey => bufferToPrivateKey ( typeof value === 'string' ? Buffer . from ( strip0x ( value ) , 'hex' ) : value ) ; const privateKeyToBuffer = ( value ) : Buffer => typeof value === 'string' ? hexToPrivateKey ( value ) : value ; const privateKeyToString = ( value ) : string => typeof value === 'string' ? value : privateKeyToHex ( value ) ; const stringToPrivateKey = hexToPrivateKey ; const fixedFromDecimal = ( value , decimals ) => { if ( value instanceof BN ) { return value ; } const d = new BN ( 10 ** decimals ) ; if ( typeof value === 'number' ) { return new BN ( value ) . mul ( d ) ; } const valueBigNumber = typeof value === 'string' ? new BigNumber ( value ) : value ; const dBigNumber = new BigNumber ( d . toString ( 10 ) ) ; return new BN ( valueBigNumber . times ( dBigNumber ) . toString ( ) , 10 ) ; } ; const D8 = new BN ( 100000000 ) ; const fixed8FromDecimal = ( value ) : BN => fixedFromDecimal ( value , 8 ) ; const fixedToDecimal = ( value , decimals ) : BigNumber => { const dBigNumber = new BigNumber ( 10 ** decimals ) ; return new BigNumber ( value . toString ( 10 ) ) . div ( dBigNumber ) ; } ; const fixed8ToDecimal = ( bn ) : BigNumber => fixedToDecimal ( bn , 8 ) ; const isWildcard = ( input ) : input is Wildcard => input === '*' ; const NEGATIVE_SATOSHI_FIXED8 = new BN ( - 1 ) ; const TEN_FIXED8 = fixed8FromDecimal ( 10 ) ; const TWENTY_FIXED8 = fixed8FromDecimal ( 20 ) ; const ONE_HUNDRED_FIXED8 = fixed8FromDecimal ( 100 ) ; const FOUR_HUNDRED_FIXED8 = fixed8FromDecimal ( 400 ) ; const FIVE_HUNDRED_FIXED8 = fixed8FromDecimal ( 500 ) ; const ONE_THOUSAND_FIXED8 = fixed8FromDecimal ( 1000 ) ; const FIVE_THOUSAND_FIXED8 = fixed8FromDecimal ( 5000 ) ; const TEN_THOUSAND_FIXED8 = fixed8FromDecimal ( 10000 ) ; const ONE_HUNDRED_MILLION_FIXED8 = fixed8FromDecimal ( 100000000 ) ; const nativeScriptHashes = { ContractManagement : '0xfffdc93764dbaddd97c48f252a53ea4643faa3fd' , StdLib : '0xacce6fd80d44e1796aa0c2c625e9e4e0ce39efc0' , CryptoLib : '0x726cb6e0cd8628a1350a611384688911ab75f51b' , Ledger : '0xda65b600f7124ce6c79950c1772a36403104f2be' , NEO : '0xef4073a0f2b305a38ec4050e4d3d28bc40ea63f5' , GAS : '0xd2a4cff31913016155e38e474a2c06d08be276cf' , Policy : '0xcc5e4edd9f5f8dba8bb65734541df7a1c081c67b' , RoleManagement : '0x49cf4e5378ffcd4dec034fd98a174c5491e395e2' , Oracle : '0xfe924b7cfe89ddd271abaf7210a80a7e11178758' } ; const nativeHashes = { ContractManagement : hexToUInt160 ( nativeScriptHashes . ContractManagement ) , StdLib : hexToUInt160 ( nativeScriptHashes . StdLib ) , CryptoLib : hexToUInt160 ( nativeScriptHashes . CryptoLib ) , Ledger : hexToUInt160 ( nativeScriptHashes . Ledger ) , NEO : hexToUInt160 ( nativeScriptHashes . NEO ) , GAS : hexToUInt160 ( nativeScriptHashes . GAS ) , Policy : hexToUInt160 ( nativeScriptHashes . Policy ) , RoleManagement : hexToUInt160 ( nativeScriptHashes . RoleManagement ) , Oracle : hexToUInt160 ( nativeScriptHashes . Oracle ) } ; const MAP_STACK_ITEM_MAX_KEY_SIZE = 64 ; export const common = { D8 , NEO_ADDRESS_VERSION : 0x35 , NEO_PRIVATE_KEY_VERSION : 0x80 , GROUPING_SIZE_BYTES : 16 , MAX_CONTRACT_STRING : 252 , MAX_MANIFEST_LENGTH : 4096 , ECPOINT_BUFFER_BYTES , ECPOINT_INFINITY , ECPOINT_INFINITY_BYTE , PRIVATE_KEY_BUFFER_BYTES , UINT160_BUFFER_BYTES , UINT256_BUFFER_BYTES , ZERO_UINT160 , ZERO_UINT256 , MAX_UINT256 , NEGATIVE_SATOSHI_FIXED8 , TEN_FIXED8 , TWENTY_FIXED8 , ONE_HUNDRED_FIXED8 , FOUR_HUNDRED_FIXED8 , FIVE_HUNDRED_FIXED8 , ONE_THOUSAND_FIXED8 , FIVE_THOUSAND_FIXED8 , TEN_THOUSAND_FIXED8 , ONE_HUNDRED_MILLION_FIXED8 , MAP_STACK_ITEM_MAX_KEY_SIZE , BYTE_MAX_VALUE : 255 , uInt160ToBuffer , add0x , strip0x , asUInt160 , uInt160ToHex , hexToUInt160 , uInt160ToString , stringToUInt160 , uInt160Equal , isUInt160 , stringIsValidUInt160 , bufferToUInt160 , isUInt256 , stringIsValidUInt256 , asUInt256 , isECPoint , stringIsValidECPoint , asECPoint , uInt256ToBuffer , bufferToUInt256 , uInt256ToHex , hexToUInt256 , uInt256ToString , stringToUInt256 , uInt256Equal , toUInt32LE , ecPointToBuffer , bufferToECPoint , ecPointToHex , hexToECPoint , base64ToECPoint , ecPointToString , ecPointToBase64 , ecPointCompare , stringToECPoint , ecPointEqual , ecPointIsInfinity , privateKeyToHex , hexToPrivateKey , privateKeyToBuffer , bufferToPrivateKey , privateKeyToString , stringToPrivateKey , isPrivateKey , asPrivateKey , fixed8FromDecimal , fixed8ToDecimal , fixedFromDecimal , fixedToDecimal , reverse , isWildcard , nativeScriptHashes , nativeHashes } ;