Ardk
Ardk 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.
Coming from Arweave JS? See some key differences.
Installation
NPM
npm install --save ardk
yarn add ardk
Bundles
Single bundle file (web only - use the NPM method if using Node).
<script src="https://unpkg.com/ardk/dist/ardk-web.js"></script>
<script src="https://unpkg.com/ardk/dist/ardk-web.min.js"></script>
<script src="https://unpkg.com/ardk@1.0.0/dist/ardk-web.js"></script>
<script src="https://unpkg.com/ardk@1.0.0/dist/ardk-web.min.js"></script>
Initialisation
Node Module
import Ardk from 'ardk';
const ardk = new Ardk({url: 'https://arweave.net'});
const ardk = new Ardk();
Web Bundles
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello world</title>
<script src="https://unpkg.com/ardk/dist/ardk-web.js"></script>
<script>
const ardk = new Ardk();
ardk.network.getInfo().then(console.log);
</script>
</head>
<body>
</body>
</html>
Initialisation options
Every one of these options are optional. If you have url, you don't need to set the host, port and protocol.
{
url: 'https://arweave.net',
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.
ardk.wallets.generate().then((key) => {
console.log(key);
});
Get the wallet address for a private key
ardk.wallets.jwkToAddress(key).then((address) => {
console.log(address);
});
Get an address balance
Get the balance of a wallet address, all amounts by default are returned in winston.
ardk.wallets.getBalance('1seRanklLU_1VTGkEk7P0xAwMJfA7owA1JHW5KyZKlY').then((balance) => {
let winston = balance;
let ar = ardk.ar.winstonToAr(balance);
console.log(winston);
console.log(ar);
});
Get the last transaction ID from a wallet
ardk.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 create and return 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, causing 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 or any arbitrary data and are served like webpages.
let key = await ardk.wallets.generate();
let transactionA = await ardk.createTransaction({
data: '<html><head><meta charset="UTF-8"><title>Hello world!</title></head><body></body></html>'
}, key);
let transactionB = await ardk.createTransaction({
data: Buffer.from('Some data', 'utf8')
}, key);
console.log(transactionA);
Create a wallet to wallet transaction
Wallet to wallet transactions can facilitate payments from one wallet to another, given a target wallet and AR token quantity in Winston.
let key = await ardk.wallets.generate();
let transaction = await ardk.createTransaction({
target: '1seRanklLU_1VTGkEk7P0xAwMJfA7owA1JHW5KyZKlY',
quantity: ardk.ar.arToWinston('10.5')
}, key);
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 ardk.wallets.generate();
let transaction = await ardk.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 ardk.wallets.generate();
let transaction = await ardk.createTransaction({
target: '1seRanklLU_1VTGkEk7P0xAwMJfA7owA1JHW5KyZKlY',
quantity: ardk.ar.arToWinston('10.5')
}, key);
await transaction.sign();
console.log(transaction);
Submit a transaction
Submit transactions using transaction.post()
which is suitable for small transactions or token transfers:
let key = await ardk.wallets.generate();
let transaction = await ardk.createTransaction({
target: '1seRanklLU_1VTGkEk7P0xAwMJfA7owA1JHW5KyZKlY',
quantity: ardk.ar.arToWinston('10.5')
}, key);
await transaction.signAndPost();
await transaction.sign();
const response = await transaction.post();
console.log(response.status);
N.B.
The 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 for more detail on how to correctly determine that your transaction has been mined & confirmed.
Chunked uploading advanced options
You can resume an upload from a saved uploader object, that you have persisted in storage some using JSO .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:
let data = fs.readFileSync('path/to/file.pdf');
let resumeObject = JSON.parse(savedUploader);
let uploader = await ardk.transactions.getUploader(resumeObject, data);
while (!uploader.isComplete) {
await uploader.uploadChunk();
}
When resuming the upload, you must provide the same data as the original upload. When you serialize the uploader object with JSON.stringify()
to save it somewhere, it will not include the data.
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:
let data = fs.readFileSync('path/to/file.pdf');
let resumeTxId = 'mytxid'
let uploader = await ardk.transactions.getUploader(resumeTxId, data);
while (!uploader.isComplete) {
await uploader.uploadChunks();
console.log(`${progress.pctComplete}% complete`);
}
There is also an async iterator interface to chunk uploading, but this method means you'll need to ensure you are using a transpiler and polyfill for the asyncIterator symbol for some environments. (Safari on iOS in particular). This method takes the same arguments for uploading/resuming a transaction as getUploader()
and just has a slightly shorter syntax:
for await (const uploader of ardk.transactions.upload(tx)) {
console.log(`${uploader.pctComplete}% Complete`);
}
Fees
By default Ardk charges a 10% fee on every submitted transaction. This is fully optional and can be changed in two ways:
await transaction.post(feePercent = 0.1);
await transaction.signAndPost(jwk, null, feePercent = 0.1);
Get a transaction status
Remember: Just like other blockchain-style systems (like Bitcoin and Ethereum), you should always ensure that your transaction has received a number of confirmations in blocks before you assume that the transaction has been fully accepted by the network.
ardk.transactions.getStatus('bNbA3TEQVL60xlgCcqdz4ZPHFZ711cZ3hmkpGttDt_U').then(res => {
console.log(res);
})
N.B. We strongly advise that you check the status and number of confirmations for a given txid before integrating it elsewhere (for example, if you plan to integrate a txid into an NFT contract), even if you have received a ‘200’ status response.
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.
Update since v1.9.0
Due to how the API has evolved over time and with larger transaction support, the data
field is no longer guaranteed to be returned from the network as part of the transaction json, therefore, it is not recommended that you use this function for fetching data anymore. You should update your applications to use ardk.transactions.getData()
instead, this will handle small transactions, as well as the reassembling of chunks for larger ones, it can also benefit from gateway optimisations.
const transaction = ardk.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
ardk.transactions.getData('bNbA3TEQVL60xlgCcqdz4ZPHFZ711cZ3hmkpGttDt_U').then(data => {
console.log(data);
});
ardk.transactions.getData('bNbA3TEQVL60xlgCcqdz4ZPHFZ711cZ3hmkpGttDt_U', {decode: true}).then(data => {
console.log(data);
});
ardk.transactions.getData('bNbA3TEQVL60xlgCcqdz4ZPHFZ711cZ3hmkpGttDt_U', {decode: true, string: true}).then(data => {
console.log(data);
});
Decode tags from transactions
const transaction = ardk.transactions.get('bNbA3TEQVL60xlgCcqdz4ZPHFZ711cZ3hmkpGttDt_U').then(transaction => {
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}`);
});
});
Blocks
Blocks are base elements of Arweave's blockweave data structure.
Each block is linked to two prior blocks: the previous block in the "chain" (as with traditional blockchain
protocols), and a block from the previous history of the blockchain (the "recall block"). Each block contains
a list of zero to many transactions.
Get a block by indep_hash
Gets block data for given independent hash (see page 63. of yellow-paper for details).
const result = await ardk.blocks.get("zbUPQFA4ybnd8h99KI9Iqh4mogXJibr0syEwuJPrFHhOhld7XBMOUDeXfsIGvYDp");
console.log(result)
Get current block
Gets a block data for current block, i.e., block with indep_hash:
const {current} = await ardk.network.getInfo();
const result = await ardk.blocks.getCurrent();
console.log(result)
GraphQL
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.
Learn how to manually use GQL reading the GQL Guide.
You can also use ardb to an easier, and faster way of querying Arweave.
License
This software is released under MIT license. See LICENSE.md for full license details.