fix: allow Uint8Array and Array<number> where Buffer is accepted
This commit is contained in:
committed by
Michael Vines
parent
6ba2f1d524
commit
6a7115b8bd
@@ -2,6 +2,7 @@
|
||||
import nacl from 'tweetnacl';
|
||||
import type {KeyPair} from 'tweetnacl';
|
||||
|
||||
import {toBuffer} from './util/to-buffer';
|
||||
import {PublicKey} from './publickey';
|
||||
|
||||
/**
|
||||
@@ -18,9 +19,9 @@ export class Account {
|
||||
*
|
||||
* @param secretKey Secret key for the account
|
||||
*/
|
||||
constructor(secretKey: ?Buffer = null) {
|
||||
constructor(secretKey?: Buffer | Uint8Array | Array<number>) {
|
||||
if (secretKey) {
|
||||
this._keypair = nacl.sign.keyPair.fromSecretKey(secretKey);
|
||||
this._keypair = nacl.sign.keyPair.fromSecretKey(toBuffer(secretKey));
|
||||
} else {
|
||||
this._keypair = nacl.sign.keyPair();
|
||||
}
|
||||
|
@@ -36,7 +36,7 @@ export class BpfLoader {
|
||||
static load(
|
||||
connection: Connection,
|
||||
payer: Account,
|
||||
elf: Buffer | Array<number>,
|
||||
elf: Buffer | Uint8Array | Array<number>,
|
||||
): Promise<PublicKey> {
|
||||
const program = new Account();
|
||||
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 {Transaction} from './transaction';
|
||||
import {sleep} from './util/sleep';
|
||||
import {toBuffer} from './util/to-buffer';
|
||||
import type {Blockhash} from './blockhash';
|
||||
import type {FeeCalculator} from './fee-calculator';
|
||||
import type {Account} from './account';
|
||||
@@ -1281,9 +1282,9 @@ export class Connection {
|
||||
* wire format
|
||||
*/
|
||||
async sendRawTransaction(
|
||||
rawTransaction: Buffer,
|
||||
rawTransaction: Buffer | Uint8Array | Array<number>,
|
||||
): Promise<TransactionSignature> {
|
||||
const encodedTransaction = bs58.encode(rawTransaction);
|
||||
const encodedTransaction = bs58.encode(toBuffer(rawTransaction));
|
||||
const result = await this.sendEncodedTransaction(encodedTransaction);
|
||||
return result;
|
||||
}
|
||||
|
@@ -52,7 +52,7 @@ export class Loader {
|
||||
payer: Account,
|
||||
program: Account,
|
||||
programId: PublicKey,
|
||||
data: Buffer | Array<number>,
|
||||
data: Buffer | Uint8Array | Array<number>,
|
||||
): Promise<PublicKey> {
|
||||
{
|
||||
const balanceNeeded = await connection.getMinimumBalanceForRentExemption(
|
||||
|
@@ -13,7 +13,7 @@ export class PublicKey {
|
||||
/**
|
||||
* Create a new PublicKey object
|
||||
*/
|
||||
constructor(value: number | string | Buffer | Array<number>) {
|
||||
constructor(value: number | string | Buffer | Uint8Array | Array<number>) {
|
||||
if (typeof value === 'string') {
|
||||
// hexadecimal number
|
||||
if (value.startsWith('0x')) {
|
||||
|
@@ -480,7 +480,7 @@ export class Transaction {
|
||||
/**
|
||||
* 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
|
||||
let byteArray = [...buffer];
|
||||
|
||||
|
11
web3.js/src/util/to-buffer.js
Normal file
11
web3.js/src/util/to-buffer.js
Normal file
@@ -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
|
||||
* @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;
|
||||
|
||||
let byteArray = [...buffer];
|
||||
|
@@ -3,6 +3,7 @@ import * as BufferLayout from 'buffer-layout';
|
||||
|
||||
import * as Layout from './layout';
|
||||
import {PublicKey} from './publickey';
|
||||
import {toBuffer} from './util/to-buffer';
|
||||
|
||||
export const VOTE_PROGRAM_ID = new PublicKey(
|
||||
'Vote111111111111111111111111111111111111111',
|
||||
@@ -79,8 +80,8 @@ export class VoteAccount {
|
||||
* @param buffer account data
|
||||
* @return VoteAccount
|
||||
*/
|
||||
static fromAccountData(buffer: Buffer): VoteAccount {
|
||||
const va = VoteAccountLayout.decode(buffer, 0);
|
||||
static fromAccountData(buffer: Buffer | Uint8Array | Array<number>): VoteAccount {
|
||||
const va = VoteAccountLayout.decode(toBuffer(buffer), 0);
|
||||
va.nodePubkey = new PublicKey(va.nodePubkey);
|
||||
va.authorizedVoterPubkey = new PublicKey(va.authorizedVoterPubkey);
|
||||
va.authorizedWithdrawerPubkey = new PublicKey(
|
||||
|
Reference in New Issue
Block a user