import { CallFlags , common , crypto , privateKeyToAddress , privateKeyToScriptHash , ScriptBuilder , scriptHashToAddress , SignerModel , TransactionModel , WitnessModel , WitnessScopeModel , } from '@neo-one/client-common' ; import { constants } from '@neo-one/utils' ; import { BN } from 'bn.js' ; import { LocalKeyStore , LocalMemoryStore } from '../../user' ; describe ( 'RPC Call sendrawtransaction' , ( ) => { const keystore = new LocalKeyStore ( new LocalMemoryStore ( ) ) ; const network = 1951352142 ; const maxValidUntilBlockIncrement = 86400000 / 15000 ; test ( 'Create a valid private net transaction with one signer' , async ( ) => { await keystore . addUserAccount ( { network : constants . LOCAL_NETWORK_NAME , privateKey : constants . PRIVATE_NET_PRIVATE_KEY , name : 'master' } ) ; const signer = new SignerModel ( { account : common . stringToUInt160 ( privateKeyToScriptHash ( constants . PRIVATE_NET_PRIVATE_KEY ) ) , scopes : WitnessScopeModel . None } ) ; const txUnsigned = new TransactionModel ( { script : new ScriptBuilder ( ) . emitOp ( 'PUSHNULL' ) . build ( ) , systemFee : new BN ( 0 ) , networkFee : new BN ( 1590000 ) , validUntilBlock : 800000 , signers : [ signer ] , network , maxValidUntilBlockIncrement } ) ; const signature = await keystore . sign ( { account : { network : 'local' , address : privateKeyToAddress ( constants . PRIVATE_NET_PRIVATE_KEY ) } , message : txUnsigned . message . toString ( 'hex' ) } ) ; const witness = crypto . createWitnessForSignature ( Buffer . from ( signature , 'hex' ) , common . stringToECPoint ( constants . PRIVATE_NET_PUBLIC_KEY ) , WitnessModel ) ; const finalTransaction = txUnsigned . clone ( { witnesses : [ witness ] } ) ; console . log ( finalTransaction . serializeWire ( ) . toString ( 'hex' ) ) ; } ) ; test ( 'Create a valid private net transaction with multiple signers' , async ( ) => { await keystore . addUserAccount ( { network : constants . LOCAL_NETWORK_NAME , privateKey : constants . PRIVATE_NET_PRIVATE_KEY , name : 'master' } ) ; const standbyValidators = [ '0248be3c070df745a60f3b8b494efcc6caf90244d803a9a72fe95d9bae2120ec70' ] . map ( ( val ) => common . stringToECPoint ( val ) ) ; const address = crypto . toScriptHash ( crypto . createMultiSignatureVerificationScript ( standbyValidators . length / 2 + 1 , standbyValidators ) ) ; const signer = new SignerModel ( { account : address , scopes : WitnessScopeModel . None } ) ; const txUnsigned = new TransactionModel ( { script : new ScriptBuilder ( ) . emitOp ( 'PUSHNULL' ) . build ( ) , systemFee : new BN ( 0 ) , networkFee : new BN ( 1590000 ) , validUntilBlock : 800000 , signers : [ signer ] , network , maxValidUntilBlockIncrement } ) ; const signature = await keystore . sign ( { account : { network : 'local' , address : privateKeyToAddress ( constants . PRIVATE_NET_PRIVATE_KEY ) } , message : txUnsigned . message . toString ( 'hex' ) } ) ; const witness = crypto . createMultiSignatureWitness ( standbyValidators . length / 2 + 1 , standbyValidators , { [ `${ common . ecPointToHex ( standbyValidators [ 0 ] ) }` ] : Buffer . from ( signature , 'hex' ) } , WitnessModel ) ; const finalTransaction = txUnsigned . clone ( { witnesses : [ witness ] } ) ; console . log ( finalTransaction . serializeWire ( ) . toString ( 'hex' ) ) ; } ) ; test ( 'Get the private net initialization address from standby validators' , async ( ) => { const standbyValidators = [ '0248be3c070df745a60f3b8b494efcc6caf90244d803a9a72fe95d9bae2120ec70' ] . map ( ( val ) => common . stringToECPoint ( val ) ) ; const address = crypto . toScriptHash ( crypto . createMultiSignatureVerificationScript ( standbyValidators . length / 2 + 1 , standbyValidators ) ) ; console . log ( common . uInt160ToString ( address ) ) ; console . log ( scriptHashToAddress ( common . uInt160ToString ( address ) ) ) ; } ) ; test . skip ( 'transaction creation for consensus testing' , async ( ) => { const config = { blockchain : { standbyValidators : [ '0248be3c070df745a60f3b8b494efcc6caf90244d803a9a72fe95d9bae2120ec70' ] . map ( ( p ) => common . stringToECPoint ( p ) ) , addressVersion : common . NEO_ADDRESS_VERSION , network : 7630401 , maxValidUntilBlockIncrement } } ; const privateKeyString = 'e35fa5d1652c4c65e296c86e63a3da6939bc471b741845be636e2daa320dc770' ; const privateKey = common . stringToPrivateKey ( privateKeyString ) ; const publicKey = crypto . privateKeyToPublicKey ( privateKey ) ; const publicKeyString = common . ecPointToHex ( publicKey ) ; const multiScriptHash = crypto . toScriptHash ( crypto . createMultiSignatureVerificationScript ( 1 , config . blockchain . standbyValidators ) ) ; const multiAddress = crypto . scriptHashToAddress ( { addressVersion : config . blockchain . addressVersion , scriptHash : multiScriptHash } ) ; const scriptHash = crypto . publicKeyToScriptHash ( publicKey ) ; const address = crypto . scriptHashToAddress ( { addressVersion : config . blockchain . addressVersion , scriptHash } ) ; await keystore . addUserAccount ( { network : 'local' , privateKey : privateKeyString , name : 'master' } ) ; const signer = new SignerModel ( { account : multiScriptHash , scopes : WitnessScopeModel . None } ) ; const builder = new ScriptBuilder ( ) ; builder . emitDynamicAppCall ( common . nativeHashes . NEO , 'transfer' , CallFlags . All , multiScriptHash , scriptHash , 10 ) ; const script = builder . build ( ) ; const txUnsigned = new TransactionModel ( { script , systemFee : new BN ( 10000 ) , networkFee : new BN ( 245000 ) , validUntilBlock : 800000 , signers : [ signer ] , network : config . blockchain . network , maxValidUntilBlockIncrement : config . blockchain . maxValidUntilBlockIncrement } ) ; const signature = await keystore . sign ( { account : { network : 'local' , address : privateKeyToAddress ( 'e35fa5d1652c4c65e296c86e63a3da6939bc471b741845be636e2daa320dc770' ) } , message : txUnsigned . message . toString ( 'hex' ) } ) ; const witness = crypto . createMultiSignatureWitness ( 1 , [ publicKey ] , { [ `${ common . ecPointToHex ( publicKey ) }` ] : Buffer . from ( signature , 'hex' ) } , WitnessModel ) ; const finalTransaction = txUnsigned . clone ( { witnesses : [ witness ] } ) ; console . log ( finalTransaction . serializeWire ( ) . toString ( 'hex' ) ) ; console . log ( address ) ; } ) ; } ) ;