Skip to content

Commit 8773d4c

Browse files
Merge pull request #15 from everFinance/feature/refactor
Feature/refactor
2 parents 48fc426 + 5ca4704 commit 8773d4c

26 files changed

+512
-567
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
main.go
22
test-keyfile.json
3-
.DS_Store
3+
.DS_Store
4+
go.sum

README.md

+57-28
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ import (
1818
"math/big"
1919

2020
"github.com/everFinance/goar/types"
21-
"github.com/everFinance/goar/wallet"
21+
"github.com/everFinance/goar"
2222
)
2323

2424
func main() {
25-
wallet, err := wallet.NewFromPath("./test-keyfile.json", "https://arweave.net")
25+
wallet, err := goar.NewWalletFromPath("./test-keyfile.json", "https://arweave.net")
2626
if err != nil {
2727
panic(err)
2828
}
2929

30-
id, stat, err := wallet.SendWinston(
30+
id, err := wallet.SendWinston(
3131
big.NewInt(1), // Winston amount
3232
{{target}}, // target address
3333
[]types.Tag{
@@ -38,7 +38,7 @@ func main() {
3838
},
3939
)
4040

41-
fmt.Println(id, stat, err) // {{id}}, Pending, nil
41+
fmt.Println(id, err) // {{id}}, nil
4242
}
4343

4444
```
@@ -52,16 +52,16 @@ import (
5252
"fmt"
5353

5454
"github.com/everFinance/goar/types"
55-
"github.com/everFinance/goar/wallet"
55+
"github.com/everFinance/goar"
5656
)
5757

5858
func main() {
59-
wallet, err := wallet.NewFromPath("./test-keyfile.json", "https://arweave.net")
59+
wallet, err := goar.NewWalletFromPath("./test-keyfile.json", "https://arweave.net")
6060
if err != nil {
6161
panic(err)
6262
}
6363

64-
id, stat, err := wallet.SendData(
64+
id, err := wallet.SendData(
6565
[]byte("123"), // Data bytes
6666
[]types.Tag{
6767
types.Tag{
@@ -71,13 +71,13 @@ func main() {
7171
},
7272
)
7373

74-
fmt.Println(id, stat, err) // {{id}}, Pending, nil
74+
fmt.Println(id, err) // {{id}}, nil
7575
}
7676
```
7777

78-
### Golang Package
78+
### Components
7979

80-
#### client
80+
#### Client
8181

8282
- [x] GetInfo
8383
- [x] GetTransactionByID
@@ -97,49 +97,75 @@ func main() {
9797
Initialize the instance:
9898

9999
```golang
100-
arClient := New("https://arweave.net")
100+
arClient := goar.NewClient("https://arweave.net")
101101

102102
// if your network is not good, you can config http proxy
103103
proxyUrl := "http://127.0.0.1:8001"
104-
arClient := New("https://arweave.net", proxyUrl)
104+
arClient := goar.NewClient("https://arweave.net", proxyUrl)
105105
```
106106

107-
#### wallet
107+
#### Wallet
108108

109109
- [x] SendAR
110+
- [x] SendARSpeedUp
110111
- [x] SendWinston
112+
- [x] SendWinstonSpeedUp
111113
- [x] SendData
112114
- [x] SendDataSpeedUp
113115
- [x] SendTransaction
114116

115117
Initialize the instance, use a keyfile.json:
116118

117119
```golang
118-
arWallet := NewFromPath("./keyfile.json")
120+
arWallet := goar.NewWalletFromPath("./keyfile.json")
119121

120122
// if your network is not good, you can config http proxy
121123
proxyUrl := "http://127.0.0.1:8001"
122-
arWallet := NewFromPath("./keyfile.json", "https://arweave.net", proxyUrl)
124+
arWallet := NewWalletFromPath("./keyfile.json", "https://arweave.net", proxyUrl)
123125
```
124126

127+
#### Utils
128+
129+
Package for Arweave develop toolkit.
130+
131+
- [x] Base64Encode
132+
- [x] Base64Decode
133+
- [x] Sign
134+
- [x] Verify
135+
- [x] DeepHash
136+
- [x] GenerateChunks
137+
- [x] ValidatePath
138+
- [x] OwnerToAddress
139+
- [x] OwnerToPubKey
140+
- [x] TagsEncode
141+
- [x] TagsDecode
142+
- [x] PrepareChunks
143+
- [x] GetChunk
144+
- [x] SignTransaction
145+
- [x] GetSignatureData
146+
- [x] VerifyTransaction
147+
125148
#### RSA Threshold Cryptography
126149

127-
- [x] CreateKeyPair
150+
- [x] CreateTcKeyPair
128151
- [x] ThresholdSign
129152
- [x] AssembleSigShares
130153
- [x] VerifySigShare
131154

132155
Create RSA Threshold Cryptography:
156+
133157
```golang
134-
bitSize := 1024 // If the values are 2048 and 4096, then the generation functions below will perform minute-level times, and we need 4096 bits as the maximum safety level for production environments.
158+
bitSize := 512 // If the values are 2048 and 4096, then the generation functions below will perform minute-level times, and we need 4096 bits as the maximum safety level for production environments.
135159
l := 5
136160
k := 3
137-
keyShares, keyMeta, err := CreateKeyPair(bitSize, k, l)
161+
keyShares, keyMeta, err := goar.CreateTcKeyPair(bitSize, k, l)
138162
```
163+
139164
New sign instance:
165+
140166
```golang
141167
exampleData := []byte("aaabbbcccddd112233") // need sign data
142-
ts, err := NewTcSign(keyMeta, exampleData)
168+
ts, err := goar.NewTcSign(keyMeta, exampleData)
143169

144170
// signer threshold sign
145171
signer01 := keyShares[0]
@@ -156,7 +182,6 @@ signature, err := ts.AssembleSigShares(signedShares)
156182
err := ts.VerifySigShare(signer01)
157183
```
158184

159-
160185
### Development
161186

162187
#### Test
@@ -177,7 +202,7 @@ Simple example:
177202

178203
```golang
179204
arNode := "https://arweave.net"
180-
w, err := NewFromPath("../example/testKey.json", arNode) // your wallet private key
205+
w, err := goar.NewWalletFromPath("../example/testKey.json", arNode) // your wallet private key
181206
anchor, err := w.Client.GetTransactionAnchor()
182207
if err != nil {
183208
return
@@ -196,18 +221,18 @@ Simple example:
196221
Format: 2,
197222
Target: "",
198223
Quantity: "0",
199-
Tags: types.TagsEncode(tags),
200-
Data: data,
224+
Tags: utils.TagsEncode(tags),
225+
Data: utils.Base64Encode(data),
201226
DataSize: fmt.Sprintf("%d", len(data)),
202227
Reward: fmt.Sprintf("%d", reward*(100+speedFactor)/100),
203228
}
204-
if err = tx.SignTransaction(w.PubKey, w.PrvKey); err != nil {
229+
if err = utils.SignTransaction(tx, w.PubKey, w.PrvKey); err != nil {
205230
return
206231
}
207232

208233
id = tx.ID
209234

210-
uploader, err := uploader.CreateUploader(w.Client, tx, nil)
235+
uploader, err := goar.CreateUploader(w.Client, tx, nil)
211236
if err != nil {
212237
return
213238
}
@@ -218,10 +243,12 @@ Simple example:
218243
}
219244
}
220245
```
246+
221247
##### Breakpoint continuingly
248+
222249
You can resume an upload from a saved uploader object, that you have persisted in storage some using json.marshal(uploader) at any stage of the upload.To resume, parse it back into an object and pass it to getUploader() along with the transactions data:
223-
```
224250

251+
```golang
225252
uploaderBuf, err := ioutil.ReadFile("./jsonUploaderFile.json")
226253
lastUploader := &txType.TransactionUploader{}
227254
err = json.Unmarshal(uploaderBuf, lastUploader)
@@ -235,17 +262,19 @@ You can resume an upload from a saved uploader object, that you have persisted i
235262
assert.NoError(t, err)
236263
}
237264
```
265+
238266
When resuming the upload, you must provide the same data as the original upload. When you serialize the uploader object with json.marshal() to save it somewhere, it will not include the data.
267+
239268
##### Breakpoint retransmission
269+
240270
You can also resume an upload from just the transaction ID and data, once it has been mined into a block. This can be useful if you didn't save the uploader somewhere but the upload got interrupted. This will re-upload all of the data from the beginning, since we don't know which parts have been uploaded:
241271

242272
```golang
243-
244273
bigData, err := ioutil.ReadFile(filePath)
245274
txId := "myTxId"
246275

247276
// get uploader by txId and post big data by chunks
248-
uploader, err := txType.CreateUploader(wallet.Client, txId, bigData)
277+
uploader, err := goar.CreateUploader(wallet.Client, txId, bigData)
249278
assert.NoError(t, err)
250279
for !uploader.IsComplete() {
251280
err := uploader.UploadChunk()

client/client.go client.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package client
1+
package goar
22

33
import (
44
"bytes"
@@ -23,7 +23,7 @@ type Client struct {
2323
url string
2424
}
2525

26-
func New(nodeUrl string, proxyUrl ...string) *Client {
26+
func NewClient(nodeUrl string, proxyUrl ...string) *Client {
2727
httpClient := http.DefaultClient
2828
// if exist proxy url
2929
if len(proxyUrl) > 0 {

client/client_test.go client_test.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
1-
package client
1+
package goar
22

33
import (
4-
"github.com/everFinance/goar/types"
4+
"testing"
5+
56
"github.com/everFinance/goar/utils"
67
"github.com/stretchr/testify/assert"
7-
"testing"
88
)
99

1010
// import (
1111
// "fmt"
1212
// "testing"
1313

1414
// func TestGetTransactionByID(t *testing.T) {
15-
// client := New("https://arweave.net")
15+
// client := NewClient("https://arweave.net")
1616
// fmt.Println(client.GetTransactionByID("FgcKlptyDXSgEonYfy5cNBimq7GJ4h8h6L6pxuuYOBc"))
1717
// }
1818

1919
// func TestGetTransactionPrice(t *testing.T) {
20-
// client := New("https://arweave.net")
20+
// client := NewClient("https://arweave.net")
2121
// target := ""
2222
// reward, err := client.GetTransactionPrice([]byte("123"), &target)
2323
// assert.NoError(t, err)
2424
// fmt.Println(reward)
2525
// }
2626

2727
// func TestGetLastTransactionID(t *testing.T) {
28-
// client := New("https://arweave.net")
28+
// client := NewClient("https://arweave.net")
2929
// lastTx, err := client.GetLastTransactionID("dQzTM9hXV5MD1fRniOKI3MvPF_-8b2XDLmpfcMN9hi8")
3030
// assert.NoError(t, err)
3131
// fmt.Println(lastTx)
3232
// }
3333

3434
// func TestGetTransactionAnchor(t *testing.T) {
35-
// client := New("https://arweave.net")
35+
// client := NewClient("https://arweave.net")
3636
// fmt.Println(client.GetTransactionAnchor())
3737
// }
3838

3939
// func TestSubmitTransaction(t *testing.T) {
40-
// client := New("https://arweave.net")
40+
// client := NewClient("https://arweave.net")
4141
// fmt.Println(
4242
// client.SubmitTransaction(&types.Transaction{
4343
// ID: "n1iKT3trKn6Uvd1d8XyOqKBy8r-8SSBtGA62m3puK5k",
@@ -46,7 +46,7 @@ import (
4646
// }
4747

4848
// func TestArql(t *testing.T) {
49-
// client := New("https://arweave.net")
49+
// client := NewClient("https://arweave.net")
5050
// fmt.Println(
5151
// client.Arql(`
5252
// {
@@ -67,7 +67,7 @@ import (
6767
// }
6868

6969
// func TestGraphQL(t *testing.T) {
70-
// client := New("https://arweave.net")
70+
// client := NewClient("https://arweave.net")
7171
// data, err := client.GraphQL(`
7272
// {
7373
// transactions(
@@ -95,14 +95,14 @@ import (
9595
// }
9696

9797
// func TestGetWalletBalance(t *testing.T) {
98-
// client := New("https://arweave.net")
98+
// client := NewClient("https://arweave.net")
9999
// fmt.Println(
100100
// client.GetWalletBalance("dQzTM9hXV5MD1fRniOKI3MvPF_-8b2XDLmpfcMN9hi8"),
101101
// )
102102
// }
103103

104104
func TestClient_DownloadChunkData(t *testing.T) {
105-
// client := New("https://arweave.net")
105+
// client := NewClient("https://arweave.net")
106106
// id := "ybEmme6TE3JKwnSYciPCjnAINwi_CWthomsxBes-kYk"
107107
// data, err := client.GetTransactionData(id, "jpg")
108108
// assert.NoError(t, err)
@@ -114,7 +114,7 @@ func TestClient_DownloadChunkData(t *testing.T) {
114114

115115
func TestClient_GetTransactionData(t *testing.T) {
116116
// proxy := "http://127.0.0.1:8001"
117-
client := New("https://arweave.net")
117+
client := NewClient("https://arweave.net")
118118
id := "lSHWbAfjJsK0so08BTTmHO_n809fGW2DYOMySsXHNuI"
119119
data, err := client.GetTransactionData(id, "json")
120120
if err != nil {
@@ -133,12 +133,12 @@ func TestNew(t *testing.T) {
133133
func TestClient_VerifyTx(t *testing.T) {
134134
// txId := "XOzxw5kaYJrt9Vljj23pA5_6b63kY2ydQ0lPfnhksMA"
135135
txId := "_fVj-WyEtXV3URXlNkSnHVGupl7_DM1UWZ64WMdhPkU"
136-
client := New("https://arweave.net")
136+
client := NewClient("https://arweave.net")
137137
tx, status, code, err := client.GetTransactionByID(txId)
138138
assert.NoError(t, err)
139139
t.Log(status, code)
140140
t.Log(tx.Format)
141-
t.Log(types.TagsDecode(tx.Tags))
142-
err = tx.VerifyTransaction()
141+
t.Log(utils.TagsDecode(tx.Tags))
142+
err = utils.VerifyTransaction(*tx)
143143
assert.NoError(t, err)
144144
}

0 commit comments

Comments
 (0)