Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions wrapper/go-wrapper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ func main() {
sample.ExternalSigningDemo()
sample.TestCardano()
sample.TestCosmos()
sample.TestAptos()

// Aptos wallet
tw, err := core.CreateWalletWithMnemonic(mn, core.CoinTypeAptos)
tw, err := core.CreateWalletWithMnemonic(mn, core.CoinTypeNEAR)
if err != nil {
panic(err)
}
printWallet(tw)
sample.TestAptos()
sample.TestNEAR()
}

func createEthTransaction(ew *core.Wallet) string {
Expand Down
63 changes: 63 additions & 0 deletions wrapper/go-wrapper/sample/near.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package sample

import (
"encoding/hex"
"errors"
"fmt"

"github.com/Cramiumlabs/wallet-core/wrapper/go-wrapper/core"
"github.com/Cramiumlabs/wallet-core/wrapper/go-wrapper/protos/near"
"github.com/Cramiumlabs/wallet-core/wrapper/go-wrapper/protos/transactioncompiler"
"google.golang.org/protobuf/proto"
)

func TestNEAR() {

signatureBytes, _ := hex.DecodeString("969a83332186ee9755e4839325525806e189a3d2d2bb4b4760e94443e97e1c4f22deeef0059a8e9713100eda6e19144da7e8a0ef7e539b20708ba1d8d021bd01")
blockHashBytes, _ := hex.DecodeString("0fa473fd26901df296be6adc4cc4df34d040efa2435224b6986910e630c2fef6")
pubKeyBytes, _ := hex.DecodeString("917b3d268d4b58f7fec1b150bd68d69be3ee5d4cc39855e341538465bb77860d")
Comment on lines +16 to +18
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve error handling for hex decoding operations.

The code ignores potential errors from hex.DecodeString() operations using the blank identifier _. This could lead to silent failures if the hardcoded hex strings are malformed.

Consider handling these errors explicitly:

-	signatureBytes, _ := hex.DecodeString("969a83332186ee9755e4839325525806e189a3d2d2bb4b4760e94443e97e1c4f22deeef0059a8e9713100eda6e19144da7e8a0ef7e539b20708ba1d8d021bd01")
-	blockHashBytes, _ := hex.DecodeString("0fa473fd26901df296be6adc4cc4df34d040efa2435224b6986910e630c2fef6")
-	pubKeyBytes, _ := hex.DecodeString("917b3d268d4b58f7fec1b150bd68d69be3ee5d4cc39855e341538465bb77860d")
+	signatureBytes, err := hex.DecodeString("969a83332186ee9755e4839325525806e189a3d2d2bb4b4760e94443e97e1c4f22deeef0059a8e9713100eda6e19144da7e8a0ef7e539b20708ba1d8d021bd01")
+	if err != nil {
+		panic(fmt.Errorf("failed to decode signature: %w", err))
+	}
+	blockHashBytes, err := hex.DecodeString("0fa473fd26901df296be6adc4cc4df34d040efa2435224b6986910e630c2fef6")
+	if err != nil {
+		panic(fmt.Errorf("failed to decode block hash: %w", err))
+	}
+	pubKeyBytes, err := hex.DecodeString("917b3d268d4b58f7fec1b150bd68d69be3ee5d4cc39855e341538465bb77860d")
+	if err != nil {
+		panic(fmt.Errorf("failed to decode public key: %w", err))
+	}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
signatureBytes, _ := hex.DecodeString("969a83332186ee9755e4839325525806e189a3d2d2bb4b4760e94443e97e1c4f22deeef0059a8e9713100eda6e19144da7e8a0ef7e539b20708ba1d8d021bd01")
blockHashBytes, _ := hex.DecodeString("0fa473fd26901df296be6adc4cc4df34d040efa2435224b6986910e630c2fef6")
pubKeyBytes, _ := hex.DecodeString("917b3d268d4b58f7fec1b150bd68d69be3ee5d4cc39855e341538465bb77860d")
signatureBytes, err := hex.DecodeString("969a83332186ee9755e4839325525806e189a3d2d2bb4b4760e94443e97e1c4f22deeef0059a8e9713100eda6e19144da7e8a0ef7e539b20708ba1d8d021bd01")
if err != nil {
panic(fmt.Errorf("failed to decode signature: %w", err))
}
blockHashBytes, err := hex.DecodeString("0fa473fd26901df296be6adc4cc4df34d040efa2435224b6986910e630c2fef6")
if err != nil {
panic(fmt.Errorf("failed to decode block hash: %w", err))
}
pubKeyBytes, err := hex.DecodeString("917b3d268d4b58f7fec1b150bd68d69be3ee5d4cc39855e341538465bb77860d")
if err != nil {
panic(fmt.Errorf("failed to decode public key: %w", err))
}
🤖 Prompt for AI Agents
In wrapper/go-wrapper/sample/near.go around lines 16 to 18, the hex.DecodeString
calls ignore errors by assigning them to the blank identifier, which risks
silent failures if the hex strings are malformed. Modify the code to capture and
check the error returned by each hex.DecodeString call, and handle any errors
appropriately, such as returning the error or logging it, to ensure any decoding
issues are detected and managed.

var transferAmount [16]byte
transferAmount[0] = 1

fmt.Println("hex amount:", hex.EncodeToString(transferAmount[:]))

input := near.SigningInput{
SignerId: "test.near",
ReceiverId: "whatever.near",
BlockHash: blockHashBytes,
Nonce: 1,
PublicKey: pubKeyBytes,
Actions: []*near.Action{
&near.Action{
Payload: &near.Action_Transfer{
Transfer: &near.Transfer{
Deposit: transferAmount[:],
},
},
},
},
}
txInputData, _ := proto.Marshal(&input)
msgForSign := core.PreImageHashes(core.CoinTypeNEAR, txInputData)

var preSigningOutput transactioncompiler.PreSigningOutput
proto.Unmarshal(msgForSign, &preSigningOutput)
Comment on lines +40 to +44
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add consistent error handling for protobuf operations.

The code has inconsistent error handling - proto.Marshal at line 40 ignores errors while proto.Unmarshal at line 57 handles them. Both operations should have proper error handling for robustness.

Apply this diff to add missing error handling:

-	txInputData, _ := proto.Marshal(&input)
+	txInputData, err := proto.Marshal(&input)
+	if err != nil {
+		panic(fmt.Errorf("failed to marshal signing input: %w", err))
+	}
 	msgForSign := core.PreImageHashes(core.CoinTypeNEAR, txInputData)
 
 	var preSigningOutput transactioncompiler.PreSigningOutput
-	proto.Unmarshal(msgForSign, &preSigningOutput)
+	err = proto.Unmarshal(msgForSign, &preSigningOutput)
+	if err != nil {
+		panic(fmt.Errorf("failed to unmarshal pre-signing output: %w", err))
+	}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
txInputData, _ := proto.Marshal(&input)
msgForSign := core.PreImageHashes(core.CoinTypeNEAR, txInputData)
var preSigningOutput transactioncompiler.PreSigningOutput
proto.Unmarshal(msgForSign, &preSigningOutput)
txInputData, err := proto.Marshal(&input)
if err != nil {
panic(fmt.Errorf("failed to marshal signing input: %w", err))
}
msgForSign := core.PreImageHashes(core.CoinTypeNEAR, txInputData)
var preSigningOutput transactioncompiler.PreSigningOutput
err = proto.Unmarshal(msgForSign, &preSigningOutput)
if err != nil {
panic(fmt.Errorf("failed to unmarshal pre-signing output: %w", err))
}
🤖 Prompt for AI Agents
In wrapper/go-wrapper/sample/near.go around lines 40 to 44, the proto.Marshal
call ignores errors while proto.Unmarshal handles them inconsistently. Modify
the code to capture and check errors returned by proto.Marshal and
proto.Unmarshal, returning or handling the error appropriately to ensure robust
error handling for both protobuf operations.


fmt.Println("sigHash:", hex.EncodeToString(preSigningOutput.DataHash))

valid := core.PublicKeyVerify(pubKeyBytes, core.PublicKeyTypeED25519, signatureBytes, preSigningOutput.DataHash)
if !valid {
panic(errors.New("verification failed"))
}
fmt.Println("Signature verification successfully")

txOutput := core.CompileWithSignatures(core.CoinTypeNEAR, txInputData, [][]byte{signatureBytes}, [][]byte{pubKeyBytes})

var output near.SigningOutput
err := proto.Unmarshal(txOutput, &output)
if err != nil {
panic(errors.New("unmarshal output failed"))
}
fmt.Println("Message for broadcast:", hex.EncodeToString(output.SignedTransaction))
fmt.Println("Transaction hash:", hex.EncodeToString(output.GetHash()))
}
Loading