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 {struct} from 'superstruct';
|
|
|
|
|
2018-09-13 18:48:51 -07:00
|
|
|
import {bs58DecodePublicKey, Transaction} from './transaction';
|
2018-08-23 10:52:48 -07:00
|
|
|
import type {Account, PublicKey} from './account';
|
2018-09-13 18:48:51 -07:00
|
|
|
import type {TransactionSignature, TransactionId} from './transaction';
|
2018-08-23 10:52:48 -07:00
|
|
|
|
2018-08-23 20:10:30 -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
|
|
|
|
*/
|
2018-08-23 20:10:30 -07:00
|
|
|
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-09-13 18:48:51 -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> {
|
2018-09-13 18:48:51 -07:00
|
|
|
const transaction = new Transaction();
|
|
|
|
transaction.fee = 0;
|
|
|
|
transaction.lastId = await this.getLastId();
|
|
|
|
transaction.keys[0] = from.publicKey;
|
|
|
|
transaction.keys[1] = to;
|
|
|
|
|
|
|
|
// Forge a simple Budget Pay contract into `userdata`
|
|
|
|
// TODO: Clean this up
|
|
|
|
const userdata = Buffer.alloc(68); // 68 = serialized size of Budget enum
|
|
|
|
userdata.writeUInt32LE(60, 0);
|
|
|
|
userdata.writeUInt32LE(amount, 12); // u64
|
|
|
|
userdata.writeUInt32LE(amount, 28); // u64
|
|
|
|
const toData = bs58DecodePublicKey(to);
|
|
|
|
toData.copy(userdata, 36);
|
|
|
|
transaction.userdata = userdata;
|
|
|
|
|
|
|
|
transaction.sign(from);
|
2018-08-24 17:14:58 -07:00
|
|
|
|
|
|
|
// Send it
|
2018-09-13 18:48:51 -07:00
|
|
|
const wireTransaction = transaction.serialize();
|
2018-08-24 17:14:58 -07:00
|
|
|
const unsafeRes = await this._rpcRequest('sendTransaction', [[...wireTransaction]]);
|
2018-08-23 20:10:30 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|