-
Notifications
You must be signed in to change notification settings - Fork 300
feat(express): migrate shareWallet to typed routes #7146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
danielzhao122
wants to merge
3
commits into
master
Choose a base branch
from
WP-5419-express-migrate-api-v2-coin-wallet-id-share-to-typed-routes
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import * as t from 'io-ts'; | ||
import { httpRoute, httpRequest, optional } from '@api-ts/io-ts-http'; | ||
import { BitgoExpressError } from '../../schemas/error'; | ||
import { ShareState, ShareWalletKeychain } from '../../schemas/wallet'; | ||
|
||
/** | ||
* Path parameters for sharing a wallet | ||
*/ | ||
export const ShareWalletParams = { | ||
/** Coin ticker / chain identifier */ | ||
coin: t.string, | ||
/** Wallet ID */ | ||
id: t.string, | ||
} as const; | ||
|
||
/** | ||
* Request body for sharing a wallet | ||
*/ | ||
export const ShareWalletBody = { | ||
/** Recipient email address (required) */ | ||
email: t.string, | ||
/** Permissions string, e.g., "view,spend" (required) */ | ||
permissions: t.string, | ||
/** Wallet passphrase used to derive shared key when needed */ | ||
walletPassphrase: optional(t.string), | ||
/** Optional message to include with the share */ | ||
message: optional(t.string), | ||
/** If true, allows sharing without a keychain */ | ||
reshare: optional(t.boolean), | ||
/** If true, skips sharing the wallet keychain with the recipient */ | ||
skipKeychain: optional(t.boolean), | ||
/** If true, suppresses email notification to the recipient */ | ||
disableEmail: optional(t.boolean), | ||
} as const; | ||
|
||
/** | ||
* Response for sharing a wallet | ||
*/ | ||
export const ShareWalletResponse200 = t.intersection([ | ||
t.type({ | ||
/** Wallet share id */ | ||
id: t.string, | ||
/** Coin of the wallet */ | ||
coin: t.string, | ||
/** Wallet id */ | ||
wallet: t.string, | ||
/** Id of the sharer */ | ||
fromUser: t.string, | ||
/** Id of the recipient */ | ||
toUser: t.string, | ||
/** Comma-separated list of privileges for wallet */ | ||
permissions: t.string, | ||
}), | ||
t.partial({ | ||
/** Wallet label */ | ||
walletLabel: t.string, | ||
/** User-readable message */ | ||
message: t.string, | ||
/** Share state */ | ||
state: ShareState, | ||
/** Enterprise id, if applicable */ | ||
enterprise: t.string, | ||
/** Pending approval id, if one was generated */ | ||
pendingApprovalId: t.string, | ||
/** Included if shared with spend permission */ | ||
keychain: ShareWalletKeychain, | ||
}), | ||
]); | ||
|
||
export const ShareWalletResponse = { | ||
200: ShareWalletResponse200, | ||
400: BitgoExpressError, | ||
} as const; | ||
|
||
/** | ||
* Share this wallet with another BitGo user. | ||
* | ||
* @operationId express.v2.wallet.share | ||
*/ | ||
export const PostShareWallet = httpRoute({ | ||
path: '/api/v2/{coin}/wallet/{id}/share', | ||
method: 'POST', | ||
request: httpRequest({ | ||
params: ShareWalletParams, | ||
body: ShareWalletBody, | ||
}), | ||
response: ShareWalletResponse, | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import * as t from 'io-ts'; | ||
|
||
export const ShareState = t.union([ | ||
t.literal('pendingapproval'), | ||
t.literal('active'), | ||
t.literal('accepted'), | ||
t.literal('canceled'), | ||
t.literal('rejected'), | ||
]); | ||
|
||
export const ShareWalletKeychain = t.partial({ | ||
pub: t.string, | ||
encryptedPrv: t.string, | ||
fromPubKey: t.string, | ||
toPubKey: t.string, | ||
path: t.string, | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import * as sinon from 'sinon'; | ||
import 'should-http'; | ||
import 'should-sinon'; | ||
import '../../lib/asserts'; | ||
import nock from 'nock'; | ||
import { TestBitGo, TestBitGoAPI } from '@bitgo/sdk-test'; | ||
import { BitGo } from 'bitgo'; | ||
import { BaseCoin, Wallets, Wallet, decodeOrElse, common } from '@bitgo/sdk-core'; | ||
import { ExpressApiRouteRequest } from '../../../src/typedRoutes/api'; | ||
import { handleV2ShareWallet } from '../../../src/clientRoutes'; | ||
import { ShareWalletResponse } from '../../../src/typedRoutes/api/v2/shareWallet'; | ||
|
||
describe('Share Wallet (typed handler)', () => { | ||
let bitgo: TestBitGoAPI; | ||
|
||
before(async function () { | ||
if (!nock.isActive()) { | ||
nock.activate(); | ||
} | ||
bitgo = TestBitGo.decorate(BitGo, { env: 'test' }); | ||
bitgo.initializeTestVars(); | ||
nock.disableNetConnect(); | ||
nock.enableNetConnect('127.0.0.1'); | ||
}); | ||
|
||
after(() => { | ||
if (nock.isActive()) { | ||
nock.restore(); | ||
} | ||
}); | ||
|
||
it('should call shareWallet (no stub), mock BitGo HTTP, and return typed response', async () => { | ||
const coin = 'tbtc'; | ||
const walletId = '59cd72485007a239fb00282ed480da1f'; | ||
const email = '[email protected]'; | ||
const permissions = 'view'; | ||
const message = 'hello'; | ||
|
||
const baseCoin = bitgo.coin(coin); | ||
const walletData = { | ||
id: walletId, | ||
coin: coin, | ||
keys: ['k1', 'k2', 'k3'], | ||
coinSpecific: {}, | ||
multisigType: 'onchain', | ||
type: 'hot', | ||
}; | ||
const realWallet = new Wallet(bitgo, baseCoin, walletData); | ||
const coinStub = sinon.createStubInstance(BaseCoin, { | ||
wallets: sinon.stub<[], Wallets>().returns({ | ||
get: sinon.stub<[any], Promise<Wallet>>().resolves(realWallet), | ||
} as any), | ||
}); | ||
|
||
const stubBitgo = sinon.createStubInstance(BitGo, { coin: sinon.stub<[string]>().returns(coinStub) }); | ||
|
||
const bgUrl = common.Environments[bitgo.getEnv()].uri; | ||
const getSharingKeyNock = nock(bgUrl).post('/api/v1/user/sharingkey', { email }).reply(200, { userId: 'u1' }); | ||
const shareResponse = { | ||
id: walletId, | ||
coin, | ||
wallet: walletId, | ||
fromUser: 'u0', | ||
toUser: 'u1', | ||
permissions, | ||
message, | ||
state: 'active', | ||
}; | ||
const createShareNock = nock(bgUrl) | ||
.post(`/api/v2/${coin}/wallet/${walletId}/share`, (body) => { | ||
body.user.should.equal('u1'); | ||
body.permissions.should.equal(permissions); | ||
body.skipKeychain.should.equal(true); | ||
if (message) body.message.should.equal(message); | ||
return true; | ||
}) | ||
.reply(200, shareResponse); | ||
|
||
const req = { | ||
bitgo: stubBitgo, | ||
decoded: { | ||
coin, | ||
id: walletId, | ||
email, | ||
permissions, | ||
message, | ||
}, | ||
} as unknown as ExpressApiRouteRequest<'express.v2.wallet.share', 'post'>; | ||
|
||
const res = await handleV2ShareWallet(req); | ||
decodeOrElse('ShareWalletResponse200', ShareWalletResponse[200], res, (errors) => { | ||
throw new Error(`Response did not match expected codec: ${errors}`); | ||
}); | ||
|
||
getSharingKeyNock.isDone().should.be.true(); | ||
createShareNock.isDone().should.be.true(); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The codec already tells us this is required.