Skip to content

Commit

Permalink
Update dependencies, make the demo app work with a dev build, add lin…
Browse files Browse the repository at this point in the history
…ter (#31)

* build

* get rid of logs

* fix lint

* newline

* reset yarn.lock

* expo doctor
  • Loading branch information
tian000 authored Jan 30, 2024
1 parent 20f19f2 commit bbf38ce
Show file tree
Hide file tree
Showing 10 changed files with 5,448 additions and 2,946 deletions.
4 changes: 4 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
root: true,
extends: ["universe/native"],
};
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ web-build/

# macOS
.DS_Store

android/
ios/
72 changes: 37 additions & 35 deletions App.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import "react-native-get-random-values";
import "react-native-url-polyfill/auto";
import { clusterApiUrl, Connection, PublicKey, SystemProgram, Transaction } from "@solana/web3.js";
import bs58 from "bs58";
import { Buffer } from "buffer";
global.Buffer = global.Buffer || Buffer;
import React, { useCallback, useEffect, useRef, useState } from "react";
import { Button, Platform, ScrollView, StyleSheet, Text, View } from "react-native";
import { StatusBar } from "expo-status-bar";
import * as Linking from "expo-linking";
import { StatusBar } from "expo-status-bar";
import React, { useCallback, useEffect, useRef, useState } from "react";
import { Button, Platform, ScrollView, Text, View } from "react-native";
import nacl from "tweetnacl";
import bs58 from "bs58";
import {
clusterApiUrl,
Connection,
Keypair,
PublicKey,
SystemProgram,
Transaction,
} from "@solana/web3.js";

global.Buffer = global.Buffer || Buffer;

const NETWORK = clusterApiUrl("mainnet-beta");

Expand All @@ -26,8 +20,16 @@ const onSignAllTransactionsRedirectLink = Linking.createURL("onSignAllTransactio
const onSignTransactionRedirectLink = Linking.createURL("onSignTransaction");
const onSignMessageRedirectLink = Linking.createURL("onSignMessage");

/**
* If true, uses universal links instead of deep links. This is the recommended way for dapps
* and Phantom to handle deeplinks as we own the phantom.app domain.
*
* Set this to false to use normal deeplinks, starting with phantom://. This is easier for
* debugging with a local build such as Expo Dev Client builds.
*/
const useUniversalLinks = false;
const buildUrl = (path: string, params: URLSearchParams) =>
`https://phantom.app/ul/v1/${path}?${params.toString()}`;
`${useUniversalLinks ? "https://phantom.app/ul/" : "phantom://"}v1/${path}?${params.toString()}`;

const decryptPayload = (data: string, nonce: string, sharedSecret?: Uint8Array) => {
if (!sharedSecret) throw new Error("missing shared secret");
Expand All @@ -47,7 +49,7 @@ const encryptPayload = (payload: any, sharedSecret?: Uint8Array) => {
const encryptedPayload = nacl.box.after(
Buffer.from(JSON.stringify(payload)),
nonce,
sharedSecret
sharedSecret,
);

return [nonce, encryptedPayload];
Expand All @@ -74,9 +76,9 @@ export default function App() {
setDeepLink(initialUrl);
}
})();
Linking.addEventListener("url", handleDeepLink);
const subscription = Linking.addEventListener("url", handleDeepLink);
return () => {
Linking.removeEventListener("url", handleDeepLink);
subscription.remove();
};
}, []);

Expand All @@ -96,60 +98,60 @@ export default function App() {
return;
}

if (/onConnect/.test(url.pathname)) {
if (/onConnect/.test(url.pathname || url.host)) {
const sharedSecretDapp = nacl.box.before(
bs58.decode(params.get("phantom_encryption_public_key")!),
dappKeyPair.secretKey
dappKeyPair.secretKey,
);

const connectData = decryptPayload(
params.get("data")!,
params.get("nonce")!,
sharedSecretDapp
sharedSecretDapp,
);

setSharedSecret(sharedSecretDapp);
setSession(connectData.session);
setPhantomWalletPublicKey(new PublicKey(connectData.public_key));

addLog(JSON.stringify(connectData, null, 2));
} else if (/onDisconnect/.test(url.pathname)) {
} else if (/onDisconnect/.test(url.pathname || url.host)) {
addLog("Disconnected!");
} else if (/onSignAndSendTransaction/.test(url.pathname)) {
} else if (/onSignAndSendTransaction/.test(url.pathname || url.host)) {
const signAndSendTransactionData = decryptPayload(
params.get("data")!,
params.get("nonce")!,
sharedSecret
sharedSecret,
);

addLog(JSON.stringify(signAndSendTransactionData, null, 2));
} else if (/onSignAllTransactions/.test(url.pathname)) {
} else if (/onSignAllTransactions/.test(url.pathname || url.host)) {
const signAllTransactionsData = decryptPayload(
params.get("data")!,
params.get("nonce")!,
sharedSecret
sharedSecret,
);

const decodedTransactions = signAllTransactionsData.transactions.map((t: string) =>
Transaction.from(bs58.decode(t))
Transaction.from(bs58.decode(t)),
);

addLog(JSON.stringify(decodedTransactions, null, 2));
} else if (/onSignTransaction/.test(url.pathname)) {
} else if (/onSignTransaction/.test(url.pathname || url.host)) {
const signTransactionData = decryptPayload(
params.get("data")!,
params.get("nonce")!,
sharedSecret
sharedSecret,
);

const decodedTransaction = Transaction.from(bs58.decode(signTransactionData.transaction));

addLog(JSON.stringify(decodedTransaction, null, 2));
} else if (/onSignMessage/.test(url.pathname)) {
} else if (/onSignMessage/.test(url.pathname || url.host)) {
const signMessageData = decryptPayload(
params.get("data")!,
params.get("nonce")!,
sharedSecret
sharedSecret,
);

addLog(JSON.stringify(signMessageData, null, 2));
Expand All @@ -158,12 +160,12 @@ export default function App() {

const createTransferTransaction = async () => {
if (!phantomWalletPublicKey) throw new Error("missing public key from user");
let transaction = new Transaction().add(
const transaction = new Transaction().add(
SystemProgram.transfer({
fromPubkey: phantomWalletPublicKey,
toPubkey: phantomWalletPublicKey,
lamports: 100,
})
}),
);
transaction.feePayer = phantomWalletPublicKey;
addLog("Getting recent blockhash");
Expand Down Expand Up @@ -236,8 +238,8 @@ export default function App() {
bs58.encode(
t.serialize({
requireAllSignatures: false,
})
)
}),
),
);

const payload = {
Expand Down Expand Up @@ -265,7 +267,7 @@ export default function App() {
const serializedTransaction = bs58.encode(
transaction.serialize({
requireAllSignatures: false,
})
}),
);

const payload = {
Expand Down
13 changes: 12 additions & 1 deletion app.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@
},
"assetBundlePatterns": [
"**/*"
]
],
"android": {
"package": "com.phantomwallet.deeplinkdemoapp"
},
"ios": {
"bundleIdentifier": "com.phantomwallet.deeplinkdemoapp"
},
"extra": {
"eas": {
"projectId": "6d8aec9a-b6d3-49ab-9262-c1b8c0913c57"
}
}
}
}
4 changes: 2 additions & 2 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = function(api) {
module.exports = function (api) {
api.cache(true);
return {
presets: ['babel-preset-expo'],
presets: ["babel-preset-expo"],
};
};
22 changes: 22 additions & 0 deletions eas.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"cli": {
"version": ">= 7.1.1"
},
"build": {
"development": {
"developmentClient": true,
"distribution": "internal"
},
"preview": {
"distribution": "internal"
},
"production": {
"android": {
"buildType": "apk"
}
}
},
"submit": {
"production": {}
}
}
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { registerRootComponent } from "expo";

import App from "./App";

// registerRootComponent calls AppRegistry.registerComponent('main', () => App);
// It also ensures that whether you load the app in Expo Go or in a native build,
// the environment is set up appropriately
registerRootComponent(App);
1 change: 1 addition & 0 deletions metro.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable */
const { getDefaultConfig } = require("@expo/metro-config");
const config = getDefaultConfig(__dirname);
config.resolver.sourceExts = [...config.resolver.sourceExts, "cjs"];
Expand Down
47 changes: 27 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,38 +1,45 @@
{
"name": "deep-link-demo-app",
"version": "1.0.0",
"main": "node_modules/expo/AppEntry.js",
"engines": {
"node": ">=15.0.0 <17.0.0"
"node": ">= 18.0.0"
},
"scripts": {
"start": "expo start --clear",
"android": "expo start --android",
"ios": "expo start --ios",
"web": "expo start --web",
"eject": "expo eject"
"native-prestart:android": "expo prebuild -p android",
"native-prestart:android:clean": "expo prebuild --clean -p android",
"native-prestart:ios": "expo prebuild -p ios",
"native-prestart:ios:clean": "expo prebuild --clean -p ios",
"watch": "expo start --dev-client --clear",
"watch:tunnel": " expo start --dev-client --clear --tunnel",
"android": "expo run:android",
"ios": "expo run:ios",
"build-android": "eas build --profile production -p android --local",
"lint": "eslint ."
},
"dependencies": {
"@solana/web3.js": "~1.35.0",
"buffer": "^6.0.3",
"expo": "~44.0.0",
"expo-linking": "~3.0.0",
"expo-status-bar": "~1.2.0",
"expo-updates": "~0.11.6",
"react": "17.0.1",
"react-dom": "17.0.1",
"react-native": "0.64.3",
"react-native-get-random-values": "^1.7.2",
"expo": "~48.0.21",
"expo-linking": "~4.0.1",
"expo-splash-screen": "~0.18.2",
"expo-status-bar": "~1.4.4",
"expo-updates": "~0.16.4",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-native": "0.71.14",
"react-native-get-random-values": "~1.9.0",
"react-native-url-polyfill": "^1.3.0",
"react-native-web": "0.17.1",
"react-native-web": "~0.18.11",
"tweetnacl": "^1.0.3"
},
"devDependencies": {
"@babel/core": "^7.12.9",
"@babel/core": "^7.20.0",
"@types/bs58": "^4.0.1",
"@types/react": "~17.0.21",
"@types/react-native": "~0.64.12",
"typescript": "~4.3.5"
"@types/react": "~18.0.27",
"eslint": "^8.56.0",
"eslint-config-universe": "^12.0.0",
"prettier": "^3.2.4",
"typescript": "^4.9.4"
},
"private": true
}
Loading

0 comments on commit bbf38ce

Please sign in to comment.