Skip to content

Commit 854d0cb

Browse files
committed
Merge branch 'master-ec' into 2.0alpha-release
2 parents 824efe8 + 5eb56ae commit 854d0cb

File tree

3 files changed

+62
-13
lines changed

3 files changed

+62
-13
lines changed

README.md

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# Arweave JS
1+
# Arweave JS
22

33
Arweave JS is the JavaScript/TypeScript SDK for interacting with the Arweave network and uploading data to the permaweb. It works in latest browsers and Node JS.
44

5-
> **Notes:**
5+
> **Notes:**
66
> 1. If you are planning to upload large batches of data transactions to the Arweave network, it is strongly advised that you use [@DHA-Team/ArBundles](https://github.com/DHA-Team/arbundles) instead of transactions with Arweave.js. You can read about bundles and their advantages on the [Arwiki](https://arwiki.wiki/#/en/bundles).
77
> 2. When working with NodeJS a minimum version of 18+ is required. Bun/Deno not currently working.
88
@@ -32,7 +32,7 @@ Arweave JS is the JavaScript/TypeScript SDK for interacting with the Arweave net
3232
- [Get a transaction](#get-a-transaction)
3333
- [Get transaction data](#get-transaction-data)
3434
- [Decode tags from transactions](#decode-tags-from-transactions)
35-
- [Blocks](#blocks)
35+
- [Blocks](#blocks)
3636
- [Get a block by indep_hash](#get-a-block-by-indep_hash)
3737
- [Get current block](#get-current-block)
3838
- [GraphQL](#graphql)
@@ -132,6 +132,52 @@ const arweave = Arweave.init({
132132

133133
### Wallets and Keys
134134

135+
#### Create Wallets (2.0.0)
136+
137+
```typescript
138+
import { PrivateKey, KeyType, PublicKey, fromJWK, fromIdentifier } from "arweave/node/lib/crypto/keys";
139+
import Arweave from "arweave";
140+
141+
const arweave = Arweave.init({...});
142+
// you can use KeyType.RSA_65537 in the same fashion.
143+
const sk: PrivateKey = await arweave.wallets.generateKey({type: KeyType.EC_SECP256K1});
144+
145+
// Get the public key
146+
const pk: PublicKey = await sk.public();
147+
148+
// Serialize the Key into JsonWebKey format
149+
const skJWK: JsonWebKey = await sk.serialize({format: "jwk"});
150+
const pkJWK: JsonWebKey = await pk.serialize({format: "jwk"});
151+
152+
// Serialize the Key into raw format
153+
const skRaw: Uint8Array = await sk.serialize({format: "raw"});
154+
const pkRaw: Uint8Array = await pk.serialize({format: "raw"});
155+
156+
// Get Keys public identifier
157+
const identifier: Uint8Array = await pk.identifier();
158+
159+
// Deserialize a PrivateKey from either JsonWebKey
160+
const deserializedSK = await fromJWK(skJWK);
161+
162+
// Deserialize a PublicKey from Raw or JsonWebKey
163+
const pk = await fromJWK(pkJWK);
164+
const pk = await fromIdentifier({identifier});
165+
166+
// Get address of a key (private or public)
167+
const addr = await arweave.wallets.jwkToAddress(pk);
168+
const addr = await arweave.wallets.jwkToAddress(sk);
169+
170+
// Use keys directly for crypto operations
171+
const payload = new Uint8Array([1,2]);
172+
const signature: Uint8Array = await sk.sign({payload, isDigest: false});
173+
const isValid: boolean = await pk.verify({payload, signature, isDigest: false});
174+
// Recover EC PubKey from Signature and Data
175+
const pk: PublicKey = await SECP256k1PublicKey.recover({payload, signature, isDigest: false});
176+
// Or use the keys from the existing transaction/wallet interfaces
177+
const transaction: Transaction = await arweave.createTransaction({...}, sk/pk);
178+
await arweave.transactions.sign(transaction, sk);
179+
```
180+
135181
#### Create a new wallet and private key
136182

137183
Here you can generate a new wallet address and private key ([JWK](https://docs.arweave.org/developers/server/http-api#key-format)), don't expose private keys or make them public as anyone with the key can use the corresponding wallet.
@@ -377,13 +423,13 @@ console.log(response.status);
377423
// HTTP response codes (200 - server received the transaction, 4XX - invalid transaction, 5XX - error)
378424
```
379425

380-
**N.B.**
426+
**N.B.**
381427
_This `200` response does not mean that the transaction has mined & confirmed, and that a txid can be used as if it's immutable._ _It just means that a node has received your transaction._ _See [Get a transaction status](#get-a-transaction-status) for more detail on how to correctly determine that your transaction has been mined & confirmed._ _This also applies to the `uploader` method._
382428

383429

384430
##### Chunked uploading advanced options
385431

386-
You can resume an upload from a saved uploader object, that you have persisted in storage some using
432+
You can resume an upload from a saved uploader object, that you have persisted in storage some using
387433
`JSON.stringify(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:
388434

389435
```js
@@ -559,7 +605,7 @@ a list of zero to many transactions.
559605
Gets block data for given independent hash (see page 63. of [yellow-paper](https://www.arweave.org/yellow-paper.pdf) for details).
560606

561607
```js
562-
const result = await arweave.blocks.get("zbUPQFA4ybnd8h99KI9Iqh4mogXJibr0syEwuJPrFHhOhld7XBMOUDeXfsIGvYDp");
608+
const result = await arweave.blocks.get("zbUPQFA4ybnd8h99KI9Iqh4mogXJibr0syEwuJPrFHhOhld7XBMOUDeXfsIGvYDp");
563609
console.log(result)
564610
// {
565611
// "nonce": "6jdzO4FzS4EVaQVcLBEmxm6uN5-1tqBXW24Pzp6JsRQ",
@@ -580,15 +626,15 @@ const {current} = await arweave.network.getInfo();
580626
```
581627

582628
```js
583-
const result = await arweave.blocks.getCurrent();
629+
const result = await arweave.blocks.getCurrent();
584630
console.log(result)
585631
// {
586632
// "indep_hash": "qoJwHSpzl6Ouo140HW2DTv1rGOrgfBEnHi5sHv-fJt_TsK7xA70F2QbjMCopLiMd",
587633
// ...
588634
```
589635

590-
### GraphQL
591-
Find your transation ids and tags by searching their metadata. GraphQL (GQL) provides flexible querying and allows you to search for transactions by tags, wallet address, block height, etc.
636+
### GraphQL
637+
Find your transation ids and tags by searching their metadata. GraphQL (GQL) provides flexible querying and allows you to search for transactions by tags, wallet address, block height, etc.
592638

593639
Please see the [GQL Guide](https://gql-guide.vercel.app/) for further details.
594640

test/web/web.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as chai from "chai";
22
import * as crypto from "crypto";
33
import Arweave from "../../web";
4-
import { bufferToString, stringToBuffer } from "../../src/common/lib/utils";
4+
import { bufferToString, stringToBuffer, b64UrlToBuffer } from "../../src/common/lib/utils";
55

66
const expect = chai.expect;
77

@@ -88,9 +88,8 @@ describe("Wallets and keys", function () {
8888

8989
expect(walletA.e).to.equal("AQAB");
9090

91-
expect(walletA.n).to.match(/^[a-z0-9-_]{683}$/i);
92-
93-
expect(walletA.d).to.match(/^[a-z0-9-_]{683}$/i);
91+
expect(b64UrlToBuffer(walletA.n).byteLength).to.be.equal(512);
92+
expect(b64UrlToBuffer(walletA.d!).byteLength).to.be.equal(512);
9493

9594
const addressA = await arweave.wallets.jwkToAddress(walletA);
9695
const addressB = await arweave.wallets.jwkToAddress(walletB);

webpack.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ config.web = {
2626
process: require.resolve("process/browser"),
2727
crypto: require.resolve("crypto-browserify"),
2828
stream: require.resolve("stream-browserify"),
29+
buffer: require.resolve('buffer/')
2930
},
3031
extensions: [".ts", ".js"],
3132
},
3233
plugins: [
3334
new webpack.ProvidePlugin({
3435
process: "process/browser",
36+
Buffer: ['buffer', 'Buffer']
3537
}),
3638
],
3739
output: {
@@ -108,13 +110,15 @@ config.webtests = {
108110
process: require.resolve("process/browser"),
109111
crypto: require.resolve("crypto-browserify"),
110112
stream: require.resolve("stream-browserify"),
113+
buffer: require.resolve('buffer/')
111114
},
112115
},
113116
plugins: [
114117
new webpack.ProvidePlugin({
115118
process: "process/browser",
116119
crypto: "crypto-browserify",
117120
stream: "stream-browserify",
121+
Buffer: ['buffer', 'Buffer']
118122
}),
119123
],
120124
devtool: "inline-source-map",

0 commit comments

Comments
 (0)