Arweave JS
Arweave JS is the JavaScript/TypeScript SDK for interacting with the Arweave network and uploading data ot the permaweb. It works in latest browsers and Node JS.
Installation
NPM
npm install --save arweave
Bundles
Single bundle file (web only - use the NPM method if using Node).
<script src="https://unpkg.com/arweave/bundles/web.bundle.js"></script>
<script src="https://unpkg.com/arweave/bundles/web.bundle.min.js"></script>
<script src="https://unpkg.com/arweave@1.2.0/bundles/web.bundle.js"></script>
<script src="https://unpkg.com/arweave@1.2.0/bundles/web.bundle.min.js"></script>
Initialisation
NPM Node
const Arweave = require('arweave/node');
const instance = Arweave.init({
host: '127.0.0.1',
port: 1984,
protocol: 'http'
});
NPM Web
import Arweave from 'arweave/web';
const arweave = Arweave.init();
const arweave = Arweave.init({
host: '127.0.0.1',
port: 1984,
protocol: 'http'
});
Web Bundles
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello world</title>
<script src="https://unpkg.com/arweave/bundles/web.bundle.js"></script>
<script>
const arweave = Arweave.init();
arweave.network.getInfo().then(console.log);
</script>
</head>
<body>
</body>
</html>
Initialisation options
{
host: 'arweave.net',
port: 443,
protocol: 'https',
timeout: 20000,
logging: false,
}
Usage
Wallets and Keys
Create a new wallet and private key
Here you can generate a new wallet address and private key (JWK), don't expose private keys or make them public as anyone with the key can use the corresponding wallet.
Make sure they're stored securely as they can never be recovered if lost.
Once AR has been sent to the address for a new wallet, the key can then be used to sign outgoing transactions.
arweave.wallets.generate().then((key) => {
console.log(key);
});
Get the wallet address for a private key
arweave.wallets.jwkToAddress(jwk).then((address) => {
console.log(address);
});
Get an address balance
Get the balance of a wallet address, all amounts by default are returned in winston.
arweave.wallets.getBalance('1seRanklLU_1VTGkEk7P0xAwMJfA7owA1JHW5KyZKlY').then((balance) => {
let winston = balance;
let ar = arweave.ar.winstonToAr(balance);
console.log(winston);
console.log(ar);
});
Get the last transaction ID from a wallet
arweave.wallets.getLastTransactionID('1seRanklLU_1VTGkEk7P0xAwMJfA7owA1JHW5KyZKlY').then((transactionId) => {
console.log(transactionId);
});
Transactions
Transactions are the building blocks of the Arweave permaweb, they can send AR betwen wallet addresses, or store data on the Arweave network.
The create transaction methods simply creates and returns an unsigned transaction object, you must sign the transaction and submit it separeately using the transactions.sign
and transactions.submit
methods.
Modifying a transaction object after signing it will invalidate the signature, this will cause it to be rejected by the network if submitted in that state. Transaction prices are based on the size of the data field, so modifying the data field after a transaction has been created isn't recommended as you'll need to manually update the price.
The transaction ID is a hash of the transaction signature, so a transaction ID can't be known until its contents are finalised and it has been signed.
Create a data transaction
Data transactions are used to store data on the Arweave permaweb, they can contain HTML data and be serverd like webpages or they can contain any arbitrary data.
let key = await arweave.wallets.generate();
let transactionA = arweave.createTransaction({
data: '<html><head><meta charset="UTF-8"><title>Hello world!</title></head><body></body></html>'
}, jwk);
let transactionB = arweave.createTransaction({
data: Buffer.from('Some data', 'utf8')
}, jwk);
console.log(transactionA);
Create a wallet to wallet transaction
let key = await arweave.wallets.generate();
let transaction = arweave.createTransaction({
target: '1seRanklLU_1VTGkEk7P0xAwMJfA7owA1JHW5KyZKlY',
quantity: arweave.ar.arToWinston('10.5')
}, jwk);
console.log(transaction);
Add tags to a transaction
Metadata can be added to transactions through tags, these are simple key/value attributes that can be used to document the contents of a transaction or provide related data.
ARQL uses tags when searching for transactions.
The Content-Type
is a reserved tag and is used to set the data content type. For example, a transaction with HTML data and a content type tag of text/html
will be served as a HTML page and render correctly in browsers,
if the content type is set to text/plain
then it will be served as a plain text document and not render in browsers.
let key = await arweave.wallets.generate();
let transaction = await arweave.createTransaction({
data: '<html><head><meta charset="UTF-8"><title>Hello world!</title></head><body></body></html>',
}, key);
transaction.addTag('Content-Type', 'text/html');
transaction.addTag('key2', 'value2');
console.log(transaction);
Sign a transaction
let key = await arweave.wallets.generate();
let transaction = await arweave.createTransaction({
target: '1seRanklLU_1VTGkEk7P0xAwMJfA7owA1JHW5KyZKlY',
quantity: arweave.ar.arToWinston('10.5')
}, key);
await arweave.transactions.sign(transaction, key);
console.log(transaction);
Submit a transaction
Once a transaction is submitted to the network it'll be broadcast around all nodes and mined into a block.
let key = await arweave.wallets.generate();
let transaction = await arweave.createTransaction({
target: '1seRanklLU_1VTGkEk7P0xAwMJfA7owA1JHW5KyZKlY',
quantity: arweave.ar.arToWinston('10.5')
}, key);
await arweave.transactions.sign(transaction, key);
const response = await arweave.transactions.post(transaction);
console.log(response.status);
Get a transaction status
arweave.transactions.getStatus('bNbA3TEQVL60xlgCcqdz4ZPHFZ711cZ3hmkpGttDt_U').then(status => {
console.log(status);
})
Get a transaction
Fetch a transaction from the connected arweave node. The data and tags are base64 encoded, these can be decoded using the built in helper methods.
const transaction = arweave.transactions.get('hKMMPNh_emBf8v_at1tFzNYACisyMQNcKzeeE1QE9p8').then(transaction => {
console.log(transaction);
});
Get transaction data
You can get the transaction data from a transaction ID without having to get the entire transaction
arweave.transactions.getData('bNbA3TEQVL60xlgCcqdz4ZPHFZ711cZ3hmkpGttDt_U').then(data => {
console.log(data);
});
getData('bNbA3TEQVL60xlgCcqdz4ZPHFZ711cZ3hmkpGttDt_U', {decode: true}).then(data => {
console.log(data);
});
arweave.transactions.getData('bNbA3TEQVL60xlgCcqdz4ZPHFZ711cZ3hmkpGttDt_U', {decode: true, string: true}).then(data => {
console.log(data);
});
Decode data and tags from transactions
const transaction = arweave.transactions.get('bNbA3TEQVL60xlgCcqdz4ZPHFZ711cZ3hmkpGttDt_U').then(transaction => {
console.log(transaction.get('signature'));
console.log(transaction.get('data'));
console.log(transaction.get('data', {decode: true}));
console.log(transaction.get('data', {decode: true, string: true}));
transaction.get('tags').forEach(tag => {
let key = tag.get('name', {decode: true, string: true});
let value = tag.get('value', {decode: true, string: true});
console.log(`${key} : ${value}`);
});
});
ArQL
ArQL allows you to search for transactions by tags or by wallet address.
The allowed operators are and
, or
, and equals
which all accept exactly two expressions. Therefore, to and
three or more expressions together, you will need to nest and
expressions. The same goes for or
. Searching by wallet is done by using the special tag from
.
arweave.arql
takes the ArQL query as an object and returns the matching transaction IDs as an array of strings.
const txids = await arweave.arql({
op: "and",
expr1: {
op: "equals",
expr1: "from",
expr2: "hnRI7JoN2vpv__w90o4MC_ybE9fse6SUemwQeY8hFxM"
},
expr2: {
op: "or",
expr1: {
op: "equals",
expr1: "type",
expr2: "post"
},
expr2: {
op: "equals",
expr1: "type",
expr2: "comment"
}
}
})
console.log(txids)
There are a number of community produced helper packages for building ArQL queries.