Skip to content
Merged
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
2 changes: 2 additions & 0 deletions wrapper/go-wrapper/core/coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const (
CoinTypeCosmos CoinType = C.TWCoinTypeCosmos
CoinTypeAptos CoinType = C.TWCoinTypeAptos
CoinTypeNEAR CoinType = C.TWCoinTypeNEAR
CoinTypeICP CoinType = C.TWCoinTypeInternetComputer
CoinTypeStellar CoinType = C.TWCoinTypeStellar
)

func (c CoinType) GetName() string {
Expand Down
4 changes: 4 additions & 0 deletions wrapper/go-wrapper/core/stellar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package core

const StellarPassphrase_Stellar string = "Public Global Stellar Network ; September 2015"
const StellarPassphrase_Kin string = "Kin Mainnet ; December 2018"
10 changes: 10 additions & 0 deletions wrapper/go-wrapper/core/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package core

import "fmt"

func WalletInfo(w *Wallet) {
fmt.Printf("%s wallet: \n", w.CoinType.GetName())
fmt.Printf("\t address: %s \n", w.Address)
fmt.Printf("\t pri key: %s \n", w.PriKey)
fmt.Printf("\t pub key: %s \n", w.PubKey)
}
Comment on lines +5 to +10
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix compilation error: undefined Wallet type.

The Wallet type is not defined or imported in this package, causing a compilation error as indicated by the static analysis tool.

The Wallet type needs to be imported or this function should be moved to a package where Wallet is defined. Looking at the codebase, Wallet appears to be defined in the same core package, so you may need to check if there's a missing file or if the Wallet struct definition needs to be added.

Additionally, this function duplicates the printWallet function from main.go. Consider consolidating this functionality.

+// Add the Wallet struct definition or import it from the appropriate location
 func WalletInfo(w *Wallet) {
 	fmt.Printf("%s wallet: \n", w.CoinType.GetName())
 	fmt.Printf("\t address: %s \n", w.Address)
 	fmt.Printf("\t pri key: %s \n", w.PriKey)
 	fmt.Printf("\t pub key: %s \n", w.PubKey)
 }

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In wrapper/go-wrapper/core/utils.go around lines 5 to 10, the Wallet type is
undefined causing a compilation error. Verify if the Wallet struct is defined
elsewhere in the core package or if its definition is missing and add it here if
needed. Alternatively, move this WalletInfo function to the package where Wallet
is defined. Also, consider removing this duplicate function and consolidating
with the existing printWallet function in main.go to avoid redundancy.

2 changes: 2 additions & 0 deletions wrapper/go-wrapper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ func main() {
}
printWallet(tw)
sample.TestAptos()
sample.TestICP()
sample.TestStellar()
}

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

import (
"encoding/hex"
"fmt"

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

func TestICP() {
mn := "confirm bleak useless tail chalk destroy horn step bulb genuine attract split"

fmt.Println("==> mnemonic is valid: ", core.IsMnemonicValid(mn))

// bitcoin wallet
bw, err := core.CreateWalletWithMnemonic(mn, core.CoinTypeICP)
if err != nil {
panic(err)
}
core.WalletInfo(bw)

input := &internetcomputer.SigningInput{
Transaction: &internetcomputer.Transaction{
TransactionOneof: &internetcomputer.Transaction_Transfer_{
Transfer: &internetcomputer.Transaction_Transfer{
ToAccountIdentifier: "943d12e762f43806782f524b8f90297298a6d79e4749b41b585ec427409c826a",
Amount: 100000000,
Memo: 0,
CurrentTimestampNanos: 1691709940000000000,
},
},
},
}
fmt.Println("input:", input)

txInputData, _ := proto.Marshal(input)
msgForSign := core.PreImageHashes(core.CoinTypeICP, txInputData)

fmt.Println("msgForSign:", msgForSign)

var preSigningOutput transactioncompiler.PreSigningOutput
proto.Unmarshal(msgForSign, &preSigningOutput)
Comment on lines +39 to +45
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 error handling for protobuf operations.

The proto.Marshal and proto.Unmarshal operations can fail but errors are currently ignored, which could lead to runtime panics or unexpected behavior.

-	txInputData, _ := proto.Marshal(input)
+	txInputData, err := proto.Marshal(input)
+	if err != nil {
+		panic(err)
+	}
 	msgForSign := core.PreImageHashes(core.CoinTypeICP, txInputData)
 
 	fmt.Println("msgForSign:", msgForSign)
 
 	var preSigningOutput transactioncompiler.PreSigningOutput
-	proto.Unmarshal(msgForSign, &preSigningOutput)
+	err = proto.Unmarshal(msgForSign, &preSigningOutput)
+	if err != nil {
+		panic(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.CoinTypeICP, txInputData)
fmt.Println("msgForSign:", msgForSign)
var preSigningOutput transactioncompiler.PreSigningOutput
proto.Unmarshal(msgForSign, &preSigningOutput)
txInputData, err := proto.Marshal(input)
if err != nil {
panic(err)
}
msgForSign := core.PreImageHashes(core.CoinTypeICP, txInputData)
fmt.Println("msgForSign:", msgForSign)
var preSigningOutput transactioncompiler.PreSigningOutput
err = proto.Unmarshal(msgForSign, &preSigningOutput)
if err != nil {
panic(err)
}
🤖 Prompt for AI Agents
In wrapper/go-wrapper/sample/icp.go around lines 39 to 45, the proto.Marshal and
proto.Unmarshal calls ignore errors, risking runtime panics or unexpected
behavior. Modify the code to capture and check the errors returned by
proto.Marshal and proto.Unmarshal. If an error occurs, handle it appropriately
by returning the error or logging it, ensuring the program does not proceed with
invalid data.


fmt.Println("preSigningOutput:", &preSigningOutput)

fmt.Println("sigHash:", hex.EncodeToString(preSigningOutput.DataHash))
}
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")
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)

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()))
}
57 changes: 57 additions & 0 deletions wrapper/go-wrapper/sample/stellar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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/stellar"
"github.com/Cramiumlabs/wallet-core/wrapper/go-wrapper/protos/transactioncompiler"
"google.golang.org/protobuf/proto"
)

func TestStellar() {
input := &stellar.SigningInput{
Passphrase: core.StellarPassphrase_Stellar,
Account: "GAE2SZV4VLGBAPRYRFV2VY7YYLYGYIP5I7OU7BSP6DJT7GAZ35OKFDYI",
Fee: 1000,
Sequence: 2,
OperationOneof: &stellar.SigningInput_OpPayment{
OpPayment: &stellar.OperationPayment{
Destination: "GDCYBNRRPIHLHG7X7TKPUPAZ7WVUXCN3VO7WCCK64RIFV5XM5V5K4A52",
Amount: 10000000,
},
},
MemoTypeOneof: &stellar.SigningInput_MemoText{
MemoText: &stellar.MemoText{
Text: "Hello, world!",
},
},
}

txInputData, _ := proto.Marshal(input)
preimage := core.PreImageHashes(core.CoinTypeStellar, txInputData)

var preSigningOutput transactioncompiler.PreSigningOutput
proto.Unmarshal(preimage, &preSigningOutput)
fmt.Println("[SigHash]:", hex.EncodeToString(preSigningOutput.DataHash))

signature, _ := hex.DecodeString("5042574491827aaccbce1e2964c05098caba06194beb35e595aabfec9f788516a833f755f18144f4a2eedb3123d180f44e7c16037d00857c5c5b7033ebac2c01")
pubkey, _ := hex.DecodeString("09a966bcaacc103e38896baae3f8c2f06c21fd47dd4f864ff0d33f9819df5ca2")

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

txOutput := core.CompileWithSignatures(core.CoinTypeStellar, txInputData, [][]byte{signature}, [][]byte{pubkey})

var output stellar.SigningOutput
err := proto.Unmarshal(txOutput, &output)
if err != nil {
panic(errors.New("unmarshal output failed"))
}
fmt.Println("Message for broadcast:", &output)
}
Loading