mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-16 22:51:22 +00:00
sign_tx to sign any transaction
This commit is contained in:
@ -5,7 +5,6 @@ import (
|
|||||||
|
|
||||||
"github.com/tendermint/tendermint/account"
|
"github.com/tendermint/tendermint/account"
|
||||||
"github.com/tendermint/tendermint/binary"
|
"github.com/tendermint/tendermint/binary"
|
||||||
blk "github.com/tendermint/tendermint/block"
|
|
||||||
. "github.com/tendermint/tendermint/common"
|
. "github.com/tendermint/tendermint/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -60,37 +59,3 @@ func ListAccountsHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
Accounts []*account.Account
|
Accounts []*account.Account
|
||||||
}{blockHeight, accounts})
|
}{blockHeight, accounts})
|
||||||
}
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
func SignSendTxHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
sendTxStr := GetParam(r, "sendTx")
|
|
||||||
privAccountsStr := GetParam(r, "privAccounts")
|
|
||||||
|
|
||||||
var err error
|
|
||||||
sendTx := binary.ReadJSON(&blk.SendTx{}, []byte(sendTxStr), &err).(*blk.SendTx)
|
|
||||||
if err != nil {
|
|
||||||
WriteAPIResponse(w, API_INVALID_PARAM, Fmt("Invalid sendTx: %v", err))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
privAccounts := binary.ReadJSON([]*account.PrivAccount{}, []byte(privAccountsStr), &err).([]*account.PrivAccount)
|
|
||||||
if err != nil {
|
|
||||||
WriteAPIResponse(w, API_INVALID_PARAM, Fmt("Invalid privAccounts: %v", err))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
for i, privAccount := range privAccounts {
|
|
||||||
if privAccount == nil || privAccount.PrivKey == nil {
|
|
||||||
WriteAPIResponse(w, API_INVALID_PARAM, Fmt("Invalid (empty) privAccount @%v", i))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for i, input := range sendTx.Inputs {
|
|
||||||
input.PubKey = privAccounts[i].PubKey
|
|
||||||
input.Signature = privAccounts[i].Sign(sendTx)
|
|
||||||
}
|
|
||||||
|
|
||||||
WriteAPIResponse(w, API_OK, struct {
|
|
||||||
SendTx *blk.SendTx
|
|
||||||
}{sendTx})
|
|
||||||
}
|
|
||||||
|
@ -11,6 +11,6 @@ func initHandlers() {
|
|||||||
http.HandleFunc("/gen_priv_account", GenPrivAccountHandler)
|
http.HandleFunc("/gen_priv_account", GenPrivAccountHandler)
|
||||||
http.HandleFunc("/get_account", GetAccountHandler)
|
http.HandleFunc("/get_account", GetAccountHandler)
|
||||||
http.HandleFunc("/list_accounts", ListAccountsHandler)
|
http.HandleFunc("/list_accounts", ListAccountsHandler)
|
||||||
http.HandleFunc("/sign_send_tx", SignSendTxHandler)
|
http.HandleFunc("/sign_tx", SignTxHandler)
|
||||||
http.HandleFunc("/list_validators", ListValidatorsHandler)
|
http.HandleFunc("/list_validators", ListValidatorsHandler)
|
||||||
}
|
}
|
||||||
|
57
rpc/txs.go
Normal file
57
rpc/txs.go
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
package rpc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/tendermint/tendermint/account"
|
||||||
|
"github.com/tendermint/tendermint/binary"
|
||||||
|
blk "github.com/tendermint/tendermint/block"
|
||||||
|
. "github.com/tendermint/tendermint/common"
|
||||||
|
)
|
||||||
|
|
||||||
|
func SignTxHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
txStr := GetParam(r, "tx")
|
||||||
|
privAccountsStr := GetParam(r, "privAccounts")
|
||||||
|
|
||||||
|
var err error
|
||||||
|
var tx blk.Tx
|
||||||
|
binary.ReadJSON(&tx, []byte(txStr), &err)
|
||||||
|
if err != nil {
|
||||||
|
WriteAPIResponse(w, API_INVALID_PARAM, Fmt("Invalid tx: %v", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
privAccounts := binary.ReadJSON([]*account.PrivAccount{}, []byte(privAccountsStr), &err).([]*account.PrivAccount)
|
||||||
|
if err != nil {
|
||||||
|
WriteAPIResponse(w, API_INVALID_PARAM, Fmt("Invalid privAccounts: %v", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for i, privAccount := range privAccounts {
|
||||||
|
if privAccount == nil || privAccount.PrivKey == nil {
|
||||||
|
WriteAPIResponse(w, API_INVALID_PARAM, Fmt("Invalid (empty) privAccount @%v", i))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
switch tx.(type) {
|
||||||
|
case *blk.SendTx:
|
||||||
|
sendTx := tx.(*blk.SendTx)
|
||||||
|
for i, input := range sendTx.Inputs {
|
||||||
|
input.PubKey = privAccounts[i].PubKey
|
||||||
|
input.Signature = privAccounts[i].Sign(sendTx)
|
||||||
|
}
|
||||||
|
case *blk.BondTx:
|
||||||
|
bondTx := tx.(*blk.BondTx)
|
||||||
|
for i, input := range bondTx.Inputs {
|
||||||
|
input.PubKey = privAccounts[i].PubKey
|
||||||
|
input.Signature = privAccounts[i].Sign(bondTx)
|
||||||
|
}
|
||||||
|
case *blk.UnbondTx:
|
||||||
|
unbondTx := tx.(*blk.UnbondTx)
|
||||||
|
unbondTx.Signature = privAccounts[0].Sign(unbondTx).(account.SignatureEd25519)
|
||||||
|
case *blk.RebondTx:
|
||||||
|
rebondTx := tx.(*blk.RebondTx)
|
||||||
|
rebondTx.Signature = privAccounts[0].Sign(rebondTx).(account.SignatureEd25519)
|
||||||
|
}
|
||||||
|
|
||||||
|
WriteAPIResponse(w, API_OK, struct{ blk.Tx }{tx})
|
||||||
|
}
|
Reference in New Issue
Block a user