Skip to content

Commit

Permalink
style: improve code formating
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyasbhat0 committed Jul 19, 2024
1 parent d7b3058 commit b16f92c
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 71 deletions.
5 changes: 4 additions & 1 deletion x/claim/client/offchain/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ func CmdSignMessage() *cobra.Command {
privateKey := args[1]
signedData := &SignedData{}

signedData.sign(data, privateKey)
err := signedData.sign(data, privateKey)
if err != nil {
return err
}

signedDataString, err := signedData.getSignedDataString()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions x/claim/client/offchain/sign.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package offchain

import (
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"encoding/json"
"fmt"
Expand All @@ -22,7 +22,7 @@ func (s *SignedData) sign(data string, privateKey string) error {
}

privKey := secp256k1.PrivKey{Key: privKeyBytes}
hash := sha256.Sum256([]byte(data))
hash := sha512.Sum512([]byte(data))
signature, err := privKey.Sign(hash[:])
if err != nil {
return err
Expand Down
155 changes: 89 additions & 66 deletions x/claim/keeper/msg_server_claim_thorchain.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package keeper

import (
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"encoding/json"
Expand All @@ -18,79 +17,20 @@ import (
// Verify and update the claim record based on the memo of the thorchain tx
func (k msgServer) updateThorClaimRecord(ctx sdk.Context, creator string, thorTxMsg *types.MsgThorTxData, arkeoClaimRecord types.ClaimRecord) (types.ClaimRecord, error) {

// decode thorTxMessage
thorTxMsgDecoded, err := hex.DecodeString(thorTxMsg.ThorData)
thorTxChainData, thorTxEncodedData, err := decodeAndUnmarshallThorTxMsg(thorTxMsg)
if err != nil {
return types.ClaimRecord{}, fmt.Errorf("error hex decoding faild: %w", err)
}

// unmarshall encoded data
var thorTxEncodedData *types.ThorTxData
if err := json.Unmarshal(thorTxMsgDecoded, &thorTxEncodedData); err != nil {
return types.ClaimRecord{}, fmt.Errorf("error unmarshalling transaction data: %w", err)
return types.ClaimRecord{}, err
}

// decode tx data
thorTxDataDecoded, err := hex.DecodeString(thorTxEncodedData.TxData)
verifyTxDataHash, txDataHex, err := verifyTxDataCheckSum(thorTxChainData, thorTxEncodedData)
if err != nil {
return types.ClaimRecord{}, fmt.Errorf("error hex decoding failed: %w", err)
return types.ClaimRecord{}, err
}

// unmarshall thor tx
var thorTxChainData *types.ThorChainTxData
if err := json.Unmarshal(thorTxDataDecoded, &thorTxChainData); err != nil {
return types.ClaimRecord{}, fmt.Errorf("error unmarshalling transaction data: %w", err)
}
err = verifySignature(thorTxMsg, verifyTxDataHash, txDataHex)

// For Reverification of Hash
thorTxChainDataBytes, err := json.Marshal(thorTxChainData)
if err != nil {
return types.ClaimRecord{}, fmt.Errorf("error marshalling tx data: %w", err)
}

txDataHash := sha512.Sum512(thorTxChainDataBytes)
verifyTxDataHash := hex.EncodeToString(txDataHash[:])
txDataHex := hex.EncodeToString(thorTxChainDataBytes)

// Verify Data Check Sum
if verifyTxDataHash != thorTxEncodedData.Hash {
return types.ClaimRecord{}, fmt.Errorf("transaction data cehcksum failed")
}

txData := types.ThorTxData{
Hash: verifyTxDataHash,
TxData: txDataHex,
}

txDataBytes, err := json.Marshal(txData)
if err != nil {
return types.ClaimRecord{}, fmt.Errorf("error marshalling txData: %w", err)
}

txDataHashForVerification := hex.EncodeToString(txDataBytes)
if err != nil {
return types.ClaimRecord{}, fmt.Errorf("error hex decoding faild: %w", err)
}

// verify the tx signature
proofPubKeyDecoded, err := hex.DecodeString(thorTxMsg.ProofPubkey)
if err != nil {
return types.ClaimRecord{}, fmt.Errorf("failed to decode pub key: %w", err)
}
pubkey := crypto.PubKey{}
pubkey.Key = proofPubKeyDecoded

proofSignatureDecoded, err := hex.DecodeString(thorTxMsg.ProofSignature)
if err != nil {
return types.ClaimRecord{}, fmt.Errorf("failed to decode signature key: %w", err)
}

// create data to verify

hash := sha256.Sum256([]byte(txDataHashForVerification))

if !pubkey.VerifySignature(hash[:], proofSignatureDecoded[:]) {
return types.ClaimRecord{}, fmt.Errorf("message verification failed")
return types.ClaimRecord{}, err
}

thorAddress := thorTxChainData.ObservedTx.Tx.FromAddress
Expand Down Expand Up @@ -149,3 +89,86 @@ func (k msgServer) updateThorClaimRecord(ctx sdk.Context, creator string, thorTx
}
return newClaimRecord, nil
}

func decodeAndUnmarshallThorTxMsg(thorTxMsg *types.MsgThorTxData) (*types.ThorChainTxData, *types.ThorTxData, error) {
// decode thorTxMessage
thorTxMsgDecoded, err := hex.DecodeString(thorTxMsg.ThorData)
if err != nil {
return nil, nil, fmt.Errorf("error hex decoding faild: %w", err)
}

// unmarshall encoded data
var thorTxEncodedData *types.ThorTxData
if err := json.Unmarshal(thorTxMsgDecoded, &thorTxEncodedData); err != nil {
return nil, nil, fmt.Errorf("error unmarshalling transaction data: %w", err)
}

// decode tx data
thorTxDataDecoded, err := hex.DecodeString(thorTxEncodedData.TxData)
if err != nil {
return nil, nil, fmt.Errorf("error hex decoding failed: %w", err)
}

// unmarshall thor tx
var thorTxChainData *types.ThorChainTxData
if err := json.Unmarshal(thorTxDataDecoded, &thorTxChainData); err != nil {
return nil, nil, fmt.Errorf("error unmarshalling transaction data: %w", err)
}

return thorTxChainData, thorTxEncodedData, nil
}

func verifyTxDataCheckSum(thorTxChainData *types.ThorChainTxData, thorTxEncodedData *types.ThorTxData) (string, string, error) {
thorTxChainDataBytes, err := json.Marshal(thorTxChainData)
if err != nil {
return "", "", fmt.Errorf("error marshalling tx data: %w", err)
}

txDataHash := sha512.Sum512(thorTxChainDataBytes)
verifyTxDataHash := hex.EncodeToString(txDataHash[:])
txDataHex := hex.EncodeToString(thorTxChainDataBytes)

// Verify Data Check Sum
if verifyTxDataHash != thorTxEncodedData.Hash {
return "", "", fmt.Errorf("transaction data cehcksum failed")
}

return verifyTxDataHash, txDataHex, nil

}

func verifySignature(thorTxMsg *types.MsgThorTxData, verifyTxDataHash, txDataHex string) error {
txData := types.ThorTxData{
Hash: verifyTxDataHash,
TxData: txDataHex,
}

txDataBytes, err := json.Marshal(txData)
if err != nil {
return fmt.Errorf("error marshalling txData: %w", err)
}

txDataHashForVerification := hex.EncodeToString(txDataBytes)

// verify the tx signature
proofPubKeyDecoded, err := hex.DecodeString(thorTxMsg.ProofPubkey)
if err != nil {
return fmt.Errorf("failed to decode pub key: %w", err)
}
pubkey := crypto.PubKey{}
pubkey.Key = proofPubKeyDecoded

proofSignatureDecoded, err := hex.DecodeString(thorTxMsg.ProofSignature)
if err != nil {
return fmt.Errorf("failed to decode signature key: %w", err)
}

// create data to verify
hash := sha512.Sum512([]byte(txDataHashForVerification))

if !pubkey.VerifySignature(hash[:], proofSignatureDecoded[:]) {
return fmt.Errorf("message verification failed")
}

return nil
}
4 changes: 2 additions & 2 deletions x/claim/keeper/msg_server_claim_thorchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestClaimThorchainArkeo(t *testing.T) {
Creator: addrArkeo,
ThorTxData: &types.MsgThorTxData{
ThorData: "7b2268617368223a223133373430646435623638613938356662386364333464323737353230373039643637653065623939633665356631663430333036366233393662336566656533306138663765653539303165663432313535393036636561626439356538393834323132353439643235336536303034346133366361643934346538383835222c2274785f64617461223a223762323236663632373336353732373636353634356637343738323233613762323237343738323233613762323236393634323233613232343634313332333733363338343134353432333533323431343533303431333333373338333333373332343233343338343233313330343333353432333333373334343233323335343533383432333233303330333534333337333033323431343134343334333433313432333833313333343534343332343633313337333432323263323236333638363136393665323233613232353434383466353232323263323236363732366636643566363136343634373236353733373332323361323237343638366637323331363436633663363637393730333533373663333437383661333537353664373136363633373137393336363333323663333337383636366233303731366233363637373236343334366133383232326332323734366635663631363436343732363537333733323233613232373436383666373233313637333933383633373933333665333936643664366137323730366533303733373836643665333633333663376137343635366336353732363133333337366533383665333633373633333032323263323236333666363936653733323233613562376232323631373337333635373432323361323235343438346635323265353235353465343532323263323236313664366637353665373432323361323233303232376435643263323236373631373332323361366537353663366332633232366436353664366632323361323236343635366336353637363137343635336136313732366236353666336137343631373236623635366633313339333333353338376133323336366137373638333336353334373236343336373037333738373136363338373133363636333337303635333636363338373333373736333037383332363132323764376432633232363336663665373336353665373337353733356636383635363936373638373432323361333133353331333833333334333333303263323236363639366536313663363937333635363435663638363536393637363837343232336133313335333133383333333433333330326332323662363537393733363936373665356636643635373437323639363332323361376232323734373835663639363432323361323234363431333233373336333834313435343233353332343134353330343133333337333833333337333234323334333834323331333034333335343233333337333434323332333534353338343233323330333033353433333733303332343134313434333433343331343233383331333334353434333234363331333733343232326332323665366636343635356637343733373335663734363936643635373332323361366537353663366337643764227d",
ProofSignature: "8af1915a046a5b3a11a1c4bf5f8f30f6e05a590a1b3361f69ee8797dd4e6a3ad7679d7fcf359c500cf71d645a215c888ab3e39b8082b2c5975ad5ed8d5004c44",
ProofSignature: "2d1d9ae3ed08a91bad0a25368dcfa0420d77313d5a627becee1784f63de009c53490953c14ba4c1cdc35354dff85b48fd1ef6540bf6cd9ada4cd4ee80fa9bbf2",
ProofPubkey: "020C8FF0D34D4B5EE779540ECA039B1DAC0F8EDFE9BE6EC233AA4B4FF8DE6EBF86",
},
}
Expand Down Expand Up @@ -138,7 +138,7 @@ func TestClaimThorchainEth(t *testing.T) {
Signature: sigString,
ThorTx: &types.MsgThorTxData{
ThorData: "7b2268617368223a223133373430646435623638613938356662386364333464323737353230373039643637653065623939633665356631663430333036366233393662336566656533306138663765653539303165663432313535393036636561626439356538393834323132353439643235336536303034346133366361643934346538383835222c2274785f64617461223a223762323236663632373336353732373636353634356637343738323233613762323237343738323233613762323236393634323233613232343634313332333733363338343134353432333533323431343533303431333333373338333333373332343233343338343233313330343333353432333333373334343233323335343533383432333233303330333534333337333033323431343134343334333433313432333833313333343534343332343633313337333432323263323236333638363136393665323233613232353434383466353232323263323236363732366636643566363136343634373236353733373332323361323237343638366637323331363436633663363637393730333533373663333437383661333537353664373136363633373137393336363333323663333337383636366233303731366233363637373236343334366133383232326332323734366635663631363436343732363537333733323233613232373436383666373233313637333933383633373933333665333936643664366137323730366533303733373836643665333633333663376137343635366336353732363133333337366533383665333633373633333032323263323236333666363936653733323233613562376232323631373337333635373432323361323235343438346635323265353235353465343532323263323236313664366637353665373432323361323233303232376435643263323236373631373332323361366537353663366332633232366436353664366632323361323236343635366336353637363137343635336136313732366236353666336137343631373236623635366633313339333333353338376133323336366137373638333336353334373236343336373037333738373136363338373133363636333337303635333636363338373333373736333037383332363132323764376432633232363336663665373336353665373337353733356636383635363936373638373432323361333133353331333833333334333333303263323236363639366536313663363937333635363435663638363536393637363837343232336133313335333133383333333433333330326332323662363537393733363936373665356636643635373437323639363332323361376232323734373835663639363432323361323234363431333233373336333834313435343233353332343134353330343133333337333833333337333234323334333834323331333034333335343233333337333434323332333534353338343233323330333033353433333733303332343134313434333433343331343233383331333334353434333234363331333733343232326332323665366636343635356637343733373335663734363936643635373332323361366537353663366337643764227d",
ProofSignature: "8af1915a046a5b3a11a1c4bf5f8f30f6e05a590a1b3361f69ee8797dd4e6a3ad7679d7fcf359c500cf71d645a215c888ab3e39b8082b2c5975ad5ed8d5004c44",
ProofSignature: "2d1d9ae3ed08a91bad0a25368dcfa0420d77313d5a627becee1784f63de009c53490953c14ba4c1cdc35354dff85b48fd1ef6540bf6cd9ada4cd4ee80fa9bbf2",
ProofPubkey: "020C8FF0D34D4B5EE779540ECA039B1DAC0F8EDFE9BE6EC233AA4B4FF8DE6EBF86",
},
}
Expand Down

0 comments on commit b16f92c

Please sign in to comment.