Skip to content

Commit

Permalink
add SignedMsgDirect tx serlz method and CreateNodeTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
jchappelow committed Dec 11, 2024
1 parent 3414a5a commit 82ed46b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
21 changes: 20 additions & 1 deletion core/types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,25 @@ const (
// it needs a signer that signs
SignedMsgConcat SignedMsgSerializationType = "concat"

SignedMsgDirect SignedMsgSerializationType = "direct"

// DefaultSignedMsgSerType is the default serialization type
// It's `concat` for now, since it's the only one known works for every signer
DefaultSignedMsgSerType = SignedMsgConcat
)

// CreateTransaction creates a new unsigned transaction.
func CreateTransaction(contents Payload, chainID string, nonce uint64) (*Transaction, error) {
return createTransaction(contents, chainID, nonce, DefaultSignedMsgSerType)
}

// CreateNodeTransaction creates a new unsigned transaction with the "direct"
// serialization type.
func CreateNodeTransaction(contents Payload, chainID string, nonce uint64) (*Transaction, error) {
return createTransaction(contents, chainID, nonce, SignedMsgDirect)
}

func createTransaction(contents Payload, chainID string, nonce uint64, sert SignedMsgSerializationType) (*Transaction, error) {
data, err := contents.MarshalBinary()
if err != nil {
return nil, err
Expand All @@ -197,7 +209,7 @@ func CreateTransaction(contents Payload, chainID string, nonce uint64) (*Transac
Nonce: nonce,
ChainID: chainID,
},
Serialization: DefaultSignedMsgSerType,
Serialization: sert,
}, nil
}

Expand Down Expand Up @@ -240,6 +252,13 @@ func (t *TransactionBody) SerializeMsg(mst SignedMsgSerializationType) ([]byte,
}

switch mst {
case SignedMsgDirect:
msg, err := t.MarshalBinary()
if err != nil {
return nil, fmt.Errorf("failed to serialize transaction body: %v", err)
}
sigHash := crypto.Sha256(msg) // could just be msg
return sigHash[:], nil
case SignedMsgConcat:
// Make a human-readable message using a template(txMsgToSignTmplV0).
// In this message scheme, the displayed "token" is a hash of the
Expand Down
2 changes: 1 addition & 1 deletion node/services/jsonrpc/adminsvc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ func (svc *Service) sendTx(ctx context.Context, payload ktypes.Payload) (*userjs
return nil, jsonrpc.NewError(jsonrpc.ErrorAccountInternal, "account info error", nil)
}

tx, err := ktypes.CreateTransaction(payload, svc.chainID, uint64(nonce+1))
tx, err := ktypes.CreateNodeTransaction(payload, svc.chainID, uint64(nonce+1))
if err != nil {
return nil, jsonrpc.NewError(jsonrpc.ErrorInternal, "unable to create transaction", nil)
}
Expand Down

0 comments on commit 82ed46b

Please sign in to comment.