Skip to content

Commit da6f66e

Browse files
authored
601 multisig validation (#615)
* #601 add --post and --post-host to enable post transaction bytes on generated * #601 add multisig validation * #601 fix lint * #601 wrong description in cmd
1 parent 31247e6 commit da6f66e

File tree

6 files changed

+493
-6
lines changed

6 files changed

+493
-6
lines changed

cmd/transaction/cmd.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ func init() {
7575
"`Sender Account Address` field of the transaction")
7676
txCmd.PersistentFlags().StringVar(&recipientAccountAddress, "recipient", "", "defines the recipient intended for the transaction")
7777
txCmd.PersistentFlags().Int64Var(&fee, "fee", 1, "defines the fee of the transaction")
78+
txCmd.PersistentFlags().BoolVar(&post, "post", false, "post generated bytes to [127.0.0.1:7000](default)")
79+
txCmd.PersistentFlags().StringVar(&postHost, "post-host", "127.0.0.1:7000", "destination of post action")
7880

7981
/*
8082
SendMoney Command

cmd/transaction/const.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ var (
2323
senderSeed string
2424
recipientAccountAddress string
2525
fee int64
26+
post bool
27+
postHost string
2628

2729
// Send money transaction
2830
sendAmount int64

cmd/transaction/generator.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
package transaction
22

33
import (
4+
"context"
45
"database/sql"
56
"encoding/hex"
67
"fmt"
8+
"log"
79
"strings"
810
"time"
911

12+
rpc_model "github.com/zoobc/zoobc-core/common/model"
13+
rpc_service "github.com/zoobc/zoobc-core/common/service"
14+
"google.golang.org/grpc"
15+
1016
"github.com/zoobc/zoobc-core/common/chaintype"
1117
"github.com/zoobc/zoobc-core/common/constant"
1218
"github.com/zoobc/zoobc-core/common/crypto"
@@ -262,7 +268,26 @@ func PrintTx(signedTxBytes []byte, outputType string) {
262268
}
263269
resultStr = strings.Join(byteStrArr, ", ")
264270
}
265-
fmt.Println(resultStr)
271+
if post {
272+
conn, err := grpc.Dial(postHost, grpc.WithInsecure())
273+
if err != nil {
274+
log.Fatalf("did not connect: %s", err)
275+
}
276+
defer conn.Close()
277+
278+
c := rpc_service.NewTransactionServiceClient(conn)
279+
280+
response, err := c.PostTransaction(context.Background(), &rpc_model.PostTransactionRequest{
281+
TransactionBytes: signedTxBytes,
282+
})
283+
if err != nil {
284+
fmt.Printf("post failed: %v\n", err)
285+
} else {
286+
fmt.Printf("\n\nresult: %v\n", response)
287+
}
288+
} else {
289+
fmt.Println(resultStr)
290+
}
266291
}
267292

268293
func GenerateSignedTxBytes(tx *model.Transaction, senderSeed string) []byte {

common/transaction/multiSignature.go

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ package transaction
33
import (
44
"bytes"
55

6+
"github.com/zoobc/zoobc-core/common/crypto"
7+
8+
"github.com/zoobc/zoobc-core/common/blocker"
9+
610
"github.com/zoobc/zoobc-core/common/constant"
711
"github.com/zoobc/zoobc-core/common/fee"
812
"github.com/zoobc/zoobc-core/common/model"
@@ -13,8 +17,11 @@ type (
1317
// MultiSignatureTransaction represent wrapper transaction type that require multiple signer to approve the transcaction
1418
// wrapped
1519
MultiSignatureTransaction struct {
16-
Body *model.MultiSignatureTransactionBody
17-
NormalFee fee.FeeModelInterface
20+
Body *model.MultiSignatureTransactionBody
21+
NormalFee fee.FeeModelInterface
22+
TransactionUtil UtilInterface
23+
TypeSwitcher TypeActionSwitcher
24+
Signature crypto.SignatureInterface
1825
}
1926
)
2027

@@ -31,7 +38,79 @@ func (*MultiSignatureTransaction) UndoApplyUnconfirmed() error {
3138
}
3239

3340
// Validate dbTx specify whether validation should read from transaction state or db state
34-
func (*MultiSignatureTransaction) Validate(dbTx bool) error {
41+
func (tx *MultiSignatureTransaction) Validate(dbTx bool) error {
42+
body := tx.Body
43+
if body.MultiSignatureInfo == nil && body.SignatureInfo == nil && body.UnsignedTransactionBytes == nil {
44+
return blocker.NewBlocker(blocker.ValidationErr, "AtLeastTxBytesSignatureInfoOrMultisignatureInfoMustBe"+
45+
"Provided")
46+
}
47+
if body.MultiSignatureInfo != nil {
48+
if len(body.MultiSignatureInfo.Addresses) < 2 {
49+
return blocker.NewBlocker(
50+
blocker.ValidationErr,
51+
"AtLeastTwoParticipantRequiredForMultisig",
52+
)
53+
}
54+
if body.MultiSignatureInfo.MinimumSignatures < 1 {
55+
return blocker.NewBlocker(
56+
blocker.ValidationErr,
57+
"AtLeastOneSignatureRequiredNeedToBeSet",
58+
)
59+
}
60+
}
61+
if len(body.UnsignedTransactionBytes) > 0 {
62+
innerTx, err := tx.TransactionUtil.ParseTransactionBytes(tx.Body.UnsignedTransactionBytes, false)
63+
if err != nil {
64+
return blocker.NewBlocker(
65+
blocker.ValidationErr,
66+
"FailToParseTransactionBytes",
67+
)
68+
}
69+
innerTa, err := tx.TypeSwitcher.GetTransactionType(innerTx)
70+
if err != nil {
71+
return blocker.NewBlocker(
72+
blocker.ValidationErr,
73+
"FailToCastInnerTransaction",
74+
)
75+
}
76+
err = innerTa.Validate(dbTx)
77+
if err != nil {
78+
return blocker.NewBlocker(
79+
blocker.ValidationErr,
80+
"FailToValidateInnerTa",
81+
)
82+
}
83+
84+
}
85+
if body.SignatureInfo != nil {
86+
if body.SignatureInfo.TransactionHash == nil { // transaction hash has to come with at least one signature
87+
return blocker.NewBlocker(
88+
blocker.ValidationErr,
89+
"TransactionHashRequiredInSignatureInfo",
90+
)
91+
}
92+
if len(body.SignatureInfo.Signatures) < 1 {
93+
return blocker.NewBlocker(
94+
blocker.ValidationErr,
95+
"MinimumOneSignatureRequiredInSignatureInfo",
96+
)
97+
}
98+
for addr, sig := range body.SignatureInfo.Signatures {
99+
if sig == nil {
100+
return blocker.NewBlocker(
101+
blocker.ValidationErr,
102+
"SignatureMissing",
103+
)
104+
}
105+
res := tx.Signature.VerifySignature(body.SignatureInfo.TransactionHash, sig, addr)
106+
if !res {
107+
return blocker.NewBlocker(
108+
blocker.ValidationErr,
109+
"InvalidSignature",
110+
)
111+
}
112+
}
113+
}
35114
return nil
36115
}
37116

0 commit comments

Comments
 (0)