github.com/annchain/OG@v0.0.9/rpc/token_controller.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 rpc
    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/math"
    22  	"github.com/annchain/OG/og/types"
    23  	"github.com/annchain/OG/og/types/archive"
    24  
    25  	"github.com/annchain/OG/status"
    26  	"github.com/gin-gonic/gin"
    27  	"github.com/sirupsen/logrus"
    28  	"net/http"
    29  	"strconv"
    30  )
    31  
    32  type NewPublicOfferingRequest struct {
    33  	Nonce      uint64 `json:"nonce"`
    34  	From       string `json:"from"`
    35  	Value      string `json:"value"`
    36  	Action     uint8  `json:"action"`
    37  	EnableSPO  bool   `json:"enable_spo"`
    38  	CryptoType string `json:"crypto_type"`
    39  	Signature  string `json:"signature"`
    40  	Pubkey     string `json:"pubkey"`
    41  	TokenId    int32  `json:"token_id"`
    42  	TokenName  string `json:"token_name"`
    43  }
    44  
    45  //todo optimize later
    46  func (r *RpcController) TokenDestroy(c *gin.Context) {
    47  	var (
    48  		tx    types.Txi
    49  		txReq NewPublicOfferingRequest
    50  		sig   crypto.Signature
    51  		pub   crypto.PublicKey
    52  	)
    53  
    54  	if status.ArchiveMode {
    55  		Response(c, http.StatusBadRequest, fmt.Errorf("archive mode"), nil)
    56  		return
    57  	}
    58  
    59  	err := c.ShouldBindJSON(&txReq)
    60  	if err != nil {
    61  		Response(c, http.StatusBadRequest, fmt.Errorf("request format error: %v", err), nil)
    62  		return
    63  	}
    64  	from, err := common.StringToAddress(txReq.From)
    65  	if err != nil {
    66  		Response(c, http.StatusBadRequest, fmt.Errorf("from address format error: %v", err), nil)
    67  		return
    68  	}
    69  
    70  	//nonce, err := strconv.ParseUint(txReq.Nonce, 10, 64)
    71  	//if err != nil {
    72  	//	Response(c, http.StatusBadRequest, fmt.Errorf("nonce format error"), nil)
    73  	//	return
    74  	//}
    75  
    76  	signature := common.FromHex(txReq.Signature)
    77  	if signature == nil {
    78  		Response(c, http.StatusBadRequest, fmt.Errorf("signature format error"), nil)
    79  		return
    80  	}
    81  
    82  	if err != nil {
    83  		Response(c, http.StatusBadRequest, fmt.Errorf("action  format error: %v", err), nil)
    84  		return
    85  	}
    86  
    87  	if txReq.CryptoType == "" {
    88  		pub, err = crypto.PublicKeyFromString(txReq.Pubkey)
    89  		if err != nil {
    90  			Response(c, http.StatusBadRequest, fmt.Errorf("pubkey format error %v", err), nil)
    91  			return
    92  		}
    93  	} else {
    94  
    95  		pub, err = crypto.PublicKeyFromStringWithCryptoType(txReq.CryptoType, txReq.Pubkey)
    96  		if err != nil {
    97  			Response(c, http.StatusBadRequest, fmt.Errorf("pubkey format error %v", err), nil)
    98  			return
    99  		}
   100  	}
   101  
   102  	sig = crypto.SignatureFromBytes(pub.Type, signature)
   103  	if sig.Type != og_interface.Signer.GetCryptoType() || pub.Type != og_interface.Signer.GetCryptoType() {
   104  		Response(c, http.StatusOK, fmt.Errorf("ogcrypto algorithm mismatch"), nil)
   105  		return
   106  	}
   107  
   108  	fmt.Println(fmt.Sprintf("tx req action: %x", txReq.Action))
   109  
   110  	tx, err = r.TxCreator.NewActionTxWithSeal(ogcore.ActionTxBuildRequest{
   111  		UnsignedTxBuildRequest: ogcore.UnsignedTxBuildRequest{
   112  			From:         from,
   113  			To:           common.Address{},
   114  			Value:        math.NewBigInt(0),
   115  			AccountNonce: txReq.Nonce,
   116  			TokenId:      0,
   117  		},
   118  		Action:    txReq.Action,
   119  		EnableSpo: txReq.EnableSPO,
   120  		TokenName: txReq.TokenName,
   121  		Pubkey:    pub,
   122  		Sig:       sig,
   123  	})
   124  	if err != nil {
   125  		logrus.WithError(err).Warn("failed to seal tx")
   126  		Response(c, http.StatusInternalServerError, fmt.Errorf("new tx failed %v", err), nil)
   127  		return
   128  	}
   129  	logrus.WithField("tx", tx).Debugf("tx generated")
   130  	if !r.SyncerManager.IncrementalSyncer.Enabled {
   131  		Response(c, http.StatusOK, fmt.Errorf("tx is disabled when syncing"), nil)
   132  		return
   133  	}
   134  
   135  	r.TxBuffer.ReceivedNewTxChan <- tx
   136  
   137  	Response(c, http.StatusOK, nil, tx.GetHash().Hex())
   138  	return
   139  }
   140  
   141  func (r *RpcController) NewPublicOffering(c *gin.Context) {
   142  	var (
   143  		tx    types.Txi
   144  		txReq NewPublicOfferingRequest
   145  		sig   crypto.Signature
   146  		pub   crypto.PublicKey
   147  	)
   148  
   149  	if status.ArchiveMode {
   150  		Response(c, http.StatusBadRequest, fmt.Errorf("archive mode"), nil)
   151  		return
   152  	}
   153  
   154  	err := c.ShouldBindJSON(&txReq)
   155  	if err != nil {
   156  		Response(c, http.StatusBadRequest, fmt.Errorf("request format error: %v", err), nil)
   157  		return
   158  	}
   159  	from, err := common.StringToAddress(txReq.From)
   160  	if err != nil {
   161  		Response(c, http.StatusBadRequest, fmt.Errorf("from address format error: %v", err), nil)
   162  		return
   163  	}
   164  
   165  	value, ok := math.NewBigIntFromString(txReq.Value, 10)
   166  	if !ok {
   167  		err = fmt.Errorf("new Big Int error")
   168  	}
   169  	if err != nil {
   170  		Response(c, http.StatusBadRequest, fmt.Errorf("value format error: %v", err), nil)
   171  		return
   172  	}
   173  
   174  	//nonce, err := strconv.ParseUint(txReq.Nonce, 10, 64)
   175  	//if err != nil {
   176  	//	Response(c, http.StatusBadRequest, fmt.Errorf("nonce format error"), nil)
   177  	//	return
   178  	//}
   179  
   180  	signature := common.FromHex(txReq.Signature)
   181  	if signature == nil {
   182  		Response(c, http.StatusBadRequest, fmt.Errorf("signature format error"), nil)
   183  		return
   184  	}
   185  
   186  	if err != nil {
   187  		Response(c, http.StatusBadRequest, fmt.Errorf("action  format error: %v", err), nil)
   188  		return
   189  	}
   190  
   191  	if txReq.CryptoType == "" {
   192  		pub, err = crypto.PublicKeyFromString(txReq.Pubkey)
   193  		if err != nil {
   194  			Response(c, http.StatusBadRequest, fmt.Errorf("pubkey format error %v", err), nil)
   195  			return
   196  		}
   197  	} else {
   198  
   199  		pub, err = crypto.PublicKeyFromStringWithCryptoType(txReq.CryptoType, txReq.Pubkey)
   200  		if err != nil {
   201  			Response(c, http.StatusBadRequest, fmt.Errorf("pubkey format error %v", err), nil)
   202  			return
   203  		}
   204  	}
   205  
   206  	sig = crypto.SignatureFromBytes(pub.Type, signature)
   207  	if sig.Type != og_interface.Signer.GetCryptoType() || pub.Type != og_interface.Signer.GetCryptoType() {
   208  		Response(c, http.StatusOK, fmt.Errorf("ogcrypto algorithm mismatch"), nil)
   209  		return
   210  	}
   211  
   212  	tx, err = r.TxCreator.NewActionTxWithSeal(ogcore.ActionTxBuildRequest{
   213  		UnsignedTxBuildRequest: ogcore.UnsignedTxBuildRequest{
   214  			From:         from,
   215  			To:           common.Address{},
   216  			Value:        value,
   217  			AccountNonce: txReq.Nonce,
   218  			TokenId:      0,
   219  		},
   220  		Action:    txReq.Action,
   221  		EnableSpo: txReq.EnableSPO,
   222  		TokenName: txReq.TokenName,
   223  		Pubkey:    pub,
   224  		Sig:       sig,
   225  	})
   226  	if err != nil {
   227  		logrus.WithError(err).Warn("failed to seal tx")
   228  		Response(c, http.StatusInternalServerError, fmt.Errorf("new tx failed %v", err), nil)
   229  		return
   230  	}
   231  	logrus.WithField("tx", tx).Debugf("tx generated")
   232  	if !r.SyncerManager.IncrementalSyncer.Enabled {
   233  		Response(c, http.StatusOK, fmt.Errorf("tx is disabled when syncing"), nil)
   234  		return
   235  	}
   236  
   237  	r.TxBuffer.ReceivedNewTxChan <- tx
   238  
   239  	Response(c, http.StatusOK, nil, tx.GetHash().Hex())
   240  	return
   241  }
   242  
   243  func (r *RpcController) NewSecondOffering(c *gin.Context) {
   244  	var (
   245  		tx    types.Txi
   246  		txReq NewPublicOfferingRequest
   247  		sig   crypto.Signature
   248  		pub   crypto.PublicKey
   249  	)
   250  
   251  	if status.ArchiveMode {
   252  		Response(c, http.StatusBadRequest, fmt.Errorf("archive mode"), nil)
   253  		return
   254  	}
   255  
   256  	err := c.ShouldBindJSON(&txReq)
   257  	if err != nil {
   258  		Response(c, http.StatusBadRequest, fmt.Errorf("request format error: %v", err), nil)
   259  		return
   260  	}
   261  	from, err := common.StringToAddress(txReq.From)
   262  	if err != nil {
   263  		Response(c, http.StatusBadRequest, fmt.Errorf("from address format error: %v", err), nil)
   264  		return
   265  	}
   266  
   267  	value, ok := math.NewBigIntFromString(txReq.Value, 10)
   268  	if !ok {
   269  		err = fmt.Errorf("new Big Int error")
   270  	}
   271  	if err != nil {
   272  		Response(c, http.StatusBadRequest, fmt.Errorf("value format error: %v", err), nil)
   273  		return
   274  	}
   275  
   276  	//nonce, err := strconv.ParseUint(txReq.Nonce, 10, 64)
   277  	//if err != nil {
   278  	//	Response(c, http.StatusBadRequest, fmt.Errorf("nonce format error"), nil)
   279  	//	return
   280  	//}
   281  
   282  	signature := common.FromHex(txReq.Signature)
   283  	if signature == nil {
   284  		Response(c, http.StatusBadRequest, fmt.Errorf("signature format error"), nil)
   285  		return
   286  	}
   287  
   288  	if err != nil {
   289  		Response(c, http.StatusBadRequest, fmt.Errorf("action  format error: %v", err), nil)
   290  		return
   291  	}
   292  
   293  	if txReq.CryptoType == "" {
   294  		pub, err = crypto.PublicKeyFromString(txReq.Pubkey)
   295  		if err != nil {
   296  			Response(c, http.StatusBadRequest, fmt.Errorf("pubkey format error %v", err), nil)
   297  			return
   298  		}
   299  	} else {
   300  		pub, err = crypto.PublicKeyFromStringWithCryptoType(txReq.CryptoType, txReq.Pubkey)
   301  		if err != nil {
   302  			Response(c, http.StatusBadRequest, fmt.Errorf("pubkey format error %v", err), nil)
   303  			return
   304  		}
   305  	}
   306  
   307  	sig = crypto.SignatureFromBytes(pub.Type, signature)
   308  	if sig.Type != og_interface.Signer.GetCryptoType() || pub.Type != og_interface.Signer.GetCryptoType() {
   309  		Response(c, http.StatusOK, fmt.Errorf("ogcrypto algorithm mismatch"), nil)
   310  		return
   311  	}
   312  
   313  	tx, err = r.TxCreator.NewActionTxWithSeal(ogcore.ActionTxBuildRequest{
   314  		UnsignedTxBuildRequest: ogcore.UnsignedTxBuildRequest{
   315  			From:         from,
   316  			To:           common.Address{},
   317  			Value:        value,
   318  			AccountNonce: txReq.Nonce,
   319  			TokenId:      0,
   320  		},
   321  		Action:    archive.ActionTxActionSPO,
   322  		EnableSpo: txReq.EnableSPO,
   323  		TokenName: txReq.TokenName,
   324  		Pubkey:    pub,
   325  		Sig:       sig,
   326  	})
   327  	if err != nil {
   328  		logrus.WithError(err).Warn("failed to seal tx")
   329  		Response(c, http.StatusInternalServerError, fmt.Errorf("new tx failed %v", err), nil)
   330  		return
   331  	}
   332  	logrus.WithField("tx", tx).Debugf("tx generated")
   333  	if !r.SyncerManager.IncrementalSyncer.Enabled {
   334  		Response(c, http.StatusOK, fmt.Errorf("tx is disabled when syncing"), nil)
   335  		return
   336  	}
   337  
   338  	r.TxBuffer.ReceivedNewTxChan <- tx
   339  
   340  	Response(c, http.StatusOK, nil, tx.GetHash().Hex())
   341  	return
   342  }
   343  
   344  func (r *RpcController) LatestTokenId(c *gin.Context) {
   345  	tokenId := r.Og.Dag.GetLatestTokenId()
   346  	Response(c, http.StatusOK, nil, tokenId)
   347  }
   348  
   349  func (r *RpcController) Tokens(c *gin.Context) {
   350  	tokens := r.Og.Dag.GetTokens()
   351  	Response(c, http.StatusOK, nil, tokens)
   352  }
   353  
   354  func (r *RpcController) GetToken(c *gin.Context) {
   355  	str := c.Query("id")
   356  	tokenId, err := strconv.Atoi(str)
   357  	if err != nil {
   358  		Response(c, http.StatusBadRequest, err, nil)
   359  		return
   360  	}
   361  	token := r.Og.Dag.GetToken(int32(tokenId))
   362  	Response(c, http.StatusOK, nil, token)
   363  }