Skip to content

Commit

Permalink
first goto it
Browse files Browse the repository at this point in the history
  • Loading branch information
Geapefurit committed Jul 1, 2024
1 parent 8db5ef2 commit 6b2659a
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 41 deletions.
9 changes: 2 additions & 7 deletions pkg/coins/btc/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func preSign(ctx context.Context, in []byte, tokenInfo *coins.TokenInfo) ([]byte
amount = info.Value
)

_addr, err := btcutil.DecodeAddress(info.From, btc.BTCNetMap[info.ENV])
fromAddr, err := btcutil.DecodeAddress(from, btc.BTCNetMap[info.ENV])
if err != nil {
return nil, fmt.Errorf("%v,%v", env.ErrAddressInvalid.Error(), err)
}
Expand All @@ -163,7 +163,7 @@ func preSign(ctx context.Context, in []byte, tokenInfo *coins.TokenInfo) ([]byte
listUnspentResult, err = cli.ListUnspentMinMaxAddresses(
btc.DefaultMinConfirms,
btc.DefaultMaxConfirms,
[]btcutil.Address{_addr},
[]btcutil.Address{fromAddr},
)
if err != nil || listUnspentResult == nil {
return true, err
Expand Down Expand Up @@ -216,11 +216,6 @@ func preSign(ctx context.Context, in []byte, tokenInfo *coins.TokenInfo) ([]byte
return nil, env.ErrInsufficientBalance
}

fromAddr, err := btcutil.DecodeAddress(from, btc.BTCNetMap[info.ENV])
if err != nil {
return nil, fmt.Errorf("%v,%v", env.ErrAddressInvalid, err)
}

fromScript, err := txscript.PayToAddrScript(fromAddr)
if err != nil {
return nil, fmt.Errorf("%v,%v", env.ErrAddressInvalid, err)
Expand Down
125 changes: 92 additions & 33 deletions pkg/coins/btc/sign/sign.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package sign

import (
"bytes"
"context"
"encoding/hex"
"encoding/json"
"fmt"

"github.com/NpoolPlatform/go-service-framework/pkg/oss"
"github.com/NpoolPlatform/sphinx-plugin/pkg/coins"
"github.com/NpoolPlatform/sphinx-plugin/pkg/coins/btc"
"github.com/NpoolPlatform/sphinx-plugin/pkg/coins/register"
"github.com/NpoolPlatform/sphinx-plugin/pkg/env"
ct "github.com/NpoolPlatform/sphinx-plugin/pkg/types"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
Expand Down Expand Up @@ -38,16 +41,19 @@ func createAccount(ctx context.Context, in []byte, tokenInfo *coins.TokenInfo) (
return nil, err
}

secret, err := btcec.NewPrivateKey(btcec.S256())
if err != nil {
return nil, err
}
// secret, err := btcec.NewPrivateKey(btcec.S256())
// if err != nil {
// return nil, err
// }

if !coins.CheckSupportNet(info.ENV) {
return nil, env.ErrEVNCoinNetValue
}
// if !coins.CheckSupportNet(info.ENV) {
// return nil, env.ErrEVNCoinNetValue
// }

wif, err := btcutil.NewWIF(secret, btc.BTCNetMap[info.ENV], true)
// wif, err := btcutil.NewWIF(secret, btc.BTCNetMap[info.ENV], true)

wifStr := "cQ4yrDokKGFWfaJujp3HKPoWNqn5QTHYjnAV1JxEt6qRVhhtmzar"
wif, err := btcutil.DecodeWIF(wifStr)
if err != nil {
return nil, err
}
Expand All @@ -60,7 +66,17 @@ func createAccount(ctx context.Context, in []byte, tokenInfo *coins.TokenInfo) (
return nil, err
}

addr := addressPubKey.EncodeAddress()
pkscript, err := PayToPubKeyScript(addressPubKey.ScriptAddress())
if err != nil {
return nil, err
}

pksh, err := btcutil.NewAddressScriptHash(pkscript, btc.BTCNetMap[info.ENV])
if err != nil {
return nil, err
}

addr := pksh.EncodeAddress()

_out := ct.NewAccountResponse{
Address: addr,
Expand All @@ -75,6 +91,11 @@ func createAccount(ctx context.Context, in []byte, tokenInfo *coins.TokenInfo) (
return out, err
}

func PayToPubKeyScript(serializedPubKey []byte) ([]byte, error) {
return txscript.NewScriptBuilder().AddOp(txscript.OP_1).AddData(serializedPubKey).AddOp(txscript.OP_1).
AddOp(txscript.OP_CHECKMULTISIG).Script()
}

// signTx ..
func signTx(ctx context.Context, in []byte, tokenInfo *coins.TokenInfo) (out []byte, err error) {
info := btc.SignMsgTx{}
Expand All @@ -87,8 +108,6 @@ func signTx(ctx context.Context, in []byte, tokenInfo *coins.TokenInfo) (out []b
fromScript = info.PayToAddrScript
amounts = info.Amounts
msgTx = info.MsgTx
txIns = msgTx.TxIn
txOuts = msgTx.TxOut
)

wifStr, err := oss.GetObject(ctx, s3KeyPrxfix+from, true)
Expand All @@ -101,19 +120,34 @@ func signTx(ctx context.Context, in []byte, tokenInfo *coins.TokenInfo) (out []b
return nil, err
}

for txIdx := range txIns {
sig, err := txscript.SignatureScript(
addressPubKey, err := btcutil.NewAddressPubKey(
wif.PrivKey.PubKey().SerializeCompressed(),
btc.BTCNetMap[info.ENV],
)
if err != nil {
return nil, err
}

pkscript, err := PayToPubKeyScript(addressPubKey.ScriptAddress())
if err != nil {
return nil, err
}

fmt.Println("pre sign tx", GetRawTx(info.MsgTx))
for txIdx := range msgTx.TxIn {

sig, err := SignatureScript(
msgTx,
txIdx,
fromScript,
pkscript,
txscript.SigHashAll,
wif.PrivKey,
true,
btc.BTCNetMap[info.ENV],
)
if err != nil {
return nil, err
}

fmt.Println("scriptsig ", hex.EncodeToString(sig))
msgTx.TxIn[txIdx].SignatureScript = sig

// validate signature
Expand All @@ -130,30 +164,55 @@ func signTx(ctx context.Context, in []byte, tokenInfo *coins.TokenInfo) (out []b
if err != nil {
return nil, err
}
fmt.Println("sssssssssss2")

if err := vm.Execute(); err != nil {
return nil, err
}

fmt.Println("sssssssssss3")
}

for _, txIn := range txIns {
txIns = append(txIns, &wire.TxIn{
PreviousOutPoint: wire.OutPoint{
Hash: txIn.PreviousOutPoint.Hash,
Index: txIn.PreviousOutPoint.Index,
},
SignatureScript: txIn.SignatureScript,
Witness: txIn.Witness,
Sequence: txIn.Sequence,
})
fmt.Println("txHex:", GetRawTx(msgTx))

return json.Marshal(msgTx)
}

func SignatureScript(tx *wire.MsgTx, idx int, subscript []byte, hashType txscript.SigHashType, privKey *btcec.PrivateKey, chainParams *chaincfg.Params) ([]byte, error) {
sig, err := txscript.RawTxInSignature(tx, idx, subscript, hashType, privKey)
if err != nil {
return nil, err
}

for _, txOut := range txOuts {
txOuts = append(txOuts, &wire.TxOut{
Value: txOut.Value,
PkScript: txOut.PkScript,
})
addressPubKey, err := btcutil.NewAddressPubKey(
privKey.PubKey().SerializeCompressed(),
chainParams,
)
if err != nil {
return nil, err
}

return json.Marshal(msgTx)
pkscript, err := PayToPubKeyScript(addressPubKey.ScriptAddress())
if err != nil {
return nil, err
}

fmt.Println("signature", hex.EncodeToString(sig))
fmt.Println("pubkey", hex.EncodeToString(addressPubKey.ScriptAddress()))
fmt.Println("Redeem script", hex.EncodeToString(pkscript))

return txscript.NewScriptBuilder().AddOp(txscript.OP_0).AddData(sig).AddData(pkscript).Script()
}

func GetRawTx(tx *wire.MsgTx) string {
txHex := ""
if tx != nil {
// Serialize the transaction and convert to hex string.
buf := bytes.NewBuffer(make([]byte, 0, tx.SerializeSize()))
if err := tx.Serialize(buf); err != nil {
return "newFutureError(err)"
}
txHex = hex.EncodeToString(buf.Bytes())
}
return txHex
}
2 changes: 1 addition & 1 deletion pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func ToCoinType(coinType string) (sphinxplugin.CoinType, error) {
return sphinxplugin.CoinType(_coinType), nil
}

//nolint because CoinType not define in this package
// nolint because CoinType not define in this package
func ToCoinName(coinType sphinxplugin.CoinType) string {
coinName := strings.TrimPrefix(coinType.String(), coinTypePrefix)
return coinName
Expand Down

0 comments on commit 6b2659a

Please sign in to comment.