diff --git a/web3.js/module.flow.js b/web3.js/module.flow.js index 8c430d2ea..e71bda5e8 100644 --- a/web3.js/module.flow.js +++ b/web3.js/module.flow.js @@ -19,7 +19,6 @@ declare module '@solana/web3.js' { constructor( value: number | string | Buffer | Uint8Array | Array, ): PublicKey; - static isPublicKey(o: {}): boolean; static createWithSeed( fromPublicKey: PublicKey, seed: string, diff --git a/web3.js/src/publickey.js b/web3.js/src/publickey.js index 51c1d716c..bd1d0fda2 100644 --- a/web3.js/src/publickey.js +++ b/web3.js/src/publickey.js @@ -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 */ diff --git a/web3.js/src/transaction.js b/web3.js/src/transaction.js index 7f0802f8b..1219503f5 100644 --- a/web3.js/src/transaction.js +++ b/web3.js/src/transaction.js @@ -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 = 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); }); } diff --git a/web3.js/test/account.test.js b/web3.js/test/account.test.js index d54bbdd89..89c37f6ba 100644 --- a/web3.js/test/account.test.js +++ b/web3.js/test/account.test.js @@ -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); }); diff --git a/web3.js/test/publickey.test.js b/web3.js/test/publickey.test.js index 7cc87bdda..bfa638adc 100644 --- a/web3.js/test/publickey.test.js +++ b/web3.js/test/publickey.test.js @@ -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',