Skip to content

Instantly share code, notes, and snippets.

@cedriking
Last active March 21, 2020 19:09
Show Gist options
  • Save cedriking/e84cffd909228f7db8d318f0bfc92e07 to your computer and use it in GitHub Desktop.
Save cedriking/e84cffd909228f7db8d318f0bfc92e07 to your computer and use it in GitHub Desktop.
Arweave Global API draft

Arweave global object API

Getting the user's wallet address

arweave.wallets(): Promise<Wallet[]>

Example:

await arweave.wallets();
// ["U2SjcTBXhsVs0kuSvNhPf84H2slCRiTBZtCCKoWMzvR"]

Trigger the extension to ask the user to authorize the app to access their wallet address. If authorized, the promise will resolve with a list of a single address, otherwise, the promise will reject.

Getting the current network

arweave.network(): Promise<{
  network: string,
  host: string
}>

Example:

await arweave.network();
// {
//   network: "arweave.N.1",
//   host: "https://arweave.net"
// }

Lets the application retrieve the network and host to which the extension is currently connected to. Rejects if the extension fails to connect to the selected network.

Sign an arbitrary message

arweave.signMessage(message: ArrayBuffer): Promise<Uint8Array>

Example:

const te = new TextEncoder();
const message = te.encode("hello");
await arweave.signMessage(message);
// Uint8Array [ ... ]

Triggers the extension to ask the user to allow signing the given message. The resulting signature should start with the prefix ARWEAVE_SIGNED_MESSAGE:. Rejects if the user denies signing the message.

Send a transaction

arweave.sendTransaction(foo: {
  recipient?: Wallet,
  amount?: Winston,
  fee?: Winston,
  data?: ArrayBuffer,
  tags?: [string, string][],
}): Promise<string>

Example:

const te = new TextEncoder();
await arweave.sendTransaction({
  data: te.encode('{"foo":"bar"}'),
  tags: [
    ["app_name", "foobar"],
    ["Content-Type", "application/json"]
  ]
});
// "NpeIbi93igKhE5lKUMhH5dFmyEsNGC0fb2Qysggd-kM"

Triggers the extension to present the user with a transaction dialog letting the user see the cost of that transaction and adjust its fee. Resolves to the new transaction's ID if successfully submitted to the network. Rejects otherwise.

Get a transaction's status

arweave.getStatus(txid: string): Promise<string>

Example:

await arweave.getStatus("NpeIbi93igKhE5lKUMhH5dFmyEsNGC0fb2Qysggd-kM");
// "pending"

Gets a transactions current status.

Get a baseline fee

Should this be .getPrice?

arweave.getFee(byteSize?: number, recipient?: Wallet): Promise<Winston>

Example:

await arweave.getFee(1048576, "U2SjcTBXhsVs0kuSvNhPf84H2slCRiTBZtCCKoWMzvR");
// "6523795207"

Gets the estimated fee for the given byte size and recipient.

Listen to wallet change

arweave.on("walletsChange", handleWalletsChange: (wallets: Wallet[]) => void): void

Example:

arweave.on("walletsChange", newWallets => {
  // Update app state...
});

Listen to network change

arweave.on("networkChange", handleNetworkChange: (network: { network: string, host: string }) => void): void

Example:

arweave.on("networkChange", newNetwork => {
  // Update app state...
});

Misc. types:

type Wallet = string;
type Winston = string;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment