Skip to content

Keystone<>Core Mobile integration #3021

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
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
54ac7d5
feat: Keystone integration
Charon-Fan Jul 7, 2025
0b01a84
fix: update modal options to use formSheetScreensOptions for Keystone…
Charon-Fan Jul 7, 2025
df9bd4d
fix: simplify QRCode rendering in QRRenderer component
Charon-Fan Jul 7, 2025
a816362
feat: add light and dark QR code icons for Keystone troubleshooting s…
Charon-Fan Jul 7, 2025
3440e90
fix: update Keystone onboarding navigation and SVG icon dimensions
Charon-Fan Jul 8, 2025
2fd4a32
feat: implement KeystoneWallet and related components for wallet func…
Charon-Fan Jul 8, 2025
83a1acf
Merge branch 'main' into feat/keystone-v2
Charon-Fan Jul 8, 2025
e718afc
refactor: update getPublicKeyFor method to accept an object and remov…
Charon-Fan Jul 8, 2025
2420a73
chore: remove unused deps
Charon-Fan Jul 8, 2025
d250eeb
fix: update CreatePin to use active wallet ID when creating a new wallet
Charon-Fan Jul 8, 2025
2d4363b
feat: enhance Ethereum signature handling in KeystoneWallet
Charon-Fan Jul 10, 2025
2556db8
Merge branch 'main' into feat/keystone-v2
Charon-Fan Jul 10, 2025
4c1701f
Merge branch 'main' into feat/keystone-v2
Charon-Fan Jul 14, 2025
5695bea
feat: refactor Keystone integration and improve wallet data handling
Charon-Fan Jul 18, 2025
1b2a38a
feat: enhance Keystone troubleshooting screen with step-by-step instr…
Charon-Fan Jul 18, 2025
f6b404f
feat: implement Keystone blocking logic and update access wallet navi…
Charon-Fan Jul 18, 2025
b2c32c2
Merge branch 'main' into feat/keystone-v2
Charon-Fan Jul 18, 2025
2b2c3b6
feat: add KeystoneWallet implementation and related tests
Charon-Fan Jul 21, 2025
82b9049
Merge branch 'main' into feat/keystone-v2
Charon-Fan Jul 21, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/core-mobile/app/assets/icons/keystone.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions packages/core-mobile/app/assets/icons/qrcode_dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions packages/core-mobile/app/assets/icons/qrcode_light.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions packages/core-mobile/app/hardware/services/KeystoneService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { UR } from '@ngraveio/bc-ur'
import KeystoneSDK from '@keystonehq/keystone-sdk'
import { fromPublicKey } from 'bip32'
import { KeystoneDataStorage } from 'features/keystone/storage/KeystoneDataStorage'

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you move this service into app/new/features/keystone/services?

class KeystoneService {
private walletInfo = {
evm: '',
xp: '',
mfp: ''
}

init(ur: UR): void {
const sdk = new KeystoneSDK()
const accounts = sdk.parseMultiAccounts(ur)
const mfp = accounts.masterFingerprint
const ethAccount = accounts.keys.find(key => key.chain === 'ETH')
const avaxAccount = accounts.keys.find(key => key.chain === 'AVAX')
if (!ethAccount || !avaxAccount) {
throw new Error('No ETH or AVAX account found')
}

this.walletInfo.evm = fromPublicKey(
Buffer.from(ethAccount.publicKey, 'hex'),
Buffer.from(ethAccount.chainCode, 'hex')
).toBase58()
this.walletInfo.xp = fromPublicKey(
Buffer.from(avaxAccount.publicKey, 'hex'),
Buffer.from(avaxAccount.chainCode, 'hex')
).toBase58()
this.walletInfo.mfp = mfp
}

async save(): Promise<void> {
await KeystoneDataStorage.save(this.walletInfo)
}
}

export default new KeystoneService()
21 changes: 21 additions & 0 deletions packages/core-mobile/app/hardware/wallet/keystoneSigner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { UR } from '@ngraveio/bc-ur'
import { requestKeystoneSigner } from 'features/keystone/utils'

export const signer = async (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you move this signer file to app/services/wallet?

request: UR,
responseURTypes: string[],
handleResult: (cbor: Buffer) => Promise<string>
): Promise<string> => {
return new Promise<string>((resolve, reject) => {
requestKeystoneSigner({
request,
responseURTypes,
onReject: (message?: string) => {
reject(message ?? 'User rejected')
},
onApprove: (cbor: Buffer) => {
return handleResult(cbor).then(resolve).catch(reject)
}
})
})
}
131 changes: 131 additions & 0 deletions packages/core-mobile/app/new/common/components/KeystoneQrScanner.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import React, { useState, useCallback, useEffect } from 'react'
import { UR, URDecoder } from '@ngraveio/bc-ur'
import * as Progress from 'react-native-progress'
import { View, Text, SCREEN_WIDTH, useTheme } from '@avalabs/k2-alpine'
import { showKeystoneTroubleshooting } from 'features/keystone/utils'
import { QrCodeScanner } from './QrCodeScanner'
import { Space } from './Space'

const SCANNER_WIDTH = SCREEN_WIDTH - 64

interface Props {
urTypes: string[]
onSuccess: (ur: UR) => void
onError?: () => void
info?: string
}

export const KeystoneQrScanner: (props: Props) => JSX.Element = ({
info,
urTypes,
onSuccess,
onError
}) => {
const [urDecoder, setUrDecoder] = useState(new URDecoder())
const [progress, setProgress] = useState<number>(0)
const [showTroubleshooting, setShowTroubleshooting] = useState(false)
const { theme } = useTheme()

const progressColor = theme.isDark ? theme.colors.$white : theme.colors.$black
const handleError = useCallback(() => {
setUrDecoder(new URDecoder())
setShowTroubleshooting(true)
if (onError) {
onError()
}
}, [onError])

const showErrorSheet = useCallback(() => {
showKeystoneTroubleshooting({
errorCode: -1,
retry: () => {
setShowTroubleshooting(false)
setProgress(0)
}
})
}, [])

useEffect(() => {
if (showTroubleshooting && !onError) {
showErrorSheet()
}
}, [showTroubleshooting, showErrorSheet, onError])

const handleScan = useCallback(
(code: string) => {
if (showTroubleshooting) {
return
}
try {
urDecoder.receivePart(code)
if (!urDecoder.isComplete()) {
setProgress(urDecoder.estimatedPercentComplete())
return
}

if (urDecoder.isError()) {
handleError()
}

if (urDecoder.isSuccess()) {
const ur = urDecoder.resultUR()

if (urTypes.includes(ur.type)) {
setProgress(1)

onSuccess(ur)
} else {
throw new Error('Invalid qr code')
}
}
} catch (error) {
handleError()
}
},
[
setProgress,
onSuccess,
urDecoder,
urTypes,
handleError,
showTroubleshooting
]
)

return (
<View sx={{ alignItems: 'center', height: '100%' }}>
<QrCodeScanner
paused={showTroubleshooting}
onSuccess={handleScan}
vibrate={progress !== 1}
sx={{
width: SCANNER_WIDTH,
height: SCANNER_WIDTH
}}
/>
<View
sx={{
alignItems: 'center',
flexDirection: 'column'
}}>
<Space y={70} />
<Progress.Bar
borderRadius={8}
color={progressColor}
width={290}
height={16}
progress={progress}
style={{
opacity: [0, 1].includes(progress) ? 0 : 1
}}
/>
{info && (
<>
<Space y={32} />
<Text>{info}</Text>
</>
)}
</View>
</View>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export const NavigationRedirect = (): null => {
} else if (
pathName === '/onboarding/mnemonic/confirmation' ||
pathName === '/onboarding/seedless/confirmation' ||
pathName === '/onboarding/keystone/confirmation' ||
(pathName === '/loginWithPinOrBiometry' && !isSignedIn)
) {
// must call dismissAll() here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ type Props = {
onSuccess: (data: string) => void
vibrate?: boolean
sx?: SxProp
paused?: boolean
}

export const QrCodeScanner = ({
onSuccess,
vibrate = false,
sx
sx,
paused = false
}: Props): React.JSX.Element | undefined => {
const {
theme: { colors }
Expand All @@ -40,20 +42,27 @@ export const QrCodeScanner = ({
)
const [data, setData] = useState<string>()

const handleSuccess = (scanningResult: BarcodeScanningResult): void => {
// expo-camera's onBarcodeScanned callback is not debounced, so we need to debounce it ourselves
setData(scanningResult.data)
}
useEffect(() => {
if (paused) {
setData(undefined)
}
}, [paused, setData])

useEffect(() => {
if (data) {
if (data && !paused) {
onSuccess(data)

if (vibrate) {
notificationAsync(NotificationFeedbackType.Success)
}
}
}, [data, onSuccess, vibrate])
}, [data, onSuccess, vibrate, paused])

const handleSuccess = (scanningResult: BarcodeScanningResult): void => {
if (paused) return
// expo-camera's onBarcodeScanned callback is not debounced, so we need to debounce it ourselves
setData(scanningResult.data)
}

const checkIosPermission = useCallback(async () => {
if (
Expand Down
Loading