fix: allow Uint8Array and Array<number> where Buffer is accepted
This commit is contained in:
parent
6ba2f1d524
commit
6a7115b8bd
|
@ -3,7 +3,7 @@ declare module '@solana/web3.js' {
|
||||||
|
|
||||||
// === src/publickey.js ===
|
// === src/publickey.js ===
|
||||||
export class PublicKey {
|
export class PublicKey {
|
||||||
constructor(value: number | string | Buffer | Array<number>);
|
constructor(value: number | string | Buffer | Uint8Array | Array<number>);
|
||||||
static isPublicKey(o: object): boolean;
|
static isPublicKey(o: object): boolean;
|
||||||
equals(publickey: PublicKey): boolean;
|
equals(publickey: PublicKey): boolean;
|
||||||
toBase58(): string;
|
toBase58(): string;
|
||||||
|
@ -16,7 +16,7 @@ declare module '@solana/web3.js' {
|
||||||
|
|
||||||
// === src/account.js ===
|
// === src/account.js ===
|
||||||
export class Account {
|
export class Account {
|
||||||
constructor(secretKey?: Buffer);
|
constructor(secretKey?: Buffer | Uint8Array | Array<number>);
|
||||||
publicKey: PublicKey;
|
publicKey: PublicKey;
|
||||||
secretKey: Buffer;
|
secretKey: Buffer;
|
||||||
}
|
}
|
||||||
|
@ -206,7 +206,9 @@ declare module '@solana/web3.js' {
|
||||||
sendEncodedTransaction(
|
sendEncodedTransaction(
|
||||||
encodedTransaction: string,
|
encodedTransaction: string,
|
||||||
): Promise<TransactionSignature>;
|
): Promise<TransactionSignature>;
|
||||||
sendRawTransaction(wireTransaction: Buffer): Promise<TransactionSignature>;
|
sendRawTransaction(
|
||||||
|
wireTransaction: Buffer | Uint8Array | Array<number>,
|
||||||
|
): Promise<TransactionSignature>;
|
||||||
onAccountChange(
|
onAccountChange(
|
||||||
publickey: PublicKey,
|
publickey: PublicKey,
|
||||||
callback: AccountChangeCallback,
|
callback: AccountChangeCallback,
|
||||||
|
@ -312,7 +314,7 @@ declare module '@solana/web3.js' {
|
||||||
info: Info;
|
info: Info;
|
||||||
|
|
||||||
constructor(key: PublicKey, info: Info);
|
constructor(key: PublicKey, info: Info);
|
||||||
static fromConfigData(buffer: Buffer): ValidatorInfo | null | undefined;
|
static fromConfigData(buffer: Buffer | Uint8Array | Array<number>): ValidatorInfo | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// === src/sysvar.js ===
|
// === src/sysvar.js ===
|
||||||
|
@ -344,7 +346,7 @@ declare module '@solana/web3.js' {
|
||||||
credits: number;
|
credits: number;
|
||||||
lastEpochCredits: number;
|
lastEpochCredits: number;
|
||||||
epochCredits: Array<EpochCredits>;
|
epochCredits: Array<EpochCredits>;
|
||||||
static fromAccountData(buffer: Buffer): VoteAccount;
|
static fromAccountData(buffer: Buffer | Uint8Array | Array<number>): VoteAccount;
|
||||||
}
|
}
|
||||||
|
|
||||||
// === src/instruction.js ===
|
// === src/instruction.js ===
|
||||||
|
@ -396,7 +398,7 @@ declare module '@solana/web3.js' {
|
||||||
recentBlockhash?: Blockhash;
|
recentBlockhash?: Blockhash;
|
||||||
|
|
||||||
constructor(opts?: TransactionCtorFields);
|
constructor(opts?: TransactionCtorFields);
|
||||||
static from(buffer: Buffer): Transaction;
|
static from(buffer: Buffer | Uint8Array | Array<number>): Transaction;
|
||||||
add(
|
add(
|
||||||
...items: Array<
|
...items: Array<
|
||||||
Transaction | TransactionInstruction | TransactionInstructionCtorFields
|
Transaction | TransactionInstruction | TransactionInstructionCtorFields
|
||||||
|
@ -457,7 +459,7 @@ declare module '@solana/web3.js' {
|
||||||
payer: Account,
|
payer: Account,
|
||||||
program: Account,
|
program: Account,
|
||||||
programId: PublicKey,
|
programId: PublicKey,
|
||||||
data: Buffer | Array<number>,
|
data: Buffer | Uint8Array | Array<number>,
|
||||||
): Promise<PublicKey>;
|
): Promise<PublicKey>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -468,7 +470,7 @@ declare module '@solana/web3.js' {
|
||||||
static load(
|
static load(
|
||||||
connection: Connection,
|
connection: Connection,
|
||||||
payer: Account,
|
payer: Account,
|
||||||
elfBytes: Buffer | Array<number>,
|
elfBytes: Buffer | Uint8Array | Array<number>,
|
||||||
): Promise<PublicKey>;
|
): Promise<PublicKey>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,9 @@ import * as BufferLayout from 'buffer-layout';
|
||||||
declare module '@solana/web3.js' {
|
declare module '@solana/web3.js' {
|
||||||
// === src/publickey.js ===
|
// === src/publickey.js ===
|
||||||
declare export class PublicKey {
|
declare export class PublicKey {
|
||||||
constructor(value: number | string | Buffer | Array<number>): PublicKey;
|
constructor(
|
||||||
|
value: number | string | Buffer | Uint8Array | Array<number>,
|
||||||
|
): PublicKey;
|
||||||
static isPublicKey(o: Object): boolean;
|
static isPublicKey(o: Object): boolean;
|
||||||
equals(publickey: PublicKey): boolean;
|
equals(publickey: PublicKey): boolean;
|
||||||
toBase58(): string;
|
toBase58(): string;
|
||||||
|
@ -28,7 +30,7 @@ declare module '@solana/web3.js' {
|
||||||
|
|
||||||
// === src/account.js ===
|
// === src/account.js ===
|
||||||
declare export class Account {
|
declare export class Account {
|
||||||
constructor(secretKey: ?Buffer): Account;
|
constructor(secretKey?: Buffer | Uint8Array | Array<number>): Account;
|
||||||
publicKey: PublicKey;
|
publicKey: PublicKey;
|
||||||
secretKey: Buffer;
|
secretKey: Buffer;
|
||||||
}
|
}
|
||||||
|
@ -219,7 +221,9 @@ declare module '@solana/web3.js' {
|
||||||
sendEncodedTransaction(
|
sendEncodedTransaction(
|
||||||
encodedTransaction: string,
|
encodedTransaction: string,
|
||||||
): Promise<TransactionSignature>;
|
): Promise<TransactionSignature>;
|
||||||
sendRawTransaction(wireTransaction: Buffer): Promise<TransactionSignature>;
|
sendRawTransaction(
|
||||||
|
wireTransaction: Buffer | Uint8Array | Array<number>,
|
||||||
|
): Promise<TransactionSignature>;
|
||||||
onAccountChange(
|
onAccountChange(
|
||||||
publickey: PublicKey,
|
publickey: PublicKey,
|
||||||
callback: AccountChangeCallback,
|
callback: AccountChangeCallback,
|
||||||
|
@ -369,7 +373,7 @@ declare module '@solana/web3.js' {
|
||||||
info: Info;
|
info: Info;
|
||||||
|
|
||||||
constructor(key: PublicKey, info: Info): ValidatorInfo;
|
constructor(key: PublicKey, info: Info): ValidatorInfo;
|
||||||
static fromConfigData(buffer: Buffer): ?ValidatorInfo;
|
static fromConfigData(buffer: Buffer | Uint8Array | Array<number>): ValidatorInfo | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// === src/sysvar.js ===
|
// === src/sysvar.js ===
|
||||||
|
@ -401,7 +405,7 @@ declare module '@solana/web3.js' {
|
||||||
credits: number;
|
credits: number;
|
||||||
lastEpochCredits: number;
|
lastEpochCredits: number;
|
||||||
epochCredits: Array<EpochCredits>;
|
epochCredits: Array<EpochCredits>;
|
||||||
static fromAccountData(buffer: Buffer): VoteAccount;
|
static fromAccountData(buffer: Buffer | Uint8Array | Array<number>): VoteAccount;
|
||||||
}
|
}
|
||||||
|
|
||||||
// === src/instruction.js ===
|
// === src/instruction.js ===
|
||||||
|
@ -451,7 +455,7 @@ declare module '@solana/web3.js' {
|
||||||
recentBlockhash: ?Blockhash;
|
recentBlockhash: ?Blockhash;
|
||||||
|
|
||||||
constructor(opts?: TransactionCtorFields): Transaction;
|
constructor(opts?: TransactionCtorFields): Transaction;
|
||||||
static from(buffer: Buffer): Transaction;
|
static from(buffer: Buffer | Uint8Array | Array<number>): Transaction;
|
||||||
add(
|
add(
|
||||||
...items: Array<
|
...items: Array<
|
||||||
Transaction | TransactionInstruction | TransactionInstructionCtorFields,
|
Transaction | TransactionInstruction | TransactionInstructionCtorFields,
|
||||||
|
@ -471,7 +475,7 @@ declare module '@solana/web3.js' {
|
||||||
payer: Account,
|
payer: Account,
|
||||||
program: Account,
|
program: Account,
|
||||||
programId: PublicKey,
|
programId: PublicKey,
|
||||||
data: Buffer | Array<number>,
|
data: Buffer | Uint8Array | Array<number>,
|
||||||
): Promise<PublicKey>;
|
): Promise<PublicKey>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -482,7 +486,7 @@ declare module '@solana/web3.js' {
|
||||||
static load(
|
static load(
|
||||||
connection: Connection,
|
connection: Connection,
|
||||||
payer: Account,
|
payer: Account,
|
||||||
elfBytes: Buffer | Array<number>,
|
elfBytes: Buffer | Uint8Array | Array<number>,
|
||||||
): Promise<PublicKey>;
|
): Promise<PublicKey>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
import nacl from 'tweetnacl';
|
import nacl from 'tweetnacl';
|
||||||
import type {KeyPair} from 'tweetnacl';
|
import type {KeyPair} from 'tweetnacl';
|
||||||
|
|
||||||
|
import {toBuffer} from './util/to-buffer';
|
||||||
import {PublicKey} from './publickey';
|
import {PublicKey} from './publickey';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -18,9 +19,9 @@ export class Account {
|
||||||
*
|
*
|
||||||
* @param secretKey Secret key for the account
|
* @param secretKey Secret key for the account
|
||||||
*/
|
*/
|
||||||
constructor(secretKey: ?Buffer = null) {
|
constructor(secretKey?: Buffer | Uint8Array | Array<number>) {
|
||||||
if (secretKey) {
|
if (secretKey) {
|
||||||
this._keypair = nacl.sign.keyPair.fromSecretKey(secretKey);
|
this._keypair = nacl.sign.keyPair.fromSecretKey(toBuffer(secretKey));
|
||||||
} else {
|
} else {
|
||||||
this._keypair = nacl.sign.keyPair();
|
this._keypair = nacl.sign.keyPair();
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,7 @@ export class BpfLoader {
|
||||||
static load(
|
static load(
|
||||||
connection: Connection,
|
connection: Connection,
|
||||||
payer: Account,
|
payer: Account,
|
||||||
elf: Buffer | Array<number>,
|
elf: Buffer | Uint8Array | Array<number>,
|
||||||
): Promise<PublicKey> {
|
): Promise<PublicKey> {
|
||||||
const program = new Account();
|
const program = new Account();
|
||||||
return Loader.load(connection, payer, program, BpfLoader.programId, elf);
|
return Loader.load(connection, payer, program, BpfLoader.programId, elf);
|
||||||
|
|
|
@ -13,6 +13,7 @@ import {PublicKey} from './publickey';
|
||||||
import {DEFAULT_TICKS_PER_SLOT, NUM_TICKS_PER_SECOND} from './timing';
|
import {DEFAULT_TICKS_PER_SLOT, NUM_TICKS_PER_SECOND} from './timing';
|
||||||
import {Transaction} from './transaction';
|
import {Transaction} from './transaction';
|
||||||
import {sleep} from './util/sleep';
|
import {sleep} from './util/sleep';
|
||||||
|
import {toBuffer} from './util/to-buffer';
|
||||||
import type {Blockhash} from './blockhash';
|
import type {Blockhash} from './blockhash';
|
||||||
import type {FeeCalculator} from './fee-calculator';
|
import type {FeeCalculator} from './fee-calculator';
|
||||||
import type {Account} from './account';
|
import type {Account} from './account';
|
||||||
|
@ -1281,9 +1282,9 @@ export class Connection {
|
||||||
* wire format
|
* wire format
|
||||||
*/
|
*/
|
||||||
async sendRawTransaction(
|
async sendRawTransaction(
|
||||||
rawTransaction: Buffer,
|
rawTransaction: Buffer | Uint8Array | Array<number>,
|
||||||
): Promise<TransactionSignature> {
|
): Promise<TransactionSignature> {
|
||||||
const encodedTransaction = bs58.encode(rawTransaction);
|
const encodedTransaction = bs58.encode(toBuffer(rawTransaction));
|
||||||
const result = await this.sendEncodedTransaction(encodedTransaction);
|
const result = await this.sendEncodedTransaction(encodedTransaction);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,7 +52,7 @@ export class Loader {
|
||||||
payer: Account,
|
payer: Account,
|
||||||
program: Account,
|
program: Account,
|
||||||
programId: PublicKey,
|
programId: PublicKey,
|
||||||
data: Buffer | Array<number>,
|
data: Buffer | Uint8Array | Array<number>,
|
||||||
): Promise<PublicKey> {
|
): Promise<PublicKey> {
|
||||||
{
|
{
|
||||||
const balanceNeeded = await connection.getMinimumBalanceForRentExemption(
|
const balanceNeeded = await connection.getMinimumBalanceForRentExemption(
|
||||||
|
|
|
@ -13,7 +13,7 @@ export class PublicKey {
|
||||||
/**
|
/**
|
||||||
* Create a new PublicKey object
|
* Create a new PublicKey object
|
||||||
*/
|
*/
|
||||||
constructor(value: number | string | Buffer | Array<number>) {
|
constructor(value: number | string | Buffer | Uint8Array | Array<number>) {
|
||||||
if (typeof value === 'string') {
|
if (typeof value === 'string') {
|
||||||
// hexadecimal number
|
// hexadecimal number
|
||||||
if (value.startsWith('0x')) {
|
if (value.startsWith('0x')) {
|
||||||
|
|
|
@ -480,7 +480,7 @@ export class Transaction {
|
||||||
/**
|
/**
|
||||||
* Parse a wire transaction into a Transaction object.
|
* Parse a wire transaction into a Transaction object.
|
||||||
*/
|
*/
|
||||||
static from(buffer: Buffer): Transaction {
|
static from(buffer: Buffer | Uint8Array | Array<number>): Transaction {
|
||||||
// Slice up wire data
|
// Slice up wire data
|
||||||
let byteArray = [...buffer];
|
let byteArray = [...buffer];
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
// @flow
|
||||||
|
|
||||||
|
export const toBuffer = (arr: Buffer | Uint8Array | Array<number>): Buffer => {
|
||||||
|
if (arr instanceof Buffer) {
|
||||||
|
return arr;
|
||||||
|
} else if (arr instanceof Uint8Array) {
|
||||||
|
return Buffer.from(arr.buffer, arr.byteOffset, arr.byteLength);
|
||||||
|
} else {
|
||||||
|
return Buffer.from(arr);
|
||||||
|
}
|
||||||
|
};
|
|
@ -72,7 +72,7 @@ export class ValidatorInfo {
|
||||||
* @param buffer config account data
|
* @param buffer config account data
|
||||||
* @return null if info was not found
|
* @return null if info was not found
|
||||||
*/
|
*/
|
||||||
static fromConfigData(buffer: Buffer): ?ValidatorInfo {
|
static fromConfigData(buffer: Buffer | Uint8Array | Array<number>): ValidatorInfo | null {
|
||||||
const PUBKEY_LENGTH = 32;
|
const PUBKEY_LENGTH = 32;
|
||||||
|
|
||||||
let byteArray = [...buffer];
|
let byteArray = [...buffer];
|
||||||
|
|
|
@ -3,6 +3,7 @@ import * as BufferLayout from 'buffer-layout';
|
||||||
|
|
||||||
import * as Layout from './layout';
|
import * as Layout from './layout';
|
||||||
import {PublicKey} from './publickey';
|
import {PublicKey} from './publickey';
|
||||||
|
import {toBuffer} from './util/to-buffer';
|
||||||
|
|
||||||
export const VOTE_PROGRAM_ID = new PublicKey(
|
export const VOTE_PROGRAM_ID = new PublicKey(
|
||||||
'Vote111111111111111111111111111111111111111',
|
'Vote111111111111111111111111111111111111111',
|
||||||
|
@ -79,8 +80,8 @@ export class VoteAccount {
|
||||||
* @param buffer account data
|
* @param buffer account data
|
||||||
* @return VoteAccount
|
* @return VoteAccount
|
||||||
*/
|
*/
|
||||||
static fromAccountData(buffer: Buffer): VoteAccount {
|
static fromAccountData(buffer: Buffer | Uint8Array | Array<number>): VoteAccount {
|
||||||
const va = VoteAccountLayout.decode(buffer, 0);
|
const va = VoteAccountLayout.decode(toBuffer(buffer), 0);
|
||||||
va.nodePubkey = new PublicKey(va.nodePubkey);
|
va.nodePubkey = new PublicKey(va.nodePubkey);
|
||||||
va.authorizedVoterPubkey = new PublicKey(va.authorizedVoterPubkey);
|
va.authorizedVoterPubkey = new PublicKey(va.authorizedVoterPubkey);
|
||||||
va.authorizedWithdrawerPubkey = new PublicKey(
|
va.authorizedWithdrawerPubkey = new PublicKey(
|
||||||
|
|
|
@ -200,7 +200,8 @@ test('serialize unsigned transaction', () => {
|
||||||
); // Arbitrary known public key
|
); // Arbitrary known public key
|
||||||
const transfer = SystemProgram.transfer(sender.publicKey, recipient, 49);
|
const transfer = SystemProgram.transfer(sender.publicKey, recipient, 49);
|
||||||
const expectedTransaction = new Transaction({recentBlockhash}).add(transfer);
|
const expectedTransaction = new Transaction({recentBlockhash}).add(transfer);
|
||||||
const wireTransaction = Buffer.from([
|
|
||||||
|
const wireTransactionArray = [
|
||||||
1,
|
1,
|
||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
|
@ -416,9 +417,11 @@ test('serialize unsigned transaction', () => {
|
||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
]);
|
];
|
||||||
expect(wireTransaction).toEqual(expectedTransaction.serialize());
|
|
||||||
|
|
||||||
const tx = Transaction.from(wireTransaction);
|
const wireTransaction = Buffer.from(wireTransactionArray);
|
||||||
expect(tx).toEqual(expectedTransaction);
|
expect(wireTransaction).toEqual(expectedTransaction.serialize());
|
||||||
|
expect(Transaction.from(wireTransaction)).toEqual(expectedTransaction);
|
||||||
|
expect(Transaction.from(wireTransactionArray)).toEqual(expectedTransaction);
|
||||||
|
expect(Transaction.from(Uint8Array.from(wireTransactionArray))).toEqual(expectedTransaction);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue