Skip to content

Commit

Permalink
update write method
Browse files Browse the repository at this point in the history
  • Loading branch information
Sagleft committed Mar 31, 2022
1 parent ac4fa54 commit 2022c65
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ Emercoin NVS lib written in Go

-----

## Install

```bash
go get github.com/sagleft/go-nvs
```

## Usage example

```go
Expand Down
1 change: 1 addition & 0 deletions examples/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func main() {
err = client.Write(gonvs.WriteEntryTask{
Name: "test:0001",
Value: []byte("entry value"),
Days: 30,
})
if err != nil {
log.Fatalln(err)
Expand Down
31 changes: 28 additions & 3 deletions nvs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package gonvs

import (
"encoding/json"
"errors"
"strconv"

rpcclient "github.com/bitlum/go-bitcoind-rpc/rpcclient"
)

Expand Down Expand Up @@ -30,7 +34,28 @@ func NewClient(task CreateClientTask) (*Client, error) {
return &c, nil
}

func (c *Client) Write(task WriteEntryTask) error {
// TODO
return nil
func (c *Client) Write(task WriteEntryTask) ([]byte, error) {
requestData := []json.RawMessage{
json.RawMessage(task.Name),
json.RawMessage(task.Value),
json.RawMessage(strconv.Itoa(task.Days)),
}
if task.ToAddress != "" {
requestData = append(requestData, json.RawMessage(task.ToAddress))
}
if task.ValueType != "" {
requestData = append(requestData, json.RawMessage(task.ValueType))
}

response, err := c.RPC.RawRequest("name_new", requestData)
if err != nil {
return nil, errors.New("failed to write entry: " + err.Error())
}

msg, err := response.MarshalJSON()
if err != nil {
return nil, err
}

return msg, nil
}
20 changes: 20 additions & 0 deletions structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,28 @@ type CreateClientTask struct {
UseSSL bool `json:"useSSL"`
}

// ValueType - NVS value type
type ValueType string

const (
// ValueTypeHex - hex value
ValueTypeHex string = "hex"

// ValueTypeBase64 - base64 value
ValueTypeBase64 string = "base64"

// ValueTypePlain - plain value
ValueTypePlain string = ""
)

// WriteEntryTask - new blockchain NVS entry data
type WriteEntryTask struct {
//required
Name string `json:"name"`
Value []byte `json:"value"`
Days int `json:"days"`

// optional
ToAddress string `json:"toAddress"`
ValueType ValueType `json:"type"`
}

0 comments on commit 2022c65

Please sign in to comment.