Skip to content

Commit

Permalink
v3 work
Browse files Browse the repository at this point in the history
  • Loading branch information
BitHighlander committed Oct 24, 2024
1 parent ac2207d commit 8396757
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 32 deletions.
2 changes: 1 addition & 1 deletion packages/keepkey-desktop-app/.env
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ REACT_APP_ETHEREUM_INFURA_URL2=https://mainnet.infura.io/v3/527a7b1368e9464fbdc9
REACT_APP_ETHEREUM_INFURA_URL3=https://avalanche-mainnet.infura.io/v3/fb05c87983c4431baafd4600fd33de7e
REACT_APP_MIDGARD_URL=https://indexer.thorchain.shapeshift.com/v2
REACT_APP_WALLET_CONNECT_PROJECT_ID=14d36ca1bc76a70273d44d384e8475ae
REACT_APP_SHAPESHIFT_DAPP_URL=https://private.shapeshift.com
REACT_APP_SHAPESHIFT_DAPP_URL=https://keepkey.info
8 changes: 4 additions & 4 deletions packages/keepkey-desktop-app/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ export const App = () => {
let defaultDapp = localStorage.getItem('@app/defaultDapp')
if (!defaultDapp || defaultDapp === '' || defaultDapp.indexOf('shapeshift') > -1) {
const defaultDappShapeShift = {
imageUrl: 'https://assets.coincap.io/assets/icons/fox@2x.png',
url: 'https://private.shapeshift.com/',
name: 'ShapeShift',
imageUrl: 'https://pioneers.dev/coins/keepkey.png',
url: 'https://app.keepkey.info',
name: 'KeepKey',
}

//set SS as default dapp
//set as default dapp
localStorage.setItem('@app/defaultDapp', JSON.stringify(defaultDappShapeShift))
openDapp(defaultDappShapeShift.url)
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/keepkey-desktop-app/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const validators = {
REACT_APP_KEEPKEY_UPDATER_BASE_URL: url(),
REACT_APP_ETHERSCAN_API_KEY: str(),
REACT_APP_WALLET_CONNECT_PROJECT_ID: str(),
REACT_APP_SHAPESHIFT_DAPP_URL: str({ default: 'https://private.shapeshift.com' }),
REACT_APP_SHAPESHIFT_DAPP_URL: str({ default: 'https://keepkey.info' }),
}

function reporter<T>({ errors }: envalid.ReporterOptions<T>) {
Expand Down
2 changes: 1 addition & 1 deletion packages/keepkey-desktop-app/src/pages/Browser/Browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ const checkIfSSDApp = (currentUrl: string) => {
}

export const Browser = () => {
const [url, setUrl] = useState('https://private.shapeshift.com/')
const [url, setUrl] = useState('https://keepkey.info/')
const [urlWc, setUrlWc] = useState('https://wallet-connect-dapp-ochre.vercel.app')
const [inputUrl, setInputUrl] = useState(url)
const [loading, setLoading] = useState(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export const DappRegistryGrid: FC = () => {
(app: RegistryItem) => {
console.log(TAG,'openDapp app: ', app)
history.push('/browser')
dispatch({ type: WalletActions.SET_BROWSER_URL, payload: app.homepage })
setTimeout(() => {
dispatch({ type: WalletActions.SET_BROWSER_URL, payload: app.homepage })
}, 2000)
Expand Down
63 changes: 50 additions & 13 deletions packages/keepkey-desktop/src/tcpBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { logger } from './helpers/middlewares/logger'
import { rendererIpc } from './ipcListeners'
import { createAndUpdateTray } from './tray'

// Function to start TCP bridge
export const startTcpBridge = async (port?: number) => {
if (tcpBridgeRunning || tcpBridgeStarting) return
setTcpBridgeStarting(true)
Expand All @@ -50,29 +51,62 @@ export const startTcpBridge = async (port?: number) => {
appExpress.use('/spec', express.static(path.join(__dirname, 'api')))

addMiddleware(logger)
setSdkPairingHandler(async (info: PairingInfo) => {
const apiKey = uuid.v4()
console.log('approving pairing request', info, apiKey)
// await promptUser(){}

// Set up pairing handler
setSdkPairingHandler(async (info: PairingInfo, req: express.Request) => {
const apiKey = uuid.v4(); // Generate a new API key
console.log('req: ',req)
// Ensure the request object is available
if (!req || !req.headers) {
console.error('Request object or headers are missing');
throw new Error('Request object or headers are missing');
}

// Get the request origin or referer to check where the request is coming from
const origin = req.headers.origin || req.headers.referer;

// Check if the origin or referer is "https://keepkey.info"
if (origin === 'https://app.keepkey.info') {
console.log('Auto-approving pairing request from keepkey.info', info, apiKey);

// Automatically approve the pairing and save to the database
info.addedOn = Date.now();
await db.insertOne<{ type: 'sdk-pairing'; apiKey: string; info: PairingInfo }>({
type: 'sdk-pairing',
apiKey,
info,
});

// Return the generated API key without user prompt
return apiKey;
}

// If the request is not from "https://keepkey.info", proceed with the normal pairing flow
console.log('Prompting user for pairing approval', info, apiKey);
let input = {
type: 'native',
data: info,
} satisfies PairingProps
let result = await (await rendererIpc).modalPair(input)
console.log('PAIR RESULT: ', result)
} satisfies PairingProps;

// Show the modal to prompt the user
let result = await (await rendererIpc).modalPair(input);
console.log('PAIR RESULT: ', result);

if (result) {
console.log('USER APPROVED!')
info.addedOn = Date.now()
console.log('USER APPROVED!');
info.addedOn = Date.now();
await db.insertOne<{ type: 'sdk-pairing'; apiKey: string; info: PairingInfo }>({
type: 'sdk-pairing',
apiKey,
info,
})
return apiKey
});
return apiKey;
} else {
return 'rejected'
return 'rejected';
}
})
});

// Set up client factory
setSdkClientFactory(async (apiKey: string) => {
const doc = await db.findOne<{ type: 'sdk-pairing'; apiKey: string; info: PairingInfo }>({
type: 'sdk-pairing',
Expand All @@ -90,6 +124,8 @@ export const startTcpBridge = async (port?: number) => {
logger: console.log.bind(console),
}
})

// Register routes
RegisterRoutes(appExpress)

await new Promise(resolve => setServer(appExpress.listen(API_PORT, () => resolve(true))))
Expand All @@ -100,6 +136,7 @@ export const startTcpBridge = async (port?: number) => {
createAndUpdateTray()
}

// Function to stop TCP bridge
export const stopTcpBridge = async () => {
if (tcpBridgeClosing) return false

Expand Down
11 changes: 6 additions & 5 deletions packages/keepkey-sdk-server/src/auth/sdkClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ export const [getSdkClientFactory, setSdkClientFactory] = (() => {
return [promise, resolver!]
})()

export type SdkPairingHandler = (info: PairingInfo) => Promise<string | undefined>
export type SdkPairingHandler = (info: PairingInfo, req: any) => Promise<string | undefined>;


export const [getSdkPairingHandler, setSdkPairingHandler] = (() => {
let resolver: (_: SdkPairingHandler) => void
const promise = new Promise<SdkPairingHandler>(resolve => (resolver = resolve))
return [promise, resolver!]
})()
let resolver: (_: SdkPairingHandler) => void;
const promise = new Promise<SdkPairingHandler>(resolve => (resolver = resolve));
return [promise, resolver!];
})();
18 changes: 11 additions & 7 deletions packages/keepkey-sdk-server/src/controllers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,22 @@ export class AuthController extends Controller {
@OperationId('Pair')
@Response(403, 'Pairing request rejected')
public async pair(
@Body()
body: PairingInfo,
@Body() body: PairingInfo,
@Request() req: any, // Use @Request() instead of @Req()
): Promise<{ apiKey: string }> {
console.log('pair body', body)
const apiKey = await (await getSdkPairingHandler)(body)
console.log('pair body', body);
console.log('pair req', req);

// Pass both body (PairingInfo) and req to the pairing handler
const apiKey = await (await getSdkPairingHandler)(body, req);

if (!apiKey) {
this.setStatus(403)
throw {}
this.setStatus(403);
throw new Error('Pairing request rejected');
}

return {
apiKey,
}
};
}
}

0 comments on commit 8396757

Please sign in to comment.