feat: wrap public key in a class

This commit is contained in:
Michael Vines
2018-09-30 18:42:45 -07:00
parent 2c3208090c
commit ca6965f8c2
15 changed files with 180 additions and 95 deletions

View File

@ -1,10 +1,10 @@
// @flow
import {Account} from '../src/account';
import {PublicKey} from '../src/publickey';
test('generate new account', () => {
const account = new Account();
expect(account.publicKey.length).toBeGreaterThanOrEqual(43);
expect(account.publicKey.length).toBeLessThanOrEqual(44);
expect(PublicKey.isPublicKey(account.publicKey)).toBeTruthy();
expect(account.secretKey).toHaveLength(64);
});
@ -17,5 +17,5 @@ test('account from secret key', () => {
74, 101, 217, 139, 135, 139, 153, 34
]);
const account = new Account(secretKey);
expect(account.publicKey).toBe('2q7pyhPwAwZ3QMfZrnAbDhnh9mDUqycszcpf86VgQxhF');
expect(account.publicKey.toBase58()).toBe('2q7pyhPwAwZ3QMfZrnAbDhnh9mDUqycszcpf86VgQxhF');
});

View File

@ -26,7 +26,7 @@ test('get account info - error', () => {
url,
{
method: 'getAccountInfo',
params: [account.publicKey],
params: [account.publicKey.toBase58()],
},
errorResponse,
]);
@ -44,7 +44,7 @@ test('get balance', async () => {
url,
{
method: 'getBalance',
params: [account.publicKey],
params: [account.publicKey.toBase58()],
},
{
error: null,
@ -56,23 +56,6 @@ test('get balance', async () => {
expect(balance).toBeGreaterThanOrEqual(0);
});
test('get balance - error', () => {
const connection = new Connection(url);
const invalidPublicKey = 'not a public key';
mockRpc.push([
url,
{
method: 'getBalance',
params: [invalidPublicKey],
},
errorResponse,
]);
expect(connection.getBalance(invalidPublicKey))
.rejects.toThrow(errorMessage);
});
test('confirm transaction - error', () => {
const connection = new Connection(url);
@ -174,7 +157,7 @@ test('request airdrop', async () => {
url,
{
method: 'requestAirdrop',
params: [account.publicKey, 40],
params: [account.publicKey.toBase58(), 40],
},
{
error: null,
@ -185,7 +168,7 @@ test('request airdrop', async () => {
url,
{
method: 'requestAirdrop',
params: [account.publicKey, 2],
params: [account.publicKey.toBase58(), 2],
},
{
error: null,
@ -196,7 +179,7 @@ test('request airdrop', async () => {
url,
{
method: 'getBalance',
params: [account.publicKey],
params: [account.publicKey.toBase58()],
},
{
error: null,
@ -214,7 +197,7 @@ test('request airdrop', async () => {
url,
{
method: 'getAccountInfo',
params: [account.publicKey],
params: [account.publicKey.toBase58()],
},
{
error: null,
@ -226,31 +209,13 @@ test('request airdrop', async () => {
tokens: 42,
userdata: [],
}
}
]);
const accountInfo = await connection.getAccountInfo(account.publicKey);
expect(accountInfo.tokens).toBe(42);
expect(accountInfo.userdata).toHaveLength(0);
expect(accountInfo.programId).toBe(SystemProgram.programId);
});
test('request airdrop - error', () => {
const invalidPublicKey = 'not a public key';
const connection = new Connection(url);
mockRpc.push([
url,
{
method: 'requestAirdrop',
params: [invalidPublicKey, 1],
},
errorResponse
]);
expect(connection.requestAirdrop(invalidPublicKey, 1))
.rejects.toThrow(errorMessage);
expect(accountInfo.programId).toEqual(SystemProgram.programId);
});
test('transaction', async () => {
@ -262,7 +227,7 @@ test('transaction', async () => {
url,
{
method: 'requestAirdrop',
params: [accountFrom.publicKey, 12],
params: [accountFrom.publicKey.toBase58(), 12],
},
{
error: null,
@ -273,7 +238,7 @@ test('transaction', async () => {
url,
{
method: 'getBalance',
params: [accountFrom.publicKey],
params: [accountFrom.publicKey.toBase58()],
},
{
error: null,
@ -287,7 +252,7 @@ test('transaction', async () => {
url,
{
method: 'requestAirdrop',
params: [accountTo.publicKey, 21],
params: [accountTo.publicKey.toBase58(), 21],
},
{
error: null,
@ -298,7 +263,7 @@ test('transaction', async () => {
url,
{
method: 'getBalance',
params: [accountTo.publicKey],
params: [accountTo.publicKey.toBase58()],
},
{
error: null,
@ -375,7 +340,7 @@ test('transaction', async () => {
url,
{
method: 'getBalance',
params: [accountFrom.publicKey],
params: [accountFrom.publicKey.toBase58()],
},
{
error: null,
@ -388,7 +353,7 @@ test('transaction', async () => {
url,
{
method: 'getBalance',
params: [accountTo.publicKey],
params: [accountTo.publicKey.toBase58()],
},
{
error: null,

View File

@ -0,0 +1,51 @@
// @flow
import {PublicKey} from '../src/publickey';
test('invalid', () => {
expect(() => {
new PublicKey([
3, 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
]);
}).toThrow();
expect(() => {
new PublicKey('0x300000000000000000000000000000000000000000000000000000000000000000000');
}).toThrow();
expect(() => {
new PublicKey('135693854574979916511997248057056142015550763280047535983739356259273198796800000');
}).toThrow();
});
test('equals', () => {
const arrayKey = new PublicKey([
3, 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,
]);
const hexKey = new PublicKey('0x300000000000000000000000000000000000000000000000000000000000000');
const decimalKey = new PublicKey('1356938545749799165119972480570561420155507632800475359837393562592731987968');
expect(arrayKey.equals(hexKey)).toBeTruthy();
expect(arrayKey.equals(decimalKey)).toBeTruthy();
});
test('isPublicKey', () => {
const key = new PublicKey('0x100000000000000000000000000000000000000000000000000000000000000');
expect(PublicKey.isPublicKey(key)).toBeTruthy();
expect(PublicKey.isPublicKey({})).toBeFalsy();
});
test('toBase58', () => {
const key = new PublicKey('0x300000000000000000000000000000000000000000000000000000000000000');
expect(key.toBase58()).toBe('CiDwVBFgWV9E5MvXWoLgnEgn2hK7rJikbvfWavzAQz3');
});
test('toBuffer', () => {
const key = new PublicKey('0x300000000000000000000000000000000000000000000000000000000000000');
expect(key.toBuffer()).toHaveLength(32);
expect(key.toBase58()).toBe('CiDwVBFgWV9E5MvXWoLgnEgn2hK7rJikbvfWavzAQz3');
const key2 = new PublicKey('0x000000000000000000000000000000000000000000000000000000000000000');
expect(key2.toBuffer()).toHaveLength(32);
expect(key2.toBase58()).toBe('11111111111111111111111111111111');
});