Skip to content

Commit a20cb74

Browse files
authored
Merge pull request #8 from Cramiumlabs/feat/stellar
go-wrapper for Stellar
2 parents d42b875 + 3588c59 commit a20cb74

File tree

7 files changed

+188
-0
lines changed

7 files changed

+188
-0
lines changed

wrapper/go-wrapper/core/coin.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ const (
2121
CoinTypeCosmos CoinType = C.TWCoinTypeCosmos
2222
CoinTypeAptos CoinType = C.TWCoinTypeAptos
2323
CoinTypeNEAR CoinType = C.TWCoinTypeNEAR
24+
CoinTypeICP CoinType = C.TWCoinTypeInternetComputer
25+
CoinTypeStellar CoinType = C.TWCoinTypeStellar
2426
)
2527

2628
func (c CoinType) GetName() string {

wrapper/go-wrapper/core/stellar.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package core
2+
3+
const StellarPassphrase_Stellar string = "Public Global Stellar Network ; September 2015"
4+
const StellarPassphrase_Kin string = "Kin Mainnet ; December 2018"

wrapper/go-wrapper/core/utils.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package core
2+
3+
import "fmt"
4+
5+
func WalletInfo(w *Wallet) {
6+
fmt.Printf("%s wallet: \n", w.CoinType.GetName())
7+
fmt.Printf("\t address: %s \n", w.Address)
8+
fmt.Printf("\t pri key: %s \n", w.PriKey)
9+
fmt.Printf("\t pub key: %s \n", w.PubKey)
10+
}

wrapper/go-wrapper/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ func main() {
5555
}
5656
printWallet(tw)
5757
sample.TestAptos()
58+
sample.TestICP()
59+
sample.TestStellar()
5860
}
5961

6062
func createEthTransaction(ew *core.Wallet) string {

wrapper/go-wrapper/sample/icp.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package sample
2+
3+
import (
4+
"encoding/hex"
5+
"fmt"
6+
7+
"github.com/Cramiumlabs/wallet-core/wrapper/go-wrapper/core"
8+
"github.com/Cramiumlabs/wallet-core/wrapper/go-wrapper/protos/internetcomputer"
9+
"github.com/Cramiumlabs/wallet-core/wrapper/go-wrapper/protos/transactioncompiler"
10+
"google.golang.org/protobuf/proto"
11+
)
12+
13+
func TestICP() {
14+
mn := "confirm bleak useless tail chalk destroy horn step bulb genuine attract split"
15+
16+
fmt.Println("==> mnemonic is valid: ", core.IsMnemonicValid(mn))
17+
18+
// bitcoin wallet
19+
bw, err := core.CreateWalletWithMnemonic(mn, core.CoinTypeICP)
20+
if err != nil {
21+
panic(err)
22+
}
23+
core.WalletInfo(bw)
24+
25+
input := &internetcomputer.SigningInput{
26+
Transaction: &internetcomputer.Transaction{
27+
TransactionOneof: &internetcomputer.Transaction_Transfer_{
28+
Transfer: &internetcomputer.Transaction_Transfer{
29+
ToAccountIdentifier: "943d12e762f43806782f524b8f90297298a6d79e4749b41b585ec427409c826a",
30+
Amount: 100000000,
31+
Memo: 0,
32+
CurrentTimestampNanos: 1691709940000000000,
33+
},
34+
},
35+
},
36+
}
37+
fmt.Println("input:", input)
38+
39+
txInputData, _ := proto.Marshal(input)
40+
msgForSign := core.PreImageHashes(core.CoinTypeICP, txInputData)
41+
42+
fmt.Println("msgForSign:", msgForSign)
43+
44+
var preSigningOutput transactioncompiler.PreSigningOutput
45+
proto.Unmarshal(msgForSign, &preSigningOutput)
46+
47+
fmt.Println("preSigningOutput:", &preSigningOutput)
48+
49+
fmt.Println("sigHash:", hex.EncodeToString(preSigningOutput.DataHash))
50+
}

wrapper/go-wrapper/sample/near.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package sample
2+
3+
import (
4+
"encoding/hex"
5+
"errors"
6+
"fmt"
7+
8+
"github.com/Cramiumlabs/wallet-core/wrapper/go-wrapper/core"
9+
"github.com/Cramiumlabs/wallet-core/wrapper/go-wrapper/protos/near"
10+
"github.com/Cramiumlabs/wallet-core/wrapper/go-wrapper/protos/transactioncompiler"
11+
"google.golang.org/protobuf/proto"
12+
)
13+
14+
func TestNEAR() {
15+
16+
signatureBytes, _ := hex.DecodeString("969a83332186ee9755e4839325525806e189a3d2d2bb4b4760e94443e97e1c4f22deeef0059a8e9713100eda6e19144da7e8a0ef7e539b20708ba1d8d021bd01")
17+
blockHashBytes, _ := hex.DecodeString("0fa473fd26901df296be6adc4cc4df34d040efa2435224b6986910e630c2fef6")
18+
pubKeyBytes, _ := hex.DecodeString("917b3d268d4b58f7fec1b150bd68d69be3ee5d4cc39855e341538465bb77860d")
19+
var transferAmount [16]byte
20+
transferAmount[0] = 1
21+
22+
fmt.Println("hex amount:", hex.EncodeToString(transferAmount[:]))
23+
24+
input := near.SigningInput{
25+
SignerId: "test.near",
26+
ReceiverId: "whatever.near",
27+
BlockHash: blockHashBytes,
28+
Nonce: 1,
29+
PublicKey: pubKeyBytes,
30+
Actions: []*near.Action{
31+
&near.Action{
32+
Payload: &near.Action_Transfer{
33+
Transfer: &near.Transfer{
34+
Deposit: transferAmount[:],
35+
},
36+
},
37+
},
38+
},
39+
}
40+
txInputData, _ := proto.Marshal(&input)
41+
msgForSign := core.PreImageHashes(core.CoinTypeNEAR, txInputData)
42+
43+
var preSigningOutput transactioncompiler.PreSigningOutput
44+
proto.Unmarshal(msgForSign, &preSigningOutput)
45+
46+
fmt.Println("sigHash:", hex.EncodeToString(preSigningOutput.DataHash))
47+
48+
valid := core.PublicKeyVerify(pubKeyBytes, core.PublicKeyTypeED25519, signatureBytes, preSigningOutput.DataHash)
49+
if !valid {
50+
panic(errors.New("verification failed"))
51+
}
52+
fmt.Println("Signature verification successfully")
53+
54+
txOutput := core.CompileWithSignatures(core.CoinTypeNEAR, txInputData, [][]byte{signatureBytes}, [][]byte{pubKeyBytes})
55+
56+
var output near.SigningOutput
57+
err := proto.Unmarshal(txOutput, &output)
58+
if err != nil {
59+
panic(errors.New("unmarshal output failed"))
60+
}
61+
fmt.Println("Message for broadcast:", hex.EncodeToString(output.SignedTransaction))
62+
fmt.Println("Transaction hash:", hex.EncodeToString(output.GetHash()))
63+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package sample
2+
3+
import (
4+
"encoding/hex"
5+
"errors"
6+
"fmt"
7+
8+
"github.com/Cramiumlabs/wallet-core/wrapper/go-wrapper/core"
9+
"github.com/Cramiumlabs/wallet-core/wrapper/go-wrapper/protos/stellar"
10+
"github.com/Cramiumlabs/wallet-core/wrapper/go-wrapper/protos/transactioncompiler"
11+
"google.golang.org/protobuf/proto"
12+
)
13+
14+
func TestStellar() {
15+
input := &stellar.SigningInput{
16+
Passphrase: core.StellarPassphrase_Stellar,
17+
Account: "GAE2SZV4VLGBAPRYRFV2VY7YYLYGYIP5I7OU7BSP6DJT7GAZ35OKFDYI",
18+
Fee: 1000,
19+
Sequence: 2,
20+
OperationOneof: &stellar.SigningInput_OpPayment{
21+
OpPayment: &stellar.OperationPayment{
22+
Destination: "GDCYBNRRPIHLHG7X7TKPUPAZ7WVUXCN3VO7WCCK64RIFV5XM5V5K4A52",
23+
Amount: 10000000,
24+
},
25+
},
26+
MemoTypeOneof: &stellar.SigningInput_MemoText{
27+
MemoText: &stellar.MemoText{
28+
Text: "Hello, world!",
29+
},
30+
},
31+
}
32+
33+
txInputData, _ := proto.Marshal(input)
34+
preimage := core.PreImageHashes(core.CoinTypeStellar, txInputData)
35+
36+
var preSigningOutput transactioncompiler.PreSigningOutput
37+
proto.Unmarshal(preimage, &preSigningOutput)
38+
fmt.Println("[SigHash]:", hex.EncodeToString(preSigningOutput.DataHash))
39+
40+
signature, _ := hex.DecodeString("5042574491827aaccbce1e2964c05098caba06194beb35e595aabfec9f788516a833f755f18144f4a2eedb3123d180f44e7c16037d00857c5c5b7033ebac2c01")
41+
pubkey, _ := hex.DecodeString("09a966bcaacc103e38896baae3f8c2f06c21fd47dd4f864ff0d33f9819df5ca2")
42+
43+
valid := core.PublicKeyVerify(pubkey, core.PublicKeyTypeED25519, signature, preSigningOutput.DataHash)
44+
if !valid {
45+
panic(errors.New("verification failed"))
46+
}
47+
fmt.Println("Valid Signature")
48+
49+
txOutput := core.CompileWithSignatures(core.CoinTypeStellar, txInputData, [][]byte{signature}, [][]byte{pubkey})
50+
51+
var output stellar.SigningOutput
52+
err := proto.Unmarshal(txOutput, &output)
53+
if err != nil {
54+
panic(errors.New("unmarshal output failed"))
55+
}
56+
fmt.Println("Message for broadcast:", &output)
57+
}

0 commit comments

Comments
 (0)