|
5 | 5 | [](https://godoc.org/github.com/Dev43/arweave-go) |
6 | 6 | [](https://goreportcard.com/report/github.com/Dev43/arweave-go) |
7 | 7 |
|
8 | | -Golang SDK for the Arweave client. |
| 8 | +Golang Client to interact with the Arweave Blockchain. |
9 | 9 |
|
10 | | -Example of use: |
| 10 | +## Usage |
| 11 | + |
| 12 | +### Wallet |
| 13 | + |
| 14 | +In the current version, you can load the Arweave wallet file created from the Arweave server or the plugin. |
11 | 15 |
|
12 | 16 | ```golang |
| 17 | +// create a new wallet instance |
| 18 | +w := wallet.NewWallet() |
| 19 | +// extract the key from the wallet instance |
| 20 | +err = w.LoadKeyFromFile("./arweave.json") |
| 21 | +if err != nil { |
| 22 | + //... |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +You can directly load the key by using it's filepath or pass it as an array of bytes using `LoadKey([]byte)`. |
| 27 | + |
| 28 | +With the wallet struct, you can sign and verify a message: |
| 29 | + |
| 30 | +```golang |
| 31 | +// sign the message "example" |
| 32 | +msg := []byte("example") |
| 33 | +sig, err := w.Sign(msg)) |
| 34 | +if err != nil { |
| 35 | + //... |
| 36 | +} |
| 37 | +err = w.Verify(msg, sig) |
| 38 | +if err != nil { |
| 39 | + // message signature is not valid... |
| 40 | +} |
| 41 | +// message signature is valid |
| 42 | +``` |
| 43 | + |
| 44 | +### API |
| 45 | + |
| 46 | +You can call all of the Arweave HTTP api endpoints using the api package. First you must give it the node url or IP address. |
| 47 | + |
| 48 | +```golang |
| 49 | +ipAddress := "127.0.0.1" |
| 50 | +c, err := api.Dial(ipAddress) |
| 51 | +if err != nil { |
| 52 | + // problem connecting |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +To call the endpoints, you will need to pass in a context. |
| 57 | + |
| 58 | +```golang |
| 59 | +c.GetBalance(context.TODO(), "1seRanklLU_1VTGkEk7P0xAwMJfA7owA1JHW5KyZKlY") |
| 60 | +``` |
13 | 61 |
|
14 | | - // create a new arweave client |
15 | | - ar, err := transactor.NewTransactor("209.97.142.169") |
16 | | - if err != nil { |
17 | | - log.Fatal(err) |
18 | | - } |
| 62 | +### Transactions |
| 63 | + |
| 64 | +To create a new transaction, you will need to interact with the `transactor` package. The transactor package has 3 main functions, creating, sending and waiting for a transaction. |
| 65 | + |
| 66 | +```golang |
| 67 | + // create a new transactor client |
| 68 | + ar, err := transactor.NewTransactor("127.0.0.1") |
| 69 | + if err != nil { |
| 70 | + //... |
| 71 | + } |
19 | 72 |
|
20 | 73 | // create a new wallet instance |
21 | 74 | w := wallet.NewWallet() |
22 | 75 | // extract the key from the wallet instance |
23 | | - err = w.ExtractKey("./arweave.json") |
| 76 | + err = w.LoadKeyFromFile("./arweave.json") |
24 | 77 | if err != nil { |
25 | | - log.Fatal(err) |
| 78 | + //... |
26 | 79 | } |
27 | 80 | // create a transaction |
28 | | - txBuilder, err := ar.CreateTransaction(context.TODO(), w, "0", []byte(""), "xblmNxr6cqDT0z7QIWBCo8V0UfJLd3CRDffDhF5Uh9g") |
| 81 | + txBuilder, err := ar.CreateTransaction(context.TODO(), w, "0", []byte(""), "1seRanklLU_1VTGkEk7P0xAwMJfA7owA1JHW5KyZKlY") |
29 | 82 | if err != nil { |
30 | | - log.Fatal(err) |
| 83 | + //... |
31 | 84 | } |
32 | | - |
| 85 | + |
33 | 86 | // sign the transaction |
34 | 87 | txn, err := txBuilder.Sign(w) |
35 | 88 | if err != nil { |
36 | | - log.Fatal(err) |
| 89 | + //... |
37 | 90 | } |
38 | 91 |
|
39 | 92 | // send the transaction |
40 | 93 | resp, err := ar.SendTransaction(context.TODO(), txn) |
41 | 94 | if err != nil { |
42 | | - log.Fatal(err) |
| 95 | + //... |
43 | 96 | } |
44 | 97 |
|
45 | | - fmt.Println(resp) |
46 | | - |
47 | 98 | // wait for the transaction to get mined |
48 | | - pendingTx, err := ar.WaitMined(context.TODO(), txn) |
| 99 | + finalTx, err := ar.WaitMined(context.TODO(), txn) |
| 100 | + if err != nil { |
| 101 | + //... |
| 102 | + } |
| 103 | + // get the hash of the transaction |
| 104 | + fmt.Println(finalTx.Hash()) |
| 105 | +``` |
| 106 | + |
| 107 | +### Batching, chunking and recombining |
| 108 | + |
| 109 | +Arweave currently has a 3MB transaction limit. To let you be able to upload more than 3MB of data, this library lets you chunk your data into multiples chunks, and upload these chunks to the weave. These chunks are all backlinked to their previous chunk (except the first one) so all you need to use to retrieve all the chunks is the last chunk's address (the "tip" of this linked list). |
| 110 | + |
| 111 | +So far the library has been tested with Tarred and Gzipped files (it's a good idea to compress your files, you'll be paying less fees to the network!) |
| 112 | + |
| 113 | +#### Creating chunks |
| 114 | + |
| 115 | +To create chunks, use the `chunker` package. Currently the chunker takes 500KB chunks, encodes it to the same encoding as the Arweave (Raw Base64 URL encoding) and adds it to a transaction. |
| 116 | + |
| 117 | +```golang |
| 118 | + f, err := os.Open("example.tar.gz") |
49 | 119 | if err != nil { |
50 | | - log.Fatal(err) |
| 120 | + //... |
51 | 121 | } |
52 | | - fmt.Println(pendingTx) |
| 122 | + info, err := f.Stat() |
| 123 | + if err != nil { |
| 124 | + //... |
| 125 | + } |
| 126 | + chunker, err := chunker.NewChunker(f, info.Size()) |
| 127 | + if err != nil { |
| 128 | + //... |
| 129 | + } |
| 130 | + chunks, err := chunker.ChunkAll() |
| 131 | + if err != nil { |
| 132 | + //... |
| 133 | + } |
| 134 | +``` |
| 135 | + |
| 136 | +#### Sending a batch of transactions |
| 137 | + |
| 138 | +To directly do both the chunking and the batch sending of transaction, you can use the `batchchunker` package. The package needs to wait for a single transaction to be mined before sending a new one. This will take a significant amount of time as the Arweave has a ~2 minute block time. |
| 139 | + |
| 140 | +```golang |
| 141 | + f, err := os.Open("example.tar.gz") |
| 142 | + if err != nil { |
| 143 | + //... |
| 144 | + } |
| 145 | + info, err := f.Stat() |
| 146 | + if err != nil { |
| 147 | + //... |
| 148 | + } |
| 149 | + |
| 150 | + // creates a new batch |
| 151 | + newB := batchchunker.NewBatch(ar, w, f, info.Size()) |
| 152 | + |
| 153 | + // sends all the transactions |
| 154 | + list, err := newB.SendBatchTransaction() |
| 155 | + if err != nil { |
| 156 | + //... |
| 157 | + } |
| 158 | +``` |
| 159 | + |
| 160 | +#### Recombining |
| 161 | + |
| 162 | +Recombining all the chunks needs to be done from the tip of the batch chain (the last transaction sent). The function will grab all the chunks and recombine it into an io.Writer. |
| 163 | + |
| 164 | +```golang |
| 165 | + // in this example, we will save the data as a file |
| 166 | + f, err := os.Create("example.tar.gz") |
| 167 | + if err != nil { |
| 168 | + //... |
| 169 | + } |
| 170 | + // create a new combiner |
| 171 | + newBCombiner := combiner.NewBatchCombiner(ar.Client) |
53 | 172 |
|
| 173 | + // grab all the chunks starting from chunk at this address |
| 174 | + liveChunks, err := newBCombiner.GetAllChunks("fBwTnPGNSAtHTNr6pGvqiYpLmBvTjjJOu3kBxuuit1c") |
| 175 | + if err != nil { |
| 176 | + //... |
| 177 | + } |
| 178 | + // recombine all the chunks into our file |
| 179 | + err = combiner.Recombine(liveChunks, f) |
| 180 | + if err != nil { |
| 181 | + //... |
| 182 | + } |
54 | 183 | ``` |
| 184 | + |
| 185 | +If you actually run the last example, it will retrieve a folder with the Bitcoin, Ethereum and Arweave whitepaper |
| 186 | + |
| 187 | + |
| 188 | +If you enjoy the library, please consider donating: |
| 189 | + |
| 190 | +- **Arweave Address**: `pfJXiTwwjQwSJF9VT1ZK6kauvobWuKKLUzjz29R1gbQ` |
| 191 | +- **Ethereum Address**: `0x3E42b8b399dca71b5c004921Fc6eFfa8dDc9409d` |
0 commit comments