2020-01-02 18:54:43 -07:00
|
|
|
// @flow
|
|
|
|
import * as BufferLayout from 'buffer-layout';
|
|
|
|
|
|
|
|
import type {Blockhash} from './blockhash';
|
|
|
|
import * as Layout from './layout';
|
|
|
|
import {PublicKey} from './publickey';
|
2020-03-05 09:44:56 -07:00
|
|
|
import type {FeeCalculator} from './fee-calculator';
|
|
|
|
import {FeeCalculatorLayout} from './fee-calculator';
|
2020-01-02 18:54:43 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* See https://github.com/solana-labs/solana/blob/0ea2843ec9cdc517572b8e62c959f41b55cf4453/sdk/src/nonce_state.rs#L29-L32
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
const NonceAccountLayout = BufferLayout.struct([
|
2020-03-05 09:44:56 -07:00
|
|
|
BufferLayout.u32('version'),
|
2020-01-02 18:54:43 -07:00
|
|
|
BufferLayout.u32('state'),
|
|
|
|
Layout.publicKey('authorizedPubkey'),
|
2020-01-07 17:57:56 -07:00
|
|
|
Layout.publicKey('nonce'),
|
2020-03-05 09:44:56 -07:00
|
|
|
BufferLayout.struct([FeeCalculatorLayout], 'feeCalculator'),
|
2020-01-02 18:54:43 -07:00
|
|
|
]);
|
|
|
|
|
2020-03-05 09:44:56 -07:00
|
|
|
export const NONCE_ACCOUNT_LENGTH = NonceAccountLayout.span;
|
|
|
|
|
2020-01-02 18:54:43 -07:00
|
|
|
/**
|
|
|
|
* NonceAccount class
|
|
|
|
*/
|
|
|
|
export class NonceAccount {
|
|
|
|
authorizedPubkey: PublicKey;
|
|
|
|
nonce: Blockhash;
|
2020-03-05 09:44:56 -07:00
|
|
|
feeCalculator: FeeCalculator;
|
2020-01-02 18:54:43 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Deserialize NonceAccount from the account data.
|
|
|
|
*
|
|
|
|
* @param buffer account data
|
|
|
|
* @return NonceAccount
|
|
|
|
*/
|
|
|
|
static fromAccountData(buffer: Buffer): NonceAccount {
|
|
|
|
const nonceAccount = NonceAccountLayout.decode(buffer, 0);
|
|
|
|
nonceAccount.authorizedPubkey = new PublicKey(
|
|
|
|
nonceAccount.authorizedPubkey,
|
|
|
|
);
|
|
|
|
nonceAccount.nonce = new PublicKey(nonceAccount.nonce).toString();
|
|
|
|
return nonceAccount;
|
|
|
|
}
|
|
|
|
}
|