diff --git a/modules/bitgo/test/v2/unit/wallet.ts b/modules/bitgo/test/v2/unit/wallet.ts index 761fb25056..efccead059 100644 --- a/modules/bitgo/test/v2/unit/wallet.ts +++ b/modules/bitgo/test/v2/unit/wallet.ts @@ -21,6 +21,7 @@ import { ManageUnspentsOptions, MessageStandardType, MessageTypes, + NeedUserSignupError, PopulatedIntent, PrebuildTransactionOptions, PrebuildTransactionWithIntentOptions, @@ -1994,6 +1995,48 @@ describe('V2 Wallet:', function () { createShareNock.isDone().should.be.True(); }); + it('should throw NeedUserSignupError when recipient pubkey is missing and spend permission is requested', async function () { + const userId = '456'; + const email = 'newuser@sdktest.com'; + const permissions = 'view,spend'; + const walletPassphrase = 'bitgo1234'; + + // Simulate a user who has not yet logged in / set up their ECDH key + const getSharingKeyNock = nock(bgUrl) + .post('/api/v1/user/sharingkey', { email }) + .reply(200, { userId }); + + await wallet + .shareWallet({ email, permissions, walletPassphrase }) + .should.be.rejectedWith(NeedUserSignupError); + + getSharingKeyNock.isDone().should.be.True(); + }); + + it('should not throw NeedUserSignupError when recipient pubkey is missing but spend permission is not requested', async function () { + const userId = '456'; + const email = 'newuser@sdktest.com'; + const permissions = 'view'; + + // Sharing key without pubkey is fine for view-only shares + const getSharingKeyNock = nock(bgUrl) + .post('/api/v1/user/sharingkey', { email }) + .reply(200, { userId }); + + const createShareNock = nock(bgUrl) + .post(`/api/v2/tbtc/wallet/${wallet.id()}/share`, { + user: userId, + permissions, + skipKeychain: true, + }) + .reply(200, {}); + + await wallet.shareWallet({ email, permissions }); + + getSharingKeyNock.isDone().should.be.True(); + createShareNock.isDone().should.be.True(); + }); + describe('Hot Wallet Sharing', function () { const userId = '123'; const email = 'shareto@sdktest.com'; diff --git a/modules/sdk-core/src/bitgo/wallet/wallet.ts b/modules/sdk-core/src/bitgo/wallet/wallet.ts index a8c1952cf5..59bc1b39ab 100644 --- a/modules/sdk-core/src/bitgo/wallet/wallet.ts +++ b/modules/sdk-core/src/bitgo/wallet/wallet.ts @@ -2064,6 +2064,9 @@ export class Wallet implements IWallet { })) as any; let sharedKeychain; if (needsKeychain) { + if (!sharing.pubkey) { + throw new NeedUserSignupError(sharing.userId); + } sharedKeychain = await this.prepareSharedKeychain( params.walletPassphrase, sharing.pubkey,