From 9e77f5146484d6431a6997047158e60921bc748f Mon Sep 17 00:00:00 2001 From: Ben Kremer Date: Wed, 16 Mar 2022 15:17:59 +0100 Subject: [PATCH] refactor(with-solana): use `solanaWallet.verifyMessageSignature` --- .../react-dapp-v2-with-solana-web3js/package.json | 2 +- dapps/react-dapp-v2-with-solana-web3js/src/App.tsx | 14 +++++--------- dapps/react-dapp-v2-with-solana-web3js/yarn.lock | 11 ++++++++++- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/dapps/react-dapp-v2-with-solana-web3js/package.json b/dapps/react-dapp-v2-with-solana-web3js/package.json index d30e7a2..f1c067b 100644 --- a/dapps/react-dapp-v2-with-solana-web3js/package.json +++ b/dapps/react-dapp-v2-with-solana-web3js/package.json @@ -45,8 +45,8 @@ "react": "^17.0.2", "react-dom": "^17.0.2", "react-scripts": "^4.0.3", + "solana-wallet": "^1.0.1", "styled-components": "^5.2.0", - "tweetnacl": "^1.0.3", "typescript": "^4.3.2", "web-vitals": "^0.2.4" }, diff --git a/dapps/react-dapp-v2-with-solana-web3js/src/App.tsx b/dapps/react-dapp-v2-with-solana-web3js/src/App.tsx index 466a3af..ba1e97c 100644 --- a/dapps/react-dapp-v2-with-solana-web3js/src/App.tsx +++ b/dapps/react-dapp-v2-with-solana-web3js/src/App.tsx @@ -2,7 +2,7 @@ import React, { useState } from "react"; import { version } from "@walletconnect/client/package.json"; import { clusterApiUrl, Connection, Keypair, SystemProgram, Transaction } from "@solana/web3.js"; import bs58 from "bs58"; -import nacl from "tweetnacl"; +import { verifyMessageSignature } from "solana-wallet"; import Banner from "./components/Banner"; import Blockchain from "./components/Blockchain"; @@ -166,8 +166,8 @@ export default function App() { const senderPublicKey = publicKeys[address]; // Encode message to `UInt8Array` first via `TextEncoder` so we can pass it to `bs58.encode`. - const message = new TextEncoder().encode( - `This is an example message to be signed - ${Date.now()}`, + const message = bs58.encode( + new TextEncoder().encode(`This is an example message to be signed - ${Date.now()}`), ); try { @@ -177,16 +177,12 @@ export default function App() { method: SolanaRpcMethod.SOL_SIGN_MESSAGE, params: { pubkey: senderPublicKey.toBase58(), - message: bs58.encode(message), + message, }, }, }); - const valid = nacl.sign.detached.verify( - message, - bs58.decode(signature), - senderPublicKey.toBytes(), - ); + const valid = verifyMessageSignature(senderPublicKey.toBase58(), signature, message); return { method: SolanaRpcMethod.SOL_SIGN_MESSAGE, diff --git a/dapps/react-dapp-v2-with-solana-web3js/yarn.lock b/dapps/react-dapp-v2-with-solana-web3js/yarn.lock index 0742e24..b82cd1a 100644 --- a/dapps/react-dapp-v2-with-solana-web3js/yarn.lock +++ b/dapps/react-dapp-v2-with-solana-web3js/yarn.lock @@ -1851,7 +1851,7 @@ dependencies: buffer "~6.0.3" -"@solana/web3.js@^1.36.0": +"@solana/web3.js@^1.35.1", "@solana/web3.js@^1.36.0": version "1.36.0" resolved "https://registry.yarnpkg.com/@solana/web3.js/-/web3.js-1.36.0.tgz#79d7d5217b49b80139f4de68953adc5b9a9a264f" integrity sha512-RNT1451iRR7TyW7EJKMCrH/0OXawIe4zVm0DWQASwXlR/u1jmW6FrmH0lujIh7cGTlfOVbH+2ZU9AVUPLBFzwA== @@ -11481,6 +11481,15 @@ sockjs@^0.3.21: uuid "^8.3.2" websocket-driver "^0.7.4" +solana-wallet@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/solana-wallet/-/solana-wallet-1.0.1.tgz#4e1bfebbb99640105ed42abd1445baab4f6e0b57" + integrity sha512-rkYu9gwayAdVMhWGdZSz6a+IaOJXs3TNtYXWuSQsdMJUndkQ+puy7cB9/u5pwZTjqzxtd1DN9cbAFKFH5648xQ== + dependencies: + "@solana/web3.js" "^1.35.1" + bs58 "^5.0.0" + tweetnacl "^1.0.3" + sonic-boom@^1.0.2: version "1.4.1" resolved "https://registry.yarnpkg.com/sonic-boom/-/sonic-boom-1.4.1.tgz#d35d6a74076624f12e6f917ade7b9d75e918f53e"