-
Notifications
You must be signed in to change notification settings - Fork 768
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b4fa9cb
commit 52b7b51
Showing
4 changed files
with
246 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// @flow | ||
import * as BufferLayout from 'buffer-layout'; | ||
|
||
import type {Blockhash} from './blockhash'; | ||
import * as Layout from './layout'; | ||
import {PublicKey} from './publickey'; | ||
|
||
/** | ||
* See https://github.com/solana-labs/solana/blob/0ea2843ec9cdc517572b8e62c959f41b55cf4453/sdk/src/nonce_state.rs#L29-L32 | ||
* | ||
* @private | ||
*/ | ||
const NonceAccountLayout = BufferLayout.struct([ | ||
BufferLayout.u32('state'), | ||
Layout.publicKey('authorizedPubkey'), | ||
Layout.publicKey('hash'), | ||
]); | ||
|
||
/** | ||
* NonceAccount class | ||
*/ | ||
export class NonceAccount { | ||
authorizedPubkey: PublicKey; | ||
nonce: Blockhash; | ||
|
||
/** | ||
* 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
// @flow | ||
|
||
import bs58 from 'bs58'; | ||
|
||
import {Account, Connection, SystemProgram} from '../src'; | ||
import {mockRpc, mockRpcEnabled} from './__mocks__/node-fetch'; | ||
import {mockGetRecentBlockhash} from './mockrpc/get-recent-blockhash'; | ||
import {url} from './url'; | ||
|
||
if (!mockRpcEnabled) { | ||
// Testing max commitment level takes around 20s to complete | ||
jest.setTimeout(30000); | ||
} | ||
|
||
test('create and query nonce account', async () => { | ||
const from = new Account(); | ||
const nonceAccount = new Account(); | ||
const connection = new Connection(url, 'recent'); | ||
|
||
mockRpc.push([ | ||
url, | ||
{ | ||
method: 'getMinimumBalanceForRentExemption', | ||
params: [68, {commitment: 'recent'}], | ||
}, | ||
{ | ||
error: null, | ||
result: 50, | ||
}, | ||
]); | ||
|
||
const minimumAmount = await connection.getMinimumBalanceForRentExemption( | ||
SystemProgram.nonceSpace, | ||
'recent', | ||
); | ||
|
||
mockRpc.push([ | ||
url, | ||
{ | ||
method: 'requestAirdrop', | ||
params: [ | ||
from.publicKey.toBase58(), | ||
minimumAmount * 2, | ||
{commitment: 'recent'}, | ||
], | ||
}, | ||
{ | ||
error: null, | ||
result: | ||
'1WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk', | ||
}, | ||
]); | ||
|
||
await connection.requestAirdrop(from.publicKey, minimumAmount * 2); | ||
|
||
mockRpc.push([ | ||
url, | ||
{ | ||
method: 'getBalance', | ||
params: [from.publicKey.toBase58(), {commitment: 'recent'}], | ||
}, | ||
{ | ||
error: null, | ||
result: minimumAmount * 2, | ||
}, | ||
]); | ||
|
||
const balance = await connection.getBalance(from.publicKey); | ||
expect(balance).toBe(minimumAmount * 2); | ||
|
||
mockGetRecentBlockhash('recent'); | ||
mockRpc.push([ | ||
url, | ||
{ | ||
method: 'sendTransaction', | ||
}, | ||
{ | ||
error: null, | ||
result: | ||
'3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk', | ||
}, | ||
]); | ||
|
||
const transaction = SystemProgram.createNonceAccount( | ||
from.publicKey, | ||
nonceAccount.publicKey, | ||
from.publicKey, | ||
minimumAmount, | ||
); | ||
await connection.sendTransaction(transaction, from, nonceAccount); | ||
|
||
const expectedData = Buffer.alloc(68); | ||
expectedData.writeInt32LE(1, 0); | ||
from.publicKey.toBuffer().copy(expectedData, 4); | ||
const mockNonce = new Account(); | ||
mockNonce.publicKey.toBuffer().copy(expectedData, 36); | ||
|
||
mockRpc.push([ | ||
url, | ||
{ | ||
method: 'getAccountInfo', | ||
params: [nonceAccount.publicKey.toBase58(), {commitment: 'recent'}], | ||
}, | ||
{ | ||
error: null, | ||
result: { | ||
owner: [ | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
], | ||
lamports: minimumAmount, | ||
data: [...expectedData], | ||
executable: false, | ||
}, | ||
}, | ||
]); | ||
// | ||
const nonceAccountData = await connection.getNonce( | ||
nonceAccount.publicKey, | ||
'recent', | ||
); | ||
expect(nonceAccountData.authorizedPubkey).toEqual(from.publicKey); | ||
expect(bs58.decode(nonceAccountData.nonce).length).toBeGreaterThan(30); | ||
}); |