- 
                Notifications
    You must be signed in to change notification settings 
- Fork 0
Feat/near sample #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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") | ||||||||||||||||||||||||||||||||||
| 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
    
   There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 -  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
 
        Suggested change
       
 🤖 Prompt for AI Agents | ||||||||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||||||||
| 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())) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
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:
📝 Committable suggestion
🤖 Prompt for AI Agents