交易所钱包开发------USDT开发

标签:钱包开发
发布时间:2019年02月17日 价值:20000.00 / 共识:25

一、基于ERC20的USDT开发(go开发)
1、地址生成

  1. func generateAddr() {
  2. privateKey, err := crypto.GenerateKey()
  3. if err != nil {
  4. log.Fatal(err)
  5. }
  6. privateKeyBytes := crypto.FromECDSA(privateKey)
  7. privateKeyStr := hexutil.Encode(privateKeyBytes)[2:]
  8. log.Println("-String--private key ---", privateKeyStr)
  9. publicKey := privateKey.Public()
  10. publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
  11. if !ok {
  12. log.Fatal("error casting public key to ECDSA")
  13. }
  14. publicKeyBytes := crypto.FromECDSAPub(publicKeyECDSA)
  15. publicKeyStr := hexutil.Encode(publicKeyBytes)[4:]
  16. log.Println("-String--public key ---", publicKeyStr)
  17. address := crypto.PubkeyToAddress(*publicKeyECDSA).Hex()
  18. return privateKeyStr, publicKeyStr, address
  19. }

2、提币

  1. func WithdrawUSDT(_fromAddress, _senderPrivateKey, _toAddress, _contractAddress, _tokenAmount string, _ethAmount, _gasLimit ,_gasPrice int64) string {
  2. client, err := ethclient.Dial(serverconfig.Config.NodeAddress)
  3. //client, err := ethclient.Dial("https://ropsten.infura.io")
  4. if err != nil {
  5. log.Fatal(err)
  6. }
  7. privateKey, _ := crypto.HexToECDSA(_senderPrivateKey)
  8. fromAddress := accounts.Account{
  9. Address: common.HexToAddress(_fromAddress),
  10. }
  11. nonce, err := client.PendingNonceAt(context.Background(), fromAddress.Address)
  12. if err != nil {
  13. log.Panic(err)
  14. }
  15. toAddress := common.HexToAddress(_toAddress)
  16. value := big.NewInt(_ethAmount) // in wei (0 eth)
  17. gasLimit := uint64(_gasLimit) // in units
  18. gasPrice := big.NewInt(_gasPrice) // 20 gwei
  19. contractAddress := common.HexToAddress(_contractAddress)
  20. transferFnSignature := []byte("transfer(address,uint256)")
  21. hash := sha3.NewKeccak256()
  22. hash.Write(transferFnSignature)
  23. methodID := hash.Sum(nil)[:4]
  24. paddedAddress := common.LeftPadBytes(toAddress.Bytes(), 32)
  25. fmt.Println(hexutil.Encode(paddedAddress)) // 0x0000000000000000000000004592d8f8d7b001e72cb26a73e4fa1806a51ac79d
  26. amount := new(big.Int)
  27. amount.SetString(_tokenAmount, 10) // 1000000000000000000000 为1000 tokens
  28. paddedAmount := common.LeftPadBytes(amount.Bytes(), 32)
  29. fmt.Println(hexutil.Encode(paddedAmount)) // 0x00000000000000000000000000000000000000000000003635c9adc5dea00000
  30. //aa,_ := hexutil.Decode(hexutil.Encode(paddedAmount))
  31. //fmt.Println(padOrTrim(aa, 9))
  32. var data []byte
  33. data = append(data, methodID...)
  34. data = append(data, paddedAddress...)
  35. data = append(data, paddedAmount...)
  36. chainID, err := client.NetworkID(context.Background())
  37. log.Println("------------nonce---", nonce)
  38. tx := types.NewTransaction(nonce, contractAddress, value, gasLimit, gasPrice, data)
  39. signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), privateKey)
  40. log.Println("------After sign------")
  41. if err != nil {
  42. log.Println("------Sign Error------",err)
  43. log.Fatal(err)
  44. }
  45. var buff bytes.Buffer
  46. signedTx.EncodeRLP(&buff)
  47. err = client.SendTransaction(context.Background(), signedTx)
  48. //time.Sleep(time.Duration(15) * time.Second)
  49. if err != nil {
  50. log.Println("------SendTransaction Error------",err)
  51. log.Fatal(err)
  52. }
  53. // Get transaction hash
  54. txHash := fmt.Sprintf("%s", signedTx.Hash().Hex())
  55. log.Println("------txHash-----", txHash)
  56. return txHash

}

3、充币监控
—————-更新中—————————-

二、基于OMNI协议的USDT开发(nodejs开发)
1、地址生成

  1. let randomBytes = require('randombytes');
  2. let BigInteger = require('bigi');
  3. let ecurve = require('ecurve');
  4. let crypto = require('crypto');
  5. let cs = require('coinstring');
  6. function generateAddress() {
  7. let secp256k1 = ecurve.getCurveByName('secp256k1')
  8. let randombytes = randomBytes(32).toString('hex')
  9. let privateKey = Buffer.from(randombytes, 'hex')
  10. let ecparams = ecurve.getCurveByName('secp256k1')
  11. let curvePt = ecparams.G.multiply(BigInteger.fromBuffer(privateKey))
  12. let x = curvePt.affineX.toBuffer(32)
  13. let y = curvePt.affineY.toBuffer(32)
  14. let publicKey = Buffer.concat([Buffer.from([0x04]), x, y])
  15. //compressed
  16. publicKey = curvePt.getEncoded(true) //true forces compressed public key
  17. console.log("压缩公钥 :" + publicKey.toString('hex'))
  18. let sha = crypto.createHash('sha256').update(publicKey).digest()
  19. let pubkeyHash = crypto.createHash('rmd160').update(sha).digest()
  20. let address = cs.encode(pubkeyHash, 0x0)
  21. // address of compressed public key
  22. console.log("压缩地址:" + address) //<-- 0x0 is for public addresses
  23. //校验和Base58编码
  24. // let private_key2 = cs.encode(privateKey, 0x80) // private_key2 have some problem
  25. // console.log(private_key2) //<--- 0x80 is for private addresses
  26. let private_key = cs.encode(Buffer.concat([privateKey, new Buffer([0x01])]), 0x80)
  27. console.log("压缩私钥:" + private_key) // <-- compressed private address
  28. return [address, publicKey.toString('hex'), private_key]
  29. }
  30. module.exports = {
  31. generateAddress
  32. }

2、提币
———-更新中——————-

参考:https://juejin.im/entry/5ad98df951882567161a1e10

  • 分享 收藏
0 条评论
  • 这篇文章暂无评论,赶紧评论一下吧~