fix: remove instanceof checks

This commit is contained in:
Justin Starry 2020-07-31 01:48:25 +08:00 committed by Michael Vines
parent 31ea69f278
commit d0f4b24481
5 changed files with 29 additions and 39 deletions

View File

@ -19,7 +19,6 @@ declare module '@solana/web3.js' {
constructor(
value: number | string | Buffer | Uint8Array | Array<number>,
): PublicKey;
static isPublicKey(o: {}): boolean;
static createWithSeed(
fromPublicKey: PublicKey,
seed: string,

View File

@ -35,13 +35,6 @@ export class PublicKey {
}
}
/**
* Checks if the provided object is a PublicKey
*/
static isPublicKey(o: Object): boolean {
return o instanceof PublicKey;
}
/**
* Checks if two publicKeys are equal
*/

View File

@ -177,10 +177,10 @@ export class Transaction {
throw new Error('No instructions');
}
items.forEach(item => {
if (item instanceof Transaction) {
items.forEach((item: any) => {
if ('instructions' in item) {
this.instructions = this.instructions.concat(item.instructions);
} else if (item instanceof TransactionInstruction) {
} else if ('data' in item && 'programId' in item && 'keys' in item) {
this.instructions.push(item);
} else {
this.instructions.push(new TransactionInstruction(item));
@ -366,31 +366,39 @@ export class Transaction {
if (partialSigners.length === 0) {
throw new Error('No signers');
}
function partialSignerPublicKey(accountOrPublicKey: any): PublicKey {
if ('publicKey' in accountOrPublicKey) {
return accountOrPublicKey.publicKey;
}
return accountOrPublicKey;
}
function signerAccount(accountOrPublicKey: any): ?Account {
if (
'publicKey' in accountOrPublicKey &&
'secretKey' in accountOrPublicKey
) {
return accountOrPublicKey;
}
}
const signatures: Array<SignaturePubkeyPair> = partialSigners.map(
accountOrPublicKey => {
const publicKey =
accountOrPublicKey instanceof Account
? accountOrPublicKey.publicKey
: accountOrPublicKey;
return {
signature: null,
publicKey,
};
},
accountOrPublicKey => ({
signature: null,
publicKey: partialSignerPublicKey(accountOrPublicKey),
}),
);
this.signatures = signatures;
const signData = this.serializeMessage();
partialSigners.forEach((accountOrPublicKey, index) => {
if (accountOrPublicKey instanceof PublicKey) {
return;
const account = signerAccount(accountOrPublicKey);
if (account) {
const signature = nacl.sign.detached(signData, account.secretKey);
invariant(signature.length === 64);
signatures[index].signature = Buffer.from(signature);
}
const signature = nacl.sign.detached(
signData,
accountOrPublicKey.secretKey,
);
invariant(signature.length === 64);
signatures[index].signature = Buffer.from(signature);
});
}

View File

@ -1,10 +1,8 @@
// @flow
import {Account} from '../src/account';
import {PublicKey} from '../src/publickey';
test('generate new account', () => {
const account = new Account();
expect(PublicKey.isPublicKey(account.publicKey)).toBeTruthy();
expect(account.secretKey).toHaveLength(64);
});

View File

@ -103,14 +103,6 @@ test('equals', () => {
expect(arrayKey.equals(base56Key)).toBe(true);
});
test('isPublicKey', () => {
const key = new PublicKey(
'0x100000000000000000000000000000000000000000000000000000000000000',
);
expect(PublicKey.isPublicKey(key)).toBe(true);
expect(PublicKey.isPublicKey({})).toBe(false);
});
test('toBase58', () => {
const key = new PublicKey(
'0x300000000000000000000000000000000000000000000000000000000000000',