solana/web3.js/src/connection.js

289 lines
7.2 KiB
JavaScript
Raw Normal View History

2018-08-23 10:52:48 -07:00
// @flow
import assert from 'assert';
import fetch from 'node-fetch';
import jayson from 'jayson/lib/client/browser';
import nacl from 'tweetnacl';
import {struct} from 'superstruct';
import bs58 from 'bs58';
2018-08-23 10:52:48 -07:00
import type {Account, PublicKey} from './account';
/**
* @typedef {string} TransactionSignature
*/
export type TransactionSignature = string;
/**
* @typedef {string} TransactionId
*/
2018-08-23 16:39:52 -07:00
export type TransactionId = string;
2018-08-23 10:52:48 -07:00
type RpcRequest = (methodName: string, args: Array<any>) => any;
2018-08-23 10:52:48 -07:00
function createRpcRequest(url): RpcRequest {
const server = jayson(
async (request, callback) => {
const options = {
method: 'POST',
body: request,
headers: {
'Content-Type': 'application/json',
}
};
try {
const res = await fetch(url, options);
const text = await res.text();
callback(null, text);
} catch (err) {
callback(err);
}
}
);
return (method, args) => {
return new Promise((resolve, reject) => {
server.request(method, args, (err, response) => {
if (err) {
reject(err);
return;
}
resolve(response);
});
});
};
}
2018-08-24 10:39:51 -07:00
2018-08-24 09:05:23 -07:00
/**
* Expected JSON RPC response for the "getBalance" message
*/
2018-08-23 10:52:48 -07:00
const GetBalanceRpcResult = struct({
jsonrpc: struct.literal('2.0'),
id: 'string',
error: 'any?',
result: 'number?',
});
2018-08-24 09:05:23 -07:00
/**
* Expected JSON RPC response for the "confirmTransaction" message
*/
2018-08-23 10:52:48 -07:00
const ConfirmTransactionRpcResult = struct({
jsonrpc: struct.literal('2.0'),
id: 'string',
error: 'any?',
result: 'boolean?',
});
2018-08-24 09:05:23 -07:00
/**
* Expected JSON RPC response for the "getTransactionCount" message
*/
2018-08-23 10:52:48 -07:00
const GetTransactionCountRpcResult = struct({
jsonrpc: struct.literal('2.0'),
id: 'string',
error: 'any?',
result: 'number?',
});
2018-08-24 09:05:23 -07:00
/**
* Expected JSON RPC response for the "getLastId" message
*/
2018-08-23 10:52:48 -07:00
const GetLastId = struct({
jsonrpc: struct.literal('2.0'),
id: 'string',
error: 'any?',
result: 'string?',
});
2018-08-24 09:05:23 -07:00
/**
* Expected JSON RPC response for the "getFinality" message
*/
2018-08-23 10:52:48 -07:00
const GetFinalityRpcResult = struct({
jsonrpc: struct.literal('2.0'),
id: 'string',
error: 'any?',
result: 'number?',
});
2018-08-24 09:05:23 -07:00
/**
* Expected JSON RPC response for the "requestAirdrop" message
*/
2018-08-23 16:39:52 -07:00
const RequestAirdropRpcResult = struct({
jsonrpc: struct.literal('2.0'),
id: 'string',
error: 'any?',
2018-09-12 17:41:20 -07:00
result: 'string?',
2018-08-23 16:39:52 -07:00
});
2018-08-24 09:05:23 -07:00
/**
* Expected JSON RPC response for the "sendTransaction" message
*/
const SendTokensRpcResult = struct({
jsonrpc: struct.literal('2.0'),
id: 'string',
error: 'any?',
result: 'string?',
});
2018-08-23 10:52:48 -07:00
2018-08-24 09:05:23 -07:00
/**
* A connection to a fullnode JSON RPC endpoint
*/
2018-08-23 10:52:48 -07:00
export class Connection {
_rpcRequest: RpcRequest;
2018-08-24 09:05:23 -07:00
/**
* Establish a JSON RPC connection
*
* @param endpoint URL to the fullnode JSON RPC endpoint
*/
2018-08-23 10:52:48 -07:00
constructor(endpoint: string) {
2018-08-23 19:47:00 -07:00
if (typeof endpoint !== 'string') {
throw new Error('Connection endpoint not specified');
}
2018-08-23 10:52:48 -07:00
this._rpcRequest = createRpcRequest(endpoint);
}
2018-08-24 09:05:23 -07:00
/**
* Fetch the balance for the specified public key
*/
async getBalance(publicKey: PublicKey): Promise<number> {
2018-08-23 10:52:48 -07:00
const unsafeRes = await this._rpcRequest(
'getBalance',
[publicKey]
);
const res = GetBalanceRpcResult(unsafeRes);
if (res.error) {
throw new Error(res.error.message);
}
assert(typeof res.result !== 'undefined');
return res.result;
}
2018-08-24 09:05:23 -07:00
/**
* Confirm the transaction identified by the specified signature
*/
2018-08-23 10:52:48 -07:00
async confirmTransaction(signature: TransactionSignature): Promise<boolean> {
const unsafeRes = await this._rpcRequest(
'confirmTransaction',
[signature]
);
const res = ConfirmTransactionRpcResult(unsafeRes);
if (res.error) {
throw new Error(res.error.message);
}
assert(typeof res.result !== 'undefined');
return res.result;
}
2018-08-24 09:05:23 -07:00
/**
* Fetch the current transaction count of the network
*/
2018-08-23 10:52:48 -07:00
async getTransactionCount(): Promise<number> {
const unsafeRes = await this._rpcRequest('getTransactionCount', []);
const res = GetTransactionCountRpcResult(unsafeRes);
if (res.error) {
throw new Error(res.error.message);
}
assert(typeof res.result !== 'undefined');
return Number(res.result);
}
2018-08-24 09:05:23 -07:00
/**
* Fetch the identifier to the latest transaction on the network
*/
2018-08-23 10:52:48 -07:00
async getLastId(): Promise<TransactionId> {
const unsafeRes = await this._rpcRequest('getLastId', []);
const res = GetLastId(unsafeRes);
if (res.error) {
throw new Error(res.error.message);
}
assert(typeof res.result !== 'undefined');
return res.result;
}
2018-08-24 09:05:23 -07:00
/**
* Return the current network finality time in millliseconds
*/
2018-08-23 10:52:48 -07:00
async getFinality(): Promise<number> {
const unsafeRes = await this._rpcRequest('getFinality', []);
const res = GetFinalityRpcResult(unsafeRes);
if (res.error) {
throw new Error(res.error.message);
}
assert(typeof res.result !== 'undefined');
return Number(res.result);
}
2018-08-24 09:05:23 -07:00
/**
* Request an allocation of tokens to the specified account
*/
2018-09-12 17:41:20 -07:00
async requestAirdrop(to: PublicKey, amount: number): Promise<TransactionSignature> {
2018-08-23 16:39:52 -07:00
const unsafeRes = await this._rpcRequest('requestAirdrop', [to, amount]);
const res = RequestAirdropRpcResult(unsafeRes);
if (res.error) {
throw new Error(res.error.message);
}
assert(typeof res.result !== 'undefined');
2018-09-12 17:41:20 -07:00
return res.result;
2018-08-23 10:52:48 -07:00
}
2018-08-24 09:05:23 -07:00
/**
* Send tokens to another account
*/
2018-08-23 10:52:48 -07:00
async sendTokens(from: Account, to: PublicKey, amount: number): Promise<TransactionSignature> {
const lastId = await this.getLastId();
const fee = 0;
//
// TODO: Redo this...
//
// Build the transaction data to be signed.
const transactionData = Buffer.alloc(124);
transactionData.writeUInt32LE(amount, 4); // u64
transactionData.writeUInt32LE(amount - fee, 20); // u64
transactionData.writeUInt32LE(32, 28); // length of public key (u64)
{
const toBytes = Buffer.from(bs58.decode(to));
assert(toBytes.length === 32);
toBytes.copy(transactionData, 36);
}
transactionData.writeUInt32LE(32, 68); // length of last id (u64)
{
const lastIdBytes = Buffer.from(bs58.decode(lastId));
assert(lastIdBytes.length === 32);
lastIdBytes.copy(transactionData, 76);
}
// Sign it
const signature = nacl.sign.detached(transactionData, from.secretKey);
assert(signature.length === 64);
// Build the over-the-wire transaction buffer
const wireTransaction = Buffer.alloc(236);
wireTransaction.writeUInt32LE(64, 0); // signature length (u64)
Buffer.from(signature).copy(wireTransaction, 8);
2018-08-24 10:39:51 -07:00
wireTransaction.writeUInt32LE(32, 72); // public key length (u64)
{
const fromBytes = Buffer.from(bs58.decode(from.publicKey));
assert(fromBytes.length === 32);
fromBytes.copy(wireTransaction, 80);
}
transactionData.copy(wireTransaction, 112);
// Send it
const unsafeRes = await this._rpcRequest('sendTransaction', [[...wireTransaction]]);
const res = SendTokensRpcResult(unsafeRes);
if (res.error) {
throw new Error(res.error.message);
}
assert(typeof res.result !== 'undefined');
assert(res.result);
return res.result;
2018-08-23 10:52:48 -07:00
}
}