github.com/annchain/OG@v0.0.9/tests/test_tps/testToken.go (about)

     1  // Copyright © 2019 Annchain Authors <EMAIL ADDRESS>
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  package main
    15  
    16  import (
    17  	"fmt"
    18  	"github.com/annchain/OG/arefactor/og_interface"
    19  	"github.com/annchain/OG/common"
    20  	"github.com/annchain/OG/common/crypto"
    21  	"github.com/annchain/OG/common/hexutil"
    22  	"github.com/annchain/OG/common/math"
    23  	"github.com/annchain/OG/og/types"
    24  	"github.com/annchain/OG/og/types/archive"
    25  	"github.com/annchain/OG/og/verifier"
    26  
    27  	"github.com/annchain/OG/rpc"
    28  )
    29  
    30  var txurl = "http://172.28.152.101:8000/new_transaction"
    31  var ipoUrl = "http://172.28.152.101:8000/token/NewPublicOffering"
    32  var spoUrl = "http://172.28.152.101:8000/token/NewSecondOffering"
    33  var destroyUrl = "http://172.28.152.101:8000/token/Destroy"
    34  
    35  var debug bool
    36  
    37  func main() {
    38  	debug = true
    39  	a := NewTxClient()
    40  	nonce := 6
    41  	priv, pub, addr := getkey()
    42  	tokenName := "btcdh"
    43  	_ = tokenName
    44  	//request := generateTokenPublishing(priv,pub,addr,nonce,true,tokenName)
    45  	//a.sendTx(&request,0,ipoUrl)
    46  	//request := secondPublicOffering(priv, pub, addr, 2, nonce)
    47  	//a.sendTx(&request, 0, spoUrl)
    48  	//request := transfer(priv, pub, addr, 3, nonce)
    49  	//a.sendTx(&request, 0, txurl)
    50  	request := destroyRequest(priv, pub, addr, 2, nonce)
    51  	a.sendTx(&request, destroyUrl, "POST")
    52  	return
    53  }
    54  
    55  func getkey() (priv crypto.PrivateKey, pub crypto.PublicKey, addr common.Address) {
    56  	var err error
    57  	pub, err = crypto.PublicKeyFromString(
    58  		"0x0104c9a6957815922545a5711cf8a12feeb67c32c8e5fd801baf1319a4d87759321abfbf3b2fde27d337982596b108a4224293a1b52ad87bb221a24375bb8c592a70")
    59  	if err != nil {
    60  		panic(err)
    61  	}
    62  	priv, err = crypto.PrivateKeyFromString(
    63  		"0x012afb81be217e411cfa7610cb99c4bbe6db0ea0e515cfe5fd92ecad0d61141d95")
    64  	if err != nil {
    65  		panic(err)
    66  	}
    67  	addr, err = common.StringToAddress("0x1c7de61f817b6a37c5b799190a3a29b8e1e2c781")
    68  	if err != nil {
    69  		panic(err)
    70  	}
    71  	return
    72  }
    73  
    74  func generateTokenPublishing(priv crypto.PrivateKey, pub crypto.PublicKey, from common.Address, nonce int, enableSPO bool, tokenName string) rpc.NewPublicOfferingRequest {
    75  	//pub, priv := ogcrypto.Signer.RandomKeyPair()
    76  	//from:= pub.Address()
    77  	fmt.Println(pub.String(), priv.String(), from.String())
    78  	value := math.NewBigInt(8888888)
    79  
    80  	tx := archive.ActionTx{
    81  		TxBase: types.TxBase{
    82  			Type:         types.TxBaseAction,
    83  			AccountNonce: uint64(nonce),
    84  			PublicKey:    pub.KeyBytes[:],
    85  		},
    86  		Action: archive.ActionTxActionIPO,
    87  		From:   &from,
    88  		ActionData: &archive.PublicOffering{
    89  			Value:     value,
    90  			EnableSPO: enableSPO,
    91  			TokenName: tokenName,
    92  		},
    93  	}
    94  	tx.Signature = og_interface.Signer.Sign(priv, tx.SignatureTargets()).SignatureBytes[:]
    95  	v := verifier.TxFormatVerifier{}
    96  	ok := v.VerifySignature(&tx)
    97  	if !ok {
    98  		target := tx.SignatureTargets()
    99  		fmt.Println(hexutil.Encode(target))
   100  		panic("not ok")
   101  	}
   102  	request := rpc.NewPublicOfferingRequest{
   103  		Nonce:     uint64(nonce),
   104  		From:      tx.From.Hex(),
   105  		Value:     value.String(),
   106  		Signature: tx.Signature.String(),
   107  		Pubkey:    pub.String(),
   108  		Action:    archive.ActionTxActionIPO,
   109  		EnableSPO: enableSPO,
   110  		TokenName: tokenName,
   111  	}
   112  
   113  	return request
   114  }
   115  
   116  func destroyRequest(priv crypto.PrivateKey, pub crypto.PublicKey, from common.Address, tokenId int32, nonce int) rpc.NewPublicOfferingRequest {
   117  	//pub, priv := ogcrypto.Signer.RandomKeyPair()
   118  	//from:= pub.Address()
   119  	fmt.Println(pub.String(), priv.String(), from.String())
   120  	value := math.NewBigInt(0)
   121  
   122  	tx := archive.ActionTx{
   123  		TxBase: types.TxBase{
   124  			Type:         types.TxBaseAction,
   125  			AccountNonce: uint64(nonce),
   126  			PublicKey:    pub.KeyBytes[:],
   127  		},
   128  		Action: archive.ActionTxActionDestroy,
   129  		From:   &from,
   130  		ActionData: &archive.PublicOffering{
   131  			Value: value,
   132  			//EnableSPO:  enableSPO,
   133  			//TokenName: "test_token",
   134  			TokenId: tokenId,
   135  		},
   136  	}
   137  	tx.Signature = og_interface.Signer.Sign(priv, tx.SignatureTargets()).SignatureBytes[:]
   138  	v := verifier.TxFormatVerifier{}
   139  	ok := v.VerifySignature(&tx)
   140  	if !ok {
   141  		target := tx.SignatureTargets()
   142  		fmt.Println(hexutil.Encode(target))
   143  		panic("not ok")
   144  	}
   145  	request := rpc.NewPublicOfferingRequest{
   146  		Nonce:     uint64(nonce),
   147  		From:      tx.From.Hex(),
   148  		Value:     value.String(),
   149  		Signature: tx.Signature.String(),
   150  		Pubkey:    pub.String(),
   151  		Action:    archive.ActionTxActionDestroy,
   152  		//EnableSPO: enableSPO,
   153  		//TokenName: "test_token",
   154  		TokenId: tokenId,
   155  	}
   156  
   157  	return request
   158  }
   159  
   160  func secondPublicOffering(priv crypto.PrivateKey, pub crypto.PublicKey, from common.Address, tokenId int32, nonce int) rpc.NewPublicOfferingRequest {
   161  	//pub, priv := ogcrypto.Signer.RandomKeyPair()
   162  	//from:= pub.Address()
   163  	fmt.Println(pub.String(), priv.String(), from.String())
   164  	value := math.NewBigInt(100000)
   165  
   166  	tx := archive.ActionTx{
   167  		TxBase: types.TxBase{
   168  			Type:         types.TxBaseAction,
   169  			AccountNonce: uint64(nonce),
   170  			PublicKey:    pub.KeyBytes[:],
   171  		},
   172  		Action: archive.ActionTxActionSPO,
   173  		From:   &from,
   174  		ActionData: &archive.PublicOffering{
   175  			Value: value,
   176  			//EnableSPO: true,
   177  			//TokenName: "test_token",
   178  			TokenId: tokenId,
   179  		},
   180  	}
   181  	tx.Signature = og_interface.Signer.Sign(priv, tx.SignatureTargets()).SignatureBytes[:]
   182  	v := verifier.TxFormatVerifier{}
   183  	ok := v.VerifySignature(&tx)
   184  	target := tx.SignatureTargets()
   185  	fmt.Println(hexutil.Encode(target))
   186  	if !ok {
   187  		target := tx.SignatureTargets()
   188  		fmt.Println(hexutil.Encode(target))
   189  		panic("not ok")
   190  	}
   191  	request := rpc.NewPublicOfferingRequest{
   192  		Nonce:     uint64(nonce),
   193  		From:      tx.From.Hex(),
   194  		Value:     value.String(),
   195  		Signature: tx.Signature.String(),
   196  		Pubkey:    pub.String(),
   197  		Action:    archive.ActionTxActionSPO,
   198  		//EnableSPO: true,
   199  		//TokenName: "test_token",
   200  		TokenId: tokenId,
   201  	}
   202  
   203  	return request
   204  }
   205  
   206  func transfer(priv crypto.PrivateKey, pub crypto.PublicKey, from common.Address, tokenId int32, nonce int) rpc.NewTxRequest {
   207  	topub, _ := og_interface.Signer.RandomKeyPair()
   208  	to := topub.Address()
   209  	fmt.Println(pub.String(), priv.String(), from.String(), to.String())
   210  
   211  	tx := types.Tx{
   212  		TxBase: types.TxBase{
   213  			Type:         types.TxBaseTypeTx,
   214  			PublicKey:    pub.KeyBytes[:],
   215  			AccountNonce: uint64(nonce),
   216  		},
   217  		From:    &from,
   218  		TokenId: tokenId,
   219  		Value:   math.NewBigInt(66),
   220  		To:      to,
   221  	}
   222  	tx.Signature = og_interface.Signer.Sign(priv, tx.SignatureTargets()).SignatureBytes[:]
   223  	v := verifier.TxFormatVerifier{}
   224  	ok := v.VerifySignature(&tx)
   225  	target := tx.SignatureTargets()
   226  	fmt.Println(hexutil.Encode(target))
   227  	if !ok {
   228  		target := tx.SignatureTargets()
   229  		fmt.Println(hexutil.Encode(target))
   230  		panic("not ok")
   231  	}
   232  	request := rpc.NewTxRequest{
   233  		Nonce:     fmt.Sprintf("%d", nonce),
   234  		From:      tx.From.Hex(),
   235  		To:        to.String(),
   236  		Value:     tx.Value.String(),
   237  		Signature: tx.Signature.String(),
   238  		Pubkey:    pub.String(),
   239  		TokenId:   tokenId,
   240  	}
   241  	return request
   242  }