Skip to content

Commit

Permalink
BBC calc txid
Browse files Browse the repository at this point in the history
  • Loading branch information
孙显松 committed Oct 13, 2020
1 parent b6ce684 commit db94bca
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
4 changes: 2 additions & 2 deletions core/bbc/internal/coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (c *Wallet) DerivePrivateKey() (privateKey string, err error) {
func (c *Wallet) Sign(msg, privateKey string) (string, error) {
var err error
// 1尝试解析为多签数据
if txData := tryParseTxDataWithTemplate(msg); txData != nil {
if txData := TryParseTxDataWithTemplate(msg); txData != nil {
txData.TxHex, err = c.SignTemplate(txData.TxHex, txData.TplHex, privateKey)
if err != nil {
return msg, errors.Wrap(err, "failed to encode tx")
Expand All @@ -148,7 +148,7 @@ func (c *Wallet) VerifySignature(pubKey, msg, signature string) error {
return errors.New("verify signature not supported for BBC currently")
}

func tryParseTxDataWithTemplate(msg string) *gobbc.TXData {
func TryParseTxDataWithTemplate(msg string) *gobbc.TXData {
var data gobbc.TXData
if err := data.DecodeString(msg); err != nil {
return nil
Expand Down
14 changes: 14 additions & 0 deletions core/bbc/mobile.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,20 @@ func Address2pubk(address string) (string, error) {
return gobbc.ConvertAddress2pubk(address)
}

// CalcTxid 计算txid, symbol: BBC|MKF
func CalcTxid(symbol, rawTx string) (string, error) {
if txData := internal.TryParseTxDataWithTemplate(rawTx); txData != nil {
rawTx = txData.TxHex
}

se := internal.SymbolSerializer(symbol)
tx, err := gobbc.DecodeRawTransaction(se, rawTx, true)
if err != nil {
return "", errors.Wrap(err, "unable to parse tx data")
}
return tx.Txid(se)
}

// TemplateInfo 简要模版信息
type TemplateInfo struct {
//Type 类型
Expand Down
10 changes: 9 additions & 1 deletion qa/bbc/example_bbc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func TestExampleBBC(t *testing.T) {
r.NoError(err)

rawTX = replaceTXVersion(*rawTX)
fmt.Println("rawTx", *rawTX)

deTx, err := bbc.DecodeSymbolTX("BBC", *rawTX) // <<=== sdk 反序列化交易
r.NoError(err)
Expand All @@ -86,8 +87,15 @@ func TestExampleBBC(t *testing.T) {
signedTX, err := bbc.SymbolSignWithPrivateKey("BBC", *rawTX, "", key.PrivateKey) // <<=== sdk 使用私钥对交易进行签名
r.NoError(err)

_, err = jsonRPC.Sendtransaction(signedTX) // <<=== RPC 发送交易
sendTxid, err := jsonRPC.Sendtransaction(signedTX) // <<=== RPC 发送交易
r.NoError(err)

sdkTxid, err := bbc.CalcTxid("BBC", signedTX)
r.NoError(err)
rpcDe, err := jsonRPC.Decodetransaction(signedTX)
r.NoError(err)
fmt.Printf("txidS\n sdk: %s\n snd: %s \n rpc: %s", sdkTxid, *sendTxid, rpcDe.Txid)
r.Equal(sdkTxid, *sendTxid)

r.NoError(bbrpc.Wait4nBlocks(1, jsonRPC))

Expand Down
9 changes: 8 additions & 1 deletion qa/bbc/mkf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,16 @@ func TestExampleMKF(t *testing.T) {
signedTX, err := bbc.SymbolSignWithPrivateKey(bbc.SymbolMKF, *rawTX, "", key.PrivateKey) // <<=== sdk 使用私钥对交易进行签名
r.NoError(err)

_, err = jsonRPC.Sendtransaction(signedTX) // <<=== RPC 发送交易
sendTxid, err := jsonRPC.Sendtransaction(signedTX) // <<=== RPC 发送交易
r.NoError(err)

sdkTxid, err := bbc.CalcTxid("MKF", signedTX)
r.NoError(err)
rpcDe, err := jsonRPC.Decodetransaction(signedTX)
r.NoError(err)
fmt.Printf("txidS\n sdk: %s\n snd: %s \n rpc: %s", sdkTxid, *sendTxid, rpcDe.Txid)
r.Equal(sdkTxid, *sendTxid)

r.NoError(bbrpc.Wait4nBlocks(1, jsonRPC))

bal, err := jsonRPC.Getbalance(nil, &key.Address) // <<=== RPC 查询余额
Expand Down
1 change: 1 addition & 0 deletions scripts/ci-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ go build -o ci.out \
&& go test ${pkg}/core/bbc/... \
&& go test ${pkg}/core/btc/... \
&& go test ${pkg}/core/eth/... \
&& go test ${pkg}/core/trx/... \
&& go test ${pkg}/qa/bbc/... \
&& go test ${pkg}/qa/btc/... \
&& go test ${pkg}/qa/eth/... \
Expand Down

0 comments on commit db94bca

Please sign in to comment.