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( constructor(
value: number | string | Buffer | Uint8Array | Array<number>, value: number | string | Buffer | Uint8Array | Array<number>,
): PublicKey; ): PublicKey;
static isPublicKey(o: {}): boolean;
static createWithSeed( static createWithSeed(
fromPublicKey: PublicKey, fromPublicKey: PublicKey,
seed: string, 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 * Checks if two publicKeys are equal
*/ */

View File

@ -177,10 +177,10 @@ export class Transaction {
throw new Error('No instructions'); throw new Error('No instructions');
} }
items.forEach(item => { items.forEach((item: any) => {
if (item instanceof Transaction) { if ('instructions' in item) {
this.instructions = this.instructions.concat(item.instructions); 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); this.instructions.push(item);
} else { } else {
this.instructions.push(new TransactionInstruction(item)); this.instructions.push(new TransactionInstruction(item));
@ -366,31 +366,39 @@ export class Transaction {
if (partialSigners.length === 0) { if (partialSigners.length === 0) {
throw new Error('No signers'); 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( const signatures: Array<SignaturePubkeyPair> = partialSigners.map(
accountOrPublicKey => { accountOrPublicKey => ({
const publicKey = signature: null,
accountOrPublicKey instanceof Account publicKey: partialSignerPublicKey(accountOrPublicKey),
? accountOrPublicKey.publicKey }),
: accountOrPublicKey;
return {
signature: null,
publicKey,
};
},
); );
this.signatures = signatures; this.signatures = signatures;
const signData = this.serializeMessage(); const signData = this.serializeMessage();
partialSigners.forEach((accountOrPublicKey, index) => { partialSigners.forEach((accountOrPublicKey, index) => {
if (accountOrPublicKey instanceof PublicKey) { const account = signerAccount(accountOrPublicKey);
return; 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 // @flow
import {Account} from '../src/account'; import {Account} from '../src/account';
import {PublicKey} from '../src/publickey';
test('generate new account', () => { test('generate new account', () => {
const account = new Account(); const account = new Account();
expect(PublicKey.isPublicKey(account.publicKey)).toBeTruthy();
expect(account.secretKey).toHaveLength(64); expect(account.secretKey).toHaveLength(64);
}); });

View File

@ -103,14 +103,6 @@ test('equals', () => {
expect(arrayKey.equals(base56Key)).toBe(true); 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', () => { test('toBase58', () => {
const key = new PublicKey( const key = new PublicKey(
'0x300000000000000000000000000000000000000000000000000000000000000', '0x300000000000000000000000000000000000000000000000000000000000000',