Skip to content
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

Add keystone #907

Open
wants to merge 7 commits into
base: release/4.8.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
65 changes: 65 additions & 0 deletions packages/@core-js/src/utils/tonProof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,58 @@ export interface TonProofArgs {
payload: string;
}

export async function createUnsignedTonProof({
address: _addr,
payload,
walletStateInit,
domain,
}) {
try {
const address = Address.parse(_addr).toRaw();
const timestamp = await getRawTimeFromLiteserverSafely();
const timestampBuffer = new Int64LE(timestamp).toBuffer();

const domainBuffer = Buffer.from(domain);
const domainLengthBuffer = Buffer.allocUnsafe(4);
domainLengthBuffer.writeInt32LE(domainBuffer.byteLength);

const [workchain, addrHash] = address.split(':');

const addressWorkchainBuffer = Buffer.allocUnsafe(4);
addressWorkchainBuffer.writeInt32BE(Number(workchain));

const addressBuffer = Buffer.concat([
addressWorkchainBuffer,
Buffer.from(addrHash, 'hex'),
]);

const messageBuffer = Buffer.concat([
Buffer.from('ton-proof-item-v2/'),
addressBuffer,
domainLengthBuffer,
domainBuffer,
timestampBuffer,
Buffer.from(payload),
]);

return {
address,
proof: {
timestamp,
domain: {
length_bytes: domainBuffer.byteLength,
value: domain,
},
payload,
state_init: walletStateInit,
},
messageBuffer: messageBuffer,
};
} catch (e) {
throw new Error('Failed to create proof');
}
}

export async function createTonProof({
address: _addr,
payload,
Expand Down Expand Up @@ -79,6 +131,19 @@ export async function createTonProof({
}
}

export async function createUnsignedSignProofForHardware(
addressRaw: string,
payload: string,
walletStateInit: string,
) {
return createUnsignedTonProof({
address: addressRaw,
payload,
walletStateInit,
domain: 'tonkeeper.com',
});
}

export async function signProofForTonkeeper(
addressRaw: string,
secretKey: Uint8Array,
Expand Down
1 change: 1 addition & 0 deletions packages/mobile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@craftzdog/react-native-buffer": "^6.0.5",
"@expo/react-native-action-sheet": "^4.0.1",
"@gorhom/bottom-sheet": "^4.6.0",

Choose a reason for hiding this comment

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

Suggested change
"@gorhom/bottom-sheet": "^4.6.0",
+ [**** ] "@gorhom/bottom-sheet": "^4.6.0",

"@keystonehq/keystone-sdk": "^0.7.5",
"@ledgerhq/hw-transport": "^6.30.6",
"@ledgerhq/react-native-hw-transport-ble": "^6.32.5",
"@rainbow-me/animated-charts": "https://github.com/tonkeeper/react-native-animated-charts#737b1633c41e13da437c8e111c4aedd15bd10558",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const useDAppBridge = (walletAddress: string, webViewUrl: string) => {
protocolVersion: CURRENT_PROTOCOL_VERSION,
isWalletBrowser: true,
connect: async (protocolVersion, request) => {
if (tk.wallet.isExternal || tk.wallet.isWatchOnly) {
if ((tk.wallet.isExternal || tk.wallet.isWatchOnly) && !tk.wallet.isKeystone) {
return new ConnectEventError(
CONNECT_EVENT_ERROR_CODES.METHOD_NOT_SUPPORTED,
'',
Expand Down
56 changes: 56 additions & 0 deletions packages/mobile/src/core/KeystoneQRCode/KeystoneQRCode.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { UR, UREncoder } from '@keystonehq/keystone-sdk';
import { View, ViewStyle, deviceWidth, ns } from '@tonkeeper/uikit';
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { Animated, LayoutChangeEvent, StyleSheet } from 'react-native';
import QRCode from 'react-native-qrcode-styled';
import { useAnimatedStyle, useSharedValue } from 'react-native-reanimated';

export type KeystoneQRCodeProps = {
ur: UR;
};

export const QR_SIZE = deviceWidth - ns(16) * 2 - ns(24) * 2;

export const QR_WRAP_STYLE: ViewStyle = {
width: QR_SIZE,
height: QR_SIZE,
alignItems: 'center',
justifyContent: 'center',
};

export const KeystoneQRCode = ({ ur }: KeystoneQRCodeProps) => {
const encoder = useMemo(() => {
return new UREncoder(ur, 200);
}, [ur]);
const [data, setData] = useState(encoder.nextPart().toUpperCase());
useEffect(() => {
const id = setInterval(() => {
setData(encoder.nextPart().toUpperCase());
}, 100);
return () => {
clearInterval(id);
};
}, [encoder, setData]);

const qrCodeScale = useSharedValue(1);

const handleQrCodeLayout = useCallback(
(e: LayoutChangeEvent) => {
qrCodeScale.value = QR_SIZE / e.nativeEvent.layout.width;
},
[qrCodeScale],
);

return (
<View style={QR_WRAP_STYLE}>
<QRCode data={data} padding={20} style={styles.qrcode} onLayout={handleQrCodeLayout} pieceSize={4} />
</View>
);
};

const styles = StyleSheet.create({
qrcode: {
backgroundColor: '#FFF',
borderRadius: 16,
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { RouteProp } from '@react-navigation/native';
import { AppStackRouteNames } from '$navigation';
import { AppStackParamList } from '$navigation/AppStack';

export enum KeystoneScanState {
SUCCESS,
FAILED,
}

export interface KeystoneScanStatus {
state: KeystoneScanState,
errorMessage: string,
}

export interface KeystoneScanQRProps {
route: RouteProp<AppStackParamList, AppStackRouteNames.KeystoneScanQR>;
}
Loading