import { coin as COIN , setting } from "@coolwallet/core" ; import * as sign from "./sign" ; import { pubKeyToAddress } from "./utils/ethUtils" ; import * as types from "./config/types" ; import * as scriptUtils from "./utils/scriptUtils" ; import * as params from "./config/params" ; import { TOKENTYPE } from "./config/tokenType" ; export { TOKENTYPE } ; export default class BSC extends COIN . ECDSACoin implements COIN . Coin { constructor ( ) { super ( params . COIN_TYPE ) ; } param { number } addressIndex return { string } async getAddress ( transport , appPrivateKey , appId , addressIndex ) { const publicKey = await this . getPublicKey ( transport , appPrivateKey , appId , addressIndex ) ; return pubKeyToAddress ( publicKey ) ; } async getAddressByAccountKey ( accPublicKey , accChainCode , addressIndex ) { const publicKey = await this . getAddressPublicKey ( accPublicKey , accChainCode , addressIndex ) ; return pubKeyToAddress ( publicKey ) ; } param { { nonce : string , gasPrice : string , gasLimit : string , to : string , value : string , data : string } } transaction param { Number } addressIndex param { String } publicKey param { Function } confirmCB param { Function } authorizedCB async signTransaction ( signTxData ) { const { value , data , to } = signTxData . transaction ; if ( value && ! data ) { return await this . signTransferTransaction ( signTxData ) ; } const functionHash = data . startsWith ( "0x" ) ? data . slice ( 2 , 10 ) : data . slice ( 0 , 8 ) ; if ( data && functionHash === "a9059cbb" ) { const upperCaseAddress = to . toUpperCase ( ) ; let tokenSignature ; for ( let tokenInfo of TOKENTYPE ) { if ( tokenInfo . contractAddress . toUpperCase ( ) === upperCaseAddress ) { tokenSignature = tokenInfo . signature ; signTxData . transaction . option . info . symbol = tokenInfo . symbol ; signTxData . transaction . option . info . decimals = tokenInfo . unit ; break ; } } const { symbol , decimals } = signTxData . transaction . option . info ; if ( symbol && decimals ) { if ( tokenSignature ) { return await this . signBEP20Transaction ( signTxData , tokenSignature ) ; } else { return await this . signBEP20Transaction ( signTxData ) ; } } } return await this . signSmartContractTransaction ( signTxData ) ; } param { { nonce : string , gasPrice : string , gasLimit : string , to : string , value : string , data : string } } transaction param { Number } addressIndex param { String } publicKey param { Function } confirmCB param { Function } authorizedCB async signTransferTransaction ( signTxData ) { const publicKey = await this . getPublicKey ( signTxData . transport , signTxData . appPrivateKey , signTxData . appId , signTxData . addressIndex ) ; const argument = await scriptUtils . getTransferArgument ( signTxData . transaction , signTxData . addressIndex ) ; const script = params . TRANSFER . script + params . TRANSFER . signature ; return sign . signTransaction ( signTxData , script , argument , publicKey ) ; } param { { nonce : string , gasPrice : string , gasLimit : string , to : string , value : string , data : string } } transaction param { Number } addressIndex param { String } publicKey param { Function } confirmCB param { Function } authorizedCB async signBEP20Transaction ( signTxData , tokenSignature = "" ) { const publicKey = await this . getPublicKey ( signTxData . transport , signTxData . appPrivateKey , signTxData . appId , signTxData . addressIndex ) ; const script = params . BEP20 . script + params . BEP20 . signature ; const argument = await scriptUtils . getBEP20Argument ( signTxData . transaction , tokenSignature , signTxData . addressIndex ) ; return sign . signTransaction ( signTxData , script , argument , publicKey ) ; } param { { nonce : string , gasPrice : string , gasLimit : string , to : string , value : string , data : string } } transaction param { Number } addressIndex param { String } publicKey param { Function } confirmCB param { Function } authorizedCB async signSmartContractTransaction ( signTxData ) { console . debug ( "signSmartContractTransaction" ) ; const publicKey = await this . getPublicKey ( signTxData . transport , signTxData . appPrivateKey , signTxData . appId , signTxData . addressIndex ) ; const script = params . BSCSmartContract . script + params . BSCSmartContract . signature ; const argument = await scriptUtils . getSmartContractArgument ( signTxData . transaction , signTxData . addressIndex ) ; return sign . signTransaction ( signTxData , script , argument , publicKey ) ; } param { String } message param { Number } addressIndex param { String } publicKey param { Boolean } isHashRequired param { Function } confirmCB param { Function } authorizedCB return { Promise < String > } async signMessage ( signMsgData ) { await setting . auth . versionCheck ( signMsgData . transport , 81 ) ; const publicKey = await this . getPublicKey ( signMsgData . transport , signMsgData . appPrivateKey , signMsgData . appId , signMsgData . addressIndex ) ; const script = params . SIGN_MESSAGE . script + params . SIGN_MESSAGE . signature ; const argument = await scriptUtils . getSignMessageArgument ( signMsgData . message , signMsgData . addressIndex ) ; return sign . signMessage ( signMsgData , script , argument , publicKey ) ; } param { Object } typedData param { Number } addressIndex param { String } publicKey param { Function } confirmCB param { Function } authorizedCB async signTypedData ( typedData ) { await setting . auth . versionCheck ( typedData . transport , 84 ) ; const publicKey = await this . getPublicKey ( typedData . transport , typedData . appPrivateKey , typedData . appId , typedData . addressIndex ) ; const script = params . SIGN_TYPED_DATA . script + params . SIGN_TYPED_DATA . signature ; return sign . signTypedData ( typedData , script , publicKey ) ; } }