Catch up to solana 0.8 Transaction wire format changes
This commit is contained in:
parent
fa5f3d81fd
commit
851ca7acc9
|
@ -3,21 +3,11 @@
|
||||||
import assert from 'assert';
|
import assert from 'assert';
|
||||||
import fetch from 'node-fetch';
|
import fetch from 'node-fetch';
|
||||||
import jayson from 'jayson/lib/client/browser';
|
import jayson from 'jayson/lib/client/browser';
|
||||||
import nacl from 'tweetnacl';
|
|
||||||
import {struct} from 'superstruct';
|
import {struct} from 'superstruct';
|
||||||
import bs58 from 'bs58';
|
|
||||||
|
|
||||||
|
import {bs58DecodePublicKey, Transaction} from './transaction';
|
||||||
import type {Account, PublicKey} from './account';
|
import type {Account, PublicKey} from './account';
|
||||||
|
import type {TransactionSignature, TransactionId} from './transaction';
|
||||||
/**
|
|
||||||
* @typedef {string} TransactionSignature
|
|
||||||
*/
|
|
||||||
export type TransactionSignature = string;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @typedef {string} TransactionId
|
|
||||||
*/
|
|
||||||
export type TransactionId = string;
|
|
||||||
|
|
||||||
type RpcRequest = (methodName: string, args: Array<any>) => any;
|
type RpcRequest = (methodName: string, args: Array<any>) => any;
|
||||||
|
|
||||||
|
@ -228,54 +218,31 @@ export class Connection {
|
||||||
return res.result;
|
return res.result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send tokens to another account
|
* Send tokens to another account
|
||||||
*/
|
*/
|
||||||
async sendTokens(from: Account, to: PublicKey, amount: number): Promise<TransactionSignature> {
|
async sendTokens(from: Account, to: PublicKey, amount: number): Promise<TransactionSignature> {
|
||||||
const lastId = await this.getLastId();
|
const transaction = new Transaction();
|
||||||
const fee = 0;
|
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: Redo this...
|
// 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;
|
||||||
|
|
||||||
// Build the transaction data to be signed.
|
transaction.sign(from);
|
||||||
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);
|
|
||||||
|
|
||||||
|
|
||||||
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
|
// Send it
|
||||||
|
const wireTransaction = transaction.serialize();
|
||||||
const unsafeRes = await this._rpcRequest('sendTransaction', [[...wireTransaction]]);
|
const unsafeRes = await this._rpcRequest('sendTransaction', [[...wireTransaction]]);
|
||||||
const res = SendTokensRpcResult(unsafeRes);
|
const res = SendTokensRpcResult(unsafeRes);
|
||||||
if (res.error) {
|
if (res.error) {
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
// @flow
|
// @flow
|
||||||
export {Account} from './account';
|
export {Account} from './account';
|
||||||
export {Connection} from './connection';
|
export {Connection} from './connection';
|
||||||
|
export {Transaction} from './transaction';
|
||||||
|
|
|
@ -0,0 +1,134 @@
|
||||||
|
// @flow
|
||||||
|
|
||||||
|
import assert from 'assert';
|
||||||
|
import nacl from 'tweetnacl';
|
||||||
|
import bs58 from 'bs58';
|
||||||
|
|
||||||
|
import type {Account, PublicKey} from './account';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
export function bs58DecodePublicKey(key: PublicKey): Buffer {
|
||||||
|
const keyBytes = Buffer.from(bs58.decode(key));
|
||||||
|
assert(keyBytes.length === 32);
|
||||||
|
return keyBytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {string} TransactionSignature
|
||||||
|
*/
|
||||||
|
export type TransactionSignature = string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {string} TransactionId
|
||||||
|
*/
|
||||||
|
export type TransactionId = string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mirrors the Transaction struct in src/transaction.rs
|
||||||
|
*/
|
||||||
|
export class Transaction {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Current signature of the transaction. Typically created by invoking the
|
||||||
|
* `sign()` method
|
||||||
|
*/
|
||||||
|
signature: ?Buffer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Public keys to include in this transaction
|
||||||
|
*/
|
||||||
|
keys: Array<PublicKey> = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A recent transaction id. Must be populated by the caller
|
||||||
|
*/
|
||||||
|
lastId: ?TransactionId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fee for this transaction
|
||||||
|
*/
|
||||||
|
fee: number = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Contract input userdata to include
|
||||||
|
*/
|
||||||
|
userdata: Buffer = Buffer.alloc(0);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
_getSignData(): Buffer {
|
||||||
|
const {lastId} = this;
|
||||||
|
if (!lastId) {
|
||||||
|
throw new Error('Transaction lastId required');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start with a Buffer that should be large enough to fit any Transaction
|
||||||
|
const transactionData = Buffer.alloc(2048);
|
||||||
|
|
||||||
|
let pos = 0;
|
||||||
|
|
||||||
|
// serialize `this.keys`
|
||||||
|
transactionData.writeUInt32LE(this.keys.length, pos); // u64
|
||||||
|
pos += 8;
|
||||||
|
for (let key of this.keys) {
|
||||||
|
const keyBytes = bs58DecodePublicKey(key);
|
||||||
|
keyBytes.copy(transactionData, pos);
|
||||||
|
pos += 32;
|
||||||
|
}
|
||||||
|
|
||||||
|
// serialize `this.lastId`
|
||||||
|
{
|
||||||
|
const lastIdBytes = Buffer.from(bs58.decode(lastId));
|
||||||
|
assert(lastIdBytes.length === 32);
|
||||||
|
lastIdBytes.copy(transactionData, pos);
|
||||||
|
pos += 32;
|
||||||
|
}
|
||||||
|
|
||||||
|
// serialize `this.fee`
|
||||||
|
transactionData.writeUInt32LE(this.fee, pos); // u64
|
||||||
|
pos += 8;
|
||||||
|
|
||||||
|
// serialize `this.userdata`
|
||||||
|
if (this.userdata.length > 0) {
|
||||||
|
this.userdata.copy(transactionData, pos);
|
||||||
|
pos += this.userdata.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
return transactionData.slice(0, pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sign the Transaction with the specified account
|
||||||
|
*
|
||||||
|
* The Transaction must be assigned a valid `lastId` before invoking this method
|
||||||
|
*/
|
||||||
|
sign(from: Account) {
|
||||||
|
const transactionData = this._getSignData();
|
||||||
|
this.signature = nacl.sign.detached(transactionData, from.secretKey);
|
||||||
|
assert(this.signature.length === 64);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Serialize the Transaction in the wire format.
|
||||||
|
*
|
||||||
|
* The Transaction must have a valid `signature` before invoking this method
|
||||||
|
*/
|
||||||
|
serialize(): Buffer {
|
||||||
|
const {signature} = this;
|
||||||
|
if (!signature) {
|
||||||
|
throw new Error('Transaction has not been signed');
|
||||||
|
}
|
||||||
|
|
||||||
|
const transactionData = this._getSignData();
|
||||||
|
const wireTransaction = Buffer.alloc(
|
||||||
|
signature.length + transactionData.length
|
||||||
|
);
|
||||||
|
|
||||||
|
Buffer.from(signature).copy(wireTransaction, 0);
|
||||||
|
transactionData.copy(wireTransaction, signature.length);
|
||||||
|
return wireTransaction;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue