Skip to content
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
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"dependencies": {
"@ethersproject/shims": "^5.7.0",
"@expo-google-fonts/roboto": "^0.2.3",
"@greymass/buoy": "^1.0.4",
"@hcaptcha/react-native-hcaptcha": "^1.7.2",
"@json-rpc-tools/utils": "^1.7.6",
"@react-native-async-storage/async-storage": "1.23.1",
Expand All @@ -39,6 +40,7 @@
"@react-navigation/native-stack": "^6.11.0",
"@sinonjs/text-encoding": "^0.7.3",
"@tonomy/tonomy-id-sdk": "0.24.0-development.1",
"@types/events": "^3.0.3",
"@veramo/core": "^6.0.0",
"@walletconnect/core": "^2.17.2",
"@walletconnect/react-native-compat": "^2.17.2",
Expand All @@ -50,9 +52,12 @@
"@wharfkit/signing-request": "^3.2.0",
"@wharfkit/wallet-plugin-privatekey": "^1.1.0",
"big-integer": "^1.6.52",
"buoy": "^1.0.0",
"buoy-js": "^1.2.1",
"cross-fetch": "^4.0.0",
"crypto-browserify": "^3.12.1",
"deepmerge": "^4.3.1",
"electron": "^33.2.0",
"ethers": "^6.13.4",
"expo": "^51.0.38",
"expo-barcode-scanner": "^13.0.1",
Expand Down Expand Up @@ -92,6 +97,7 @@
"react-native-swiper": "^1.6.0",
"react-native-walkthrough-tooltip": "^1.6.0",
"react-native-web": "^0.19.13",
"react-native-websocket": "^1.0.2",
"react-native-webview": "13.8.6",
"reflect-metadata": "^0.2.2",
"stream-browserify": "^3.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/settings.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Debug from 'debug';

const debug = Debug('tonomy-id:settings');
const env = process.env.EXPO_NODE_ENV ?? 'development';
const env = process.env.EXPO_NODE_ENV ?? 'testnet';

const settingsInputs = {
nodeEnv: process.env.NODE_ENV, // This is set by expo with webpack https://github.com/expo/expo/issues/20360
Expand Down
12 changes: 9 additions & 3 deletions src/store/sessionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@ export const useSessionStore = create<SessionState>((set, get) => ({
initializeSession: async () => {
const walletConnectSession = new WalletConnectSession();

await walletConnectSession.initialize();
walletConnectSession.onEvent();
if (!walletConnectSession.initialized) {
await walletConnectSession.initialize();
walletConnectSession.onEvent();
}

const antelopeSession = new AntelopeSession();

await antelopeSession.initialize();
if (!antelopeSession.initialized) {
await antelopeSession.initialize();
// antelopeSession.onEvent();
}

// Set the session in the store
set({ walletConnectSession, antelopeSession });
},
Expand Down
8 changes: 6 additions & 2 deletions src/utils/chain/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import Web3Wallet from '@walletconnect/web3wallet';
import { sha256 } from '@tonomy/tonomy-id-sdk';
import Debug from 'debug';
import { navigate } from '../../services/NavigationService';
import BuoySession from 'buoy';
import { Listener } from '@greymass/buoy';

const debug = Debug('tonomy-id:utils:chain:types');

Expand Down Expand Up @@ -457,17 +459,19 @@ export interface ITransactionRequest {

export interface ISession {
web3wallet?: Web3Wallet;
listener?: Listener;
sessionUUID?: string;
initialize(): Promise<void>;
onQrScan(data: string): Promise<void>; // make this function static
onLink(data: string): Promise<void>; // make this function static
onEvent(request: unknown): Promise<void>;
onEvent(): Promise<void>;
}

export abstract class AbstractSession implements ISession {
abstract initialize(): Promise<void>;
abstract onQrScan(data: string): Promise<void>;
abstract onLink(data: string): Promise<void>;
abstract onEvent(request?: unknown): Promise<void>;
abstract onEvent(): Promise<void>;

protected abstract handleLoginRequest(request: unknown): Promise<void>;
protected abstract handleTransactionRequest(request: unknown): Promise<void>;
Expand Down
130 changes: 116 additions & 14 deletions src/utils/session/antelope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,22 @@ import * as SecureStore from 'expo-secure-store';
import useUserStore from '../../store/userStore';
import { createUrl, getQueryParam } from '../strings';
import { v4 as uuidv4 } from 'uuid';

import Debug from 'debug';
import BuoyService from 'buoy';
import { Listener, ListenerEncoding } from '@greymass/buoy';
import WebSocket from 'react-native-websocket';

const debug = Debug('tonomy-id:utils:session:antelope');

export const events: Record<string, string> = {
SESSION_ADD: 'SESSION_ADD',
SESSION_ADD_IDENTITY: 'SESSION_ADD_IDENTITY',
SESSION_CONFIG: 'SESSION_CONFIG',
SESSION_REMOVE: 'SESSION_REMOVE',
};

export default events;

export class AntelopeLoginRequest implements ILoginRequest {
loginApp: ILoginApp;
account: IAccount[];
Expand Down Expand Up @@ -114,21 +125,17 @@ export class AntelopeLoginRequest implements ILoginRequest {

// Generate the identity proof signature
const publicKey = await (await this.privateKey.getPublicKey()).toString();
const receiveChUUID = uuidv4();
const forwarderAddress = 'https://pangea.web4.world';
const signature = await this.request.getIdentityProof(transaction.signatures[0]);

const receiveCh = `${forwarderAddress}/${receiveChUUID}`;
const signature = await this.request.getIdentityProof(transaction.signatures[0]);

// Construct the payload
const bodyObject = callbackParams.payload;

bodyObject.account = account.getName();
bodyObject.link_key = publicKey;
bodyObject.proof = signature.toString();
bodyObject.id_proof = signature.toString();
bodyObject.chainId = chain.getChainId();
bodyObject.link_ch = receiveCh;

bodyObject.link_ch = 'wss://cb.anchor.link/' + this.session.sessionUUID;
console.log('approveLoginRequest() callback', bodyObject);
const response = await fetch(callbackParams.url, {
method: 'POST',
headers: {
Expand Down Expand Up @@ -233,7 +240,7 @@ export class AntelopeTransactionRequest implements ITransactionRequest {
const trxId = receipt.getTransactionHash();
const callbackParams = this.request.getCallback(signedTransaction.signatures, 0);

debug('approveTransactionRequest() callbackParams', callbackParams);
console.log('approveTransactionRequest() callbackParams', callbackParams);

if (callbackParams) {
const uid = getQueryParam(callbackParams.url, 'uid');
Expand All @@ -242,14 +249,14 @@ export class AntelopeTransactionRequest implements ITransactionRequest {

bodyObject.tx = trxId;
// eslint-disable-next-line camelcase
bodyObject.tx_id = trxId;
bodyObject.uid = uid;

// eslint-disable-next-line camelcase
bodyObject.tx_id = trxId;
// eslint-disable-next-line camelcase
const newCallback = createUrl(callbackParams.url, { uid, tx_id: trxId });

debug('approveTransactionRequest() callback', new Date(), newCallback, bodyObject);
const response = await fetch(newCallback, {
const response = await fetch(callbackParams.url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down Expand Up @@ -281,10 +288,75 @@ export class AntelopeSession extends AbstractSession {
client: APIClient;
publicKey: AntelopePublicKey;
accountName: string;
initialized: boolean;
listener: Listener;
sessionUUID: string;

async initialize(): Promise<void> {
debug('initialize()');
console.log('initialize()', this.initialized);
await this.getAccountName();

if (this.initialized) {
console.log('Already initialized');
return;
}

// const service = new BuoyService();

// this.buoySession = service;
// console.log('service', service);
// service.on('message', (message: string) => {
// console.log('Buoy Message:', message);
// });

this.sessionUUID = uuidv4();

this.listener = new Listener({ service: 'https://cb.anchor.link', channel: this.sessionUUID });
this.listener.on('message', (message) => {
console.log('message', message);
});

// make sure to subscribe to the error event or they will be thrown
this.listener.on('error', (error) => {
console.warn('listener error', error);
});
// const forwarderAddress = 'ws://cb.anchor.link';

// // Create a unique session URL
// this.sessionUUID = uuidv4();
// const buoySessionUrl = `${forwarderAddress}/${this.sessionUUID}`;

// const socket = new WebSocket(buoySessionUrl);

// this.buoySession = socket;
// console.log('Buoy Session:', this.buoySession);
// socket.addEventListener('open', () => {
// console.log('Buoy session established.');
// socket.send(JSON.stringify({ type: 'ping' })); // Optional: Ping message
// });

// socket.addEventListener('message', (event) => {
// console.log('Buoy Message:', event.data);
// this.handleBuoyMessage(event.data);
// });

// socket.addEventListener('close', () => {
// console.log('Buoy session closed.');
// // Reconnect logic
// setTimeout(() => {
// this.initialize();
// }, 1000); // Reconnect after 1 second
// });

// socket.addEventListener('error', (error) => {
// console.error('Buoy session error:', error);
// // Reconnect logic
// setTimeout(() => {
// this.initialize();
// }, 1000); // Reconnect after 1 second
// });

this.initialized = true;
}

async fromChain(chain: AntelopeChain): Promise<void> {
Expand Down Expand Up @@ -353,8 +425,38 @@ export class AntelopeSession extends AbstractSession {

async onEvent(): Promise<void> {
//TODO when implement listen antelope events
console.log('onEvent()', this.listener);
this.listener.on('message', (message) => {
console.log('message', message);
});

// make sure to subscribe to the error event or they will be thrown
this.listener.on('error', (error) => {
console.warn('listener error', error);
});
}

// private handleBuoyMessage(message: string): void {
// try {
// const parsedMessage = JSON.parse(message);

// console.log('Received message from Buoy:', parsedMessage);

// switch (parsedMessage.type) {
// case 'login_request':
// this.handleLoginRequest(parsedMessage.data);
// break;
// case 'transaction_request':
// this.handleTransactionRequest(parsedMessage.data);
// break;
// default:
// console.log('Unknown Buoy message type:', parsedMessage.type);
// }
// } catch (error) {
// console.error('Failed to handle Buoy message:', error);
// }
// }

async handleLoginRequest(resolvedSigningRequest: ResolvedSigningRequest): Promise<void> {
//TODO when implement handle identity request
const callback = resolvedSigningRequest.request.data.callback;
Expand Down
Loading
Loading