sign_tx to sign any transaction

This commit is contained in:
Jae Kwon 2015-01-15 23:48:30 -08:00
parent 7d4903ef97
commit b1be787987
3 changed files with 58 additions and 36 deletions

View File

@ -5,7 +5,6 @@ import (
"github.com/tendermint/tendermint/account"
"github.com/tendermint/tendermint/binary"
blk "github.com/tendermint/tendermint/block"
. "github.com/tendermint/tendermint/common"
)
@ -60,37 +59,3 @@ func ListAccountsHandler(w http.ResponseWriter, r *http.Request) {
Accounts []*account.Account
}{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})
}

View File

@ -11,6 +11,6 @@ func initHandlers() {
http.HandleFunc("/gen_priv_account", GenPrivAccountHandler)
http.HandleFunc("/get_account", GetAccountHandler)
http.HandleFunc("/list_accounts", ListAccountsHandler)
http.HandleFunc("/sign_send_tx", SignSendTxHandler)
http.HandleFunc("/sign_tx", SignTxHandler)
http.HandleFunc("/list_validators", ListValidatorsHandler)
}

57
rpc/txs.go Normal file
View 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})
}