Skip to content
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

key exports methods; sendgramm, sendmessage, sendfile bugfixes #3

Merged
merged 1 commit into from
Oct 8, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import "github.com/mercuryoio/tonlib-go"
- [x] createNewKey
- [x] deleteKey
- [x] exportKey
- [ ] exportPemKey
- [ ] exportEncryptedKey
- [x] exportPemKey
- [x] exportEncryptedKey
- [ ] importKey
- [ ] importPemKey
- [ ] importEncryptedKey
Expand Down
91 changes: 81 additions & 10 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"github.com/dvsekhvalnov/jose2go/base64url"
_ "github.com/mercuryoio/tonlib-go/lib"
"math/rand"
"strconv"
Expand Down Expand Up @@ -179,8 +180,8 @@ func (client *Client) GetAccountState(address string) (state *TONAccountState, e
return state, nil
}

// send gramm to address
func (client *Client) SendGRAMM2Address(key *TONPrivateKey, password []byte, fromAddress, toAddress string, amount string) (*TONResult, error) {
// sends gramm to address and returns transaction`s hash
func (client *Client) SendGRAMM2Address(key *TONPrivateKey, password []byte, fromAddress, toAddress, amount, message string) (string, error) {
st := struct {
Type string `json:"@type"`
Seqno int64 `json:"seqno"`
Expand All @@ -189,6 +190,7 @@ func (client *Client) SendGRAMM2Address(key *TONPrivateKey, password []byte, fro
Destination TONAccountAddress `json:"destination"`
ValidUntil uint `json:"valid_until"`
Source TONAccountAddress `json:"source"`
Message []byte `json:"message"`
}{
Type: "generic.sendGrams",
PrivateKey: key.getInputKey(password),
Expand All @@ -200,31 +202,41 @@ func (client *Client) SendGRAMM2Address(key *TONPrivateKey, password []byte, fro
Source: TONAccountAddress{
AccountAddress: fromAddress,
},
Message: []byte(base64url.Encode([]byte(message))),
}
resp, err := client.executeAsynchronously(st)
if err != nil {
return resp, err
return "", err
}
if st, ok := resp.Data["@type"]; ok && st == "error" {
return resp, fmt.Errorf("Error ton send gramms. Code %v. Message %s. ", resp.Data["code"], resp.Data["message"])
return "", fmt.Errorf("Error ton send gramms. Code %v. Message %s. ", resp.Data["code"], resp.Data["message"])
}

r := struct {
SentUntil int `json:"sent_until"`
BodyHash string `json:"body_hash"`
}{}
err = json.Unmarshal(resp.Raw, &r)
if err != nil {
return "", err
}
return resp, nil
return r.BodyHash, nil
}

// send message to address
func (client *Client) SendMessage(initialAddress, destinationAddress, data string) (res *TONResult, err error) {
func (client *Client) SendMessage(destinationAddress string, initialAccountState, data []byte) (res *TONResult, err error) {
st := struct {
Type string `json:"@type"`
Destination TONAccountAddress `json:"destination"`
InitialAccountState string `json:"initial_account_state"`
Data string `json:"data"`
InitialAccountState []byte `json:"initial_account_state"`
Data []byte `json:"data"`
}{
Type: "raw.sendMessage",
Data: data,
Destination: TONAccountAddress{
AccountAddress: destinationAddress,
},
InitialAccountState: initialAddress,
InitialAccountState: initialAccountState,
}
return client.executeAsynchronously(st)
}
Expand Down Expand Up @@ -304,7 +316,7 @@ func (client *Client) DeletePrivateKey(key *TONPrivateKey, password []byte) (err
return nil
}

// delete private key
// export private key
func (client *Client) ExportPrivateKey(key *TONPrivateKey, password []byte) (wordList []string, err error) {
st := struct {
Type string `json:"@type"`
Expand All @@ -331,6 +343,65 @@ func (client *Client) ExportPrivateKey(key *TONPrivateKey, password []byte) (wor
return mm.WordList, nil
}

// export pem
func (client *Client) ExportPemKey(key *TONPrivateKey, password, pemPassword []byte) (pem string, err error) {
st := struct {
Type string `json:"@type"`
InputKey InputKey `json:"input_key"`
KeyPassword string `json:"key_password"`
}{
Type: "exportPemKey",
InputKey: key.getInputKey(password),
KeyPassword: base64.StdEncoding.EncodeToString(pemPassword),
}
resp, err := client.executeAsynchronously(st)
if err != nil {
return "", err
}
if st, ok := resp.Data["@type"]; ok && st == "error" {
return "", fmt.Errorf("Error ton create private key. Code %v. Message %s. ", resp.Data["code"], resp.Data["message"])
}
return "", err

p := struct {
Pem string `json:"pem"`
}{}
err = json.Unmarshal(resp.Raw, &p)
if err != nil {
return "", err
}
return p.Pem, nil
}

// export encrypted key
func (client *Client) ExportEncryptedKey(key *TONPrivateKey, password, pemPassword []byte) (data string, err error) {
st := struct {
Type string `json:"@type"`
InputKey InputKey `json:"input_key"`
KeyPassword string `json:"key_password"`
}{
Type: "exportEncryptedKey",
InputKey: key.getInputKey(password),
KeyPassword: base64.StdEncoding.EncodeToString(pemPassword),
}
resp, err := client.executeAsynchronously(st)
if err != nil {
return "", err
}
if st, ok := resp.Data["@type"]; ok && st == "error" {
return "", fmt.Errorf("Error ton create private key. Code %v. Message %s. ", resp.Data["code"], resp.Data["message"])
}

mm := struct {
Data string `json:"data"`
}{}
err = json.Unmarshal(resp.Raw, &mm)
if err != nil {
return "", err
}
return mm.Data, nil
}

//change localPassword
func (client *Client) ChangeLocalPassword(key *TONPrivateKey, password, newPassword []byte) (*TONPrivateKey, error) {
st := struct {
Expand Down
42 changes: 40 additions & 2 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,45 @@ func TestClient_ExportPrivateKey(t *testing.T) {
t.Fatal("Ton create key error", err)
}
if _, err = cln.ExportPrivateKey(pkey, []byte(TEST_PASSWORD)); err != nil {
t.Fatal("Ton delete key error", err)
t.Fatal("Ton export private key error", err)
}
}

func TestClient_ExportPemKey(t *testing.T) {
cnf, err := ParseConfigFile("./tonlib.config.json.example")
if err != nil {
t.Fatal("Config file not found", err)
}
cln, err := NewClient(cnf, Config{})
if err != nil {
t.Fatal("Init client error", err)
}
defer cln.Destroy()
pkey, err := cln.CreatePrivateKey([]byte(TEST_PASSWORD))
if err != nil {
t.Fatal("Ton create key error", err)
}
if _, err = cln.ExportPemKey(pkey, []byte(TEST_PASSWORD), []byte(TEST_PASSWORD)); err != nil {
t.Fatal("Ton export pem key error", err)
}
}

func TestClient_ExportEncryptedKey(t *testing.T) {
cnf, err := ParseConfigFile("./tonlib.config.json.example")
if err != nil {
t.Fatal("Config file not found", err)
}
cln, err := NewClient(cnf, Config{})
if err != nil {
t.Fatal("Init client error", err)
}
defer cln.Destroy()
pkey, err := cln.CreatePrivateKey([]byte(TEST_PASSWORD))
if err != nil {
t.Fatal("Ton create key error", err)
}
if _, err = cln.ExportEncryptedKey(pkey, []byte(TEST_PASSWORD), []byte(TEST_PASSWORD)); err != nil {
t.Fatal("Ton export pem key error", err)
}
}

Expand Down Expand Up @@ -214,7 +252,7 @@ func TestClient_SendGRAMM2Address(t *testing.T) {
if err != nil {
t.Fatal("Ton get address for send grams error", err)
}
_, err = cln.SendGRAMM2Address(pKey, []byte(TEST_PASSWORD), address.AccountAddress, TEST_ADDRESS, TEST_AMOUNT)
_, err = cln.SendGRAMM2Address(pKey, []byte(TEST_PASSWORD), address.AccountAddress, TEST_ADDRESS, TEST_AMOUNT, "")
if err != nil && err.Error() != "Error ton send gramms. Code 500. Message NOT_ENOUGH_FUNDS. " {
t.Fatal("Ton send gramms error", err)
}
Expand Down
20 changes: 14 additions & 6 deletions cmd/tongo/sendFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ var sendFileCmd = &cobra.Command{
Short: "Send boc file command",
Long: `Send message command. It contains four attributes:
- path2configfile. see tonlib.config.json.example
- initialAddress
- destinationAddress
- path2boc path to boc binary file
- path2boc path to boc binary file
- path2initialAccountState path to boc file with initial account state (optional)
`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 4 {
return fmt.Errorf("you have to use four args for this commaond \n")
if len(args) < 3 {
return fmt.Errorf("you have to use minimum 3 args for this commaond \n")
}
_, err := os.Stat(args[0])
if err != nil {
Expand All @@ -36,12 +36,20 @@ func sendFile(cmd *cobra.Command, args []string) {
fmt.Println("init connection error: ", err)
os.Exit(0)
}
bocFile, err := ioutil.ReadFile(args[3])
bocFile, err := ioutil.ReadFile(args[2])
if err != nil {
fmt.Println("boc file dosn't exist", err)
os.Exit(0)
}
bocInitialStateFile := []byte{}
if len(args) > 3 {
bocInitialStateFile, err = ioutil.ReadFile(args[3])
if err != nil {
fmt.Println("boc file dosn't exist", err)
os.Exit(0)
}
}

res, err := tonClient.SendMessage(args[1], args[2], string(bocFile))
res, err := tonClient.SendMessage(args[1], bocInitialStateFile, bocFile)
fmt.Printf("Got a result: %v. Errors: %v", res, err)
}
13 changes: 9 additions & 4 deletions cmd/tongo/sendGramm.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ var sendGrammCmd = &cobra.Command{
- password
- addressDestination
- amount
- message for destination. not required
`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 6 {
return fmt.Errorf("you have to use six args for this commaond \n")
if len(args) < 6 {
return fmt.Errorf("you have to use minimum six args for this commaond \n")
}
_, err := os.Stat(args[0])
if err != nil {
Expand All @@ -39,6 +40,10 @@ func sendGramm(cmd *cobra.Command, args []string) {
password := args[3]
destinationAddr := args[4]
amount := args[5]
message := ""
if len(args) > 6 {
message = args[6]
}
err := initClient(confPath)
if err != nil {
fmt.Println("init connection error: ", err)
Expand All @@ -56,6 +61,6 @@ func sendGramm(cmd *cobra.Command, args []string) {
fmt.Println("get wallet address error: ", err)
os.Exit(0)
}
resp, err := tonClient.SendGRAMM2Address(pKey, []byte(password), addr.AccountAddress, destinationAddr, amount)
fmt.Printf("Got a result: %v. Errors: %v \n", resp, err)
resp, err := tonClient.SendGRAMM2Address(pKey, []byte(password), addr.AccountAddress, destinationAddr, amount, message)
fmt.Printf("Got a result: hash %v. Errors: %v \n", resp, err)
}
11 changes: 5 additions & 6 deletions cmd/tongo/sendMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ import (
var sendMessageCmd = &cobra.Command{
Use: "sendMessage",
Short: "Send short message command",
Long: `Send message command. It contains four attributes:
Long: `Send message command. It contains three attributes:
- path2configfile. see tonlib.config.json.example
- initialAddress
- destinationAddress
- data
- data in boc format
`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 4 {
return fmt.Errorf("you have to use four args for this commaond \n")
if len(args) != 3 {
return fmt.Errorf("you have to use three args for this commaond \n")
}
_, err := os.Stat(args[0])
if err != nil {
Expand All @@ -35,6 +34,6 @@ func sendMessage(cmd *cobra.Command, args []string) {
fmt.Println("init connection error: ", err)
os.Exit(0)
}
res, err := tonClient.SendMessage(args[1], args[2], args[3])
res, err := tonClient.SendMessage(args[1], []byte{}, []byte(args[2]))
fmt.Printf("Got a result: %v. Errors: %v", res, err)
}