github.com/annchain/OG@v0.0.9/client/tx_client/request.go (about)

     1  package tx_client
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/annchain/OG/arefactor/og_interface"
     6  	"github.com/annchain/OG/common"
     7  	"github.com/annchain/OG/common/crypto"
     8  	"github.com/annchain/OG/common/hexutil"
     9  	"github.com/annchain/OG/common/math"
    10  	"github.com/annchain/OG/og/types"
    11  	"github.com/annchain/OG/og/types/archive"
    12  
    13  	"github.com/annchain/OG/og/verifier"
    14  	"github.com/annchain/OG/rpc"
    15  )
    16  
    17  type RequstGenerator struct {
    18  	privKey   crypto.PrivateKey
    19  	publicKey *crypto.PublicKey
    20  	address   common.Address
    21  	Nodebug   bool
    22  }
    23  
    24  func (r *RequstGenerator) Address() common.Address {
    25  	return r.address
    26  }
    27  
    28  func NewRequestGenerator(priv crypto.PrivateKey) *RequstGenerator {
    29  	return &RequstGenerator{
    30  		privKey:   priv,
    31  		publicKey: priv.PublicKey(),
    32  		address:   priv.PublicKey().Address(),
    33  	}
    34  }
    35  
    36  func (r *RequstGenerator) TokenPublishing(nonce uint64, enableSPO bool, tokenName string, value *math.BigInt) rpc.NewPublicOfferingRequest {
    37  	//pub, priv := ogcrypto.Signer.RandomKeyPair()
    38  	from := r.address
    39  	if !r.Nodebug {
    40  		fmt.Println(from.String())
    41  	}
    42  	tx := archive.ActionTx{
    43  		TxBase: types.TxBase{
    44  			Type:         types.TxBaseAction,
    45  			AccountNonce: uint64(nonce),
    46  			PublicKey:    r.publicKey.KeyBytes[:],
    47  		},
    48  		Action: archive.ActionTxActionIPO,
    49  		From:   &from,
    50  		ActionData: &archive.PublicOffering{
    51  			Value:     value,
    52  			EnableSPO: enableSPO,
    53  			TokenName: tokenName,
    54  		},
    55  	}
    56  	tx.Signature = og_interface.Signer.Sign(r.privKey, tx.SignatureTargets()).SignatureBytes[:]
    57  	v := verifier.TxFormatVerifier{}
    58  	ok := v.VerifySignature(&tx)
    59  	if !ok {
    60  		target := tx.SignatureTargets()
    61  		fmt.Println(hexutil.Encode(target))
    62  		panic("not ok")
    63  	}
    64  	request := rpc.NewPublicOfferingRequest{
    65  		Nonce:     nonce,
    66  		From:      tx.From.Hex(),
    67  		Value:     value.String(),
    68  		Signature: tx.Signature.String(),
    69  		Pubkey:    r.publicKey.String(),
    70  		Action:    archive.ActionTxActionIPO,
    71  		EnableSPO: enableSPO,
    72  		TokenName: tokenName,
    73  	}
    74  
    75  	return request
    76  }
    77  
    78  func (r *RequstGenerator) TokenDestroy(tokenId int32, nonce uint64) rpc.NewPublicOfferingRequest {
    79  	//pub, priv := ogcrypto.Signer.RandomKeyPair()
    80  	from := r.address
    81  	//fmt.Println(pub.String(), priv.String(), from.String())
    82  	fmt.Println(from.String())
    83  	value := math.NewBigInt(0)
    84  
    85  	tx := archive.ActionTx{
    86  		TxBase: types.TxBase{
    87  			Type:         types.TxBaseAction,
    88  			AccountNonce: uint64(nonce),
    89  			PublicKey:    r.publicKey.KeyBytes[:],
    90  		},
    91  		Action: archive.ActionTxActionDestroy,
    92  		From:   &from,
    93  		ActionData: &archive.PublicOffering{
    94  			Value: value,
    95  			//EnableSPO:  enableSPO,
    96  			//TokenName: "test_token",
    97  			TokenId: tokenId,
    98  		},
    99  	}
   100  	tx.Signature = og_interface.Signer.Sign(r.privKey, tx.SignatureTargets()).SignatureBytes[:]
   101  	v := verifier.TxFormatVerifier{}
   102  	ok := v.VerifySignature(&tx)
   103  	if !ok {
   104  		target := tx.SignatureTargets()
   105  		fmt.Println(hexutil.Encode(target))
   106  		panic("not ok")
   107  	}
   108  	request := rpc.NewPublicOfferingRequest{
   109  		Nonce:     nonce,
   110  		From:      tx.From.Hex(),
   111  		Value:     value.String(),
   112  		Signature: tx.Signature.String(),
   113  		Pubkey:    r.publicKey.String(),
   114  		Action:    archive.ActionTxActionDestroy,
   115  		//EnableSPO: enableSPO,
   116  		//TokenName: "test_token",
   117  		TokenId: tokenId,
   118  	}
   119  
   120  	return request
   121  }
   122  
   123  func (r *RequstGenerator) SecondPublicOffering(tokenId int32, nonce uint64, value *math.BigInt) rpc.NewPublicOfferingRequest {
   124  	//pub, priv := ogcrypto.Signer.RandomKeyPair()
   125  	from := r.address
   126  	if !r.Nodebug {
   127  		fmt.Println(from.String())
   128  	}
   129  	tx := archive.ActionTx{
   130  		TxBase: types.TxBase{
   131  			Type:         types.TxBaseAction,
   132  			AccountNonce: uint64(nonce),
   133  			PublicKey:    r.publicKey.KeyBytes[:],
   134  		},
   135  		Action: archive.ActionTxActionSPO,
   136  		From:   &from,
   137  		ActionData: &archive.PublicOffering{
   138  			Value: value,
   139  			//EnableSPO: true,
   140  			//TokenName: "test_token",
   141  			TokenId: tokenId,
   142  		},
   143  	}
   144  	tx.Signature = og_interface.Signer.Sign(r.privKey, tx.SignatureTargets()).SignatureBytes[:]
   145  	v := verifier.TxFormatVerifier{}
   146  	ok := v.VerifySignature(&tx)
   147  	target := tx.SignatureTargets()
   148  	fmt.Println(hexutil.Encode(target))
   149  	if !ok {
   150  		target := tx.SignatureTargets()
   151  		fmt.Println(hexutil.Encode(target))
   152  		panic("not ok")
   153  	}
   154  	request := rpc.NewPublicOfferingRequest{
   155  		Nonce:     nonce,
   156  		From:      tx.From.Hex(),
   157  		Value:     value.String(),
   158  		Signature: tx.Signature.String(),
   159  		Pubkey:    r.publicKey.String(),
   160  		Action:    archive.ActionTxActionSPO,
   161  		//EnableSPO: true,
   162  		//TokenName: "test_token",
   163  		TokenId: tokenId,
   164  	}
   165  
   166  	return request
   167  }
   168  
   169  func (r *RequstGenerator) NormalTx(tokenId int32, nonce uint64, to common.Address, value *math.BigInt) rpc.NewTxRequest {
   170  	from := r.address
   171  	if !r.Nodebug {
   172  		fmt.Println(from.String(), to.String())
   173  	}
   174  	tx := types.Tx{
   175  		TxBase: types.TxBase{
   176  			Type:         types.TxBaseTypeTx,
   177  			PublicKey:    r.publicKey.KeyBytes[:],
   178  			AccountNonce: uint64(nonce),
   179  		},
   180  		From:    &from,
   181  		TokenId: tokenId,
   182  		Value:   value,
   183  		To:      to,
   184  	}
   185  	tx.Signature = og_interface.Signer.Sign(r.privKey, tx.SignatureTargets()).SignatureBytes[:]
   186  	v := verifier.TxFormatVerifier{}
   187  	ok := v.VerifySignature(&tx)
   188  	if !ok {
   189  		target := tx.SignatureTargets()
   190  		fmt.Println(hexutil.Encode(target))
   191  		panic("not ok")
   192  	}
   193  	request := rpc.NewTxRequest{
   194  		Nonce:     fmt.Sprintf("%d", nonce),
   195  		From:      tx.From.Hex(),
   196  		To:        to.String(),
   197  		Value:     tx.Value.String(),
   198  		Signature: tx.Signature.String(),
   199  		Pubkey:    r.publicKey.String(),
   200  		TokenId:   tokenId,
   201  	}
   202  	return request
   203  }