refactor(with-solana): use solanaWallet.verifyMessageSignature

This commit is contained in:
Ben Kremer 2022-03-16 15:17:59 +01:00
parent b382036c83
commit 9e77f51464
3 changed files with 16 additions and 11 deletions

View File

@ -45,8 +45,8 @@
"react": "^17.0.2", "react": "^17.0.2",
"react-dom": "^17.0.2", "react-dom": "^17.0.2",
"react-scripts": "^4.0.3", "react-scripts": "^4.0.3",
"solana-wallet": "^1.0.1",
"styled-components": "^5.2.0", "styled-components": "^5.2.0",
"tweetnacl": "^1.0.3",
"typescript": "^4.3.2", "typescript": "^4.3.2",
"web-vitals": "^0.2.4" "web-vitals": "^0.2.4"
}, },

View File

@ -2,7 +2,7 @@ import React, { useState } from "react";
import { version } from "@walletconnect/client/package.json"; import { version } from "@walletconnect/client/package.json";
import { clusterApiUrl, Connection, Keypair, SystemProgram, Transaction } from "@solana/web3.js"; import { clusterApiUrl, Connection, Keypair, SystemProgram, Transaction } from "@solana/web3.js";
import bs58 from "bs58"; import bs58 from "bs58";
import nacl from "tweetnacl"; import { verifyMessageSignature } from "solana-wallet";
import Banner from "./components/Banner"; import Banner from "./components/Banner";
import Blockchain from "./components/Blockchain"; import Blockchain from "./components/Blockchain";
@ -166,8 +166,8 @@ export default function App() {
const senderPublicKey = publicKeys[address]; const senderPublicKey = publicKeys[address];
// Encode message to `UInt8Array` first via `TextEncoder` so we can pass it to `bs58.encode`. // Encode message to `UInt8Array` first via `TextEncoder` so we can pass it to `bs58.encode`.
const message = new TextEncoder().encode( const message = bs58.encode(
`This is an example message to be signed - ${Date.now()}`, new TextEncoder().encode(`This is an example message to be signed - ${Date.now()}`),
); );
try { try {
@ -177,16 +177,12 @@ export default function App() {
method: SolanaRpcMethod.SOL_SIGN_MESSAGE, method: SolanaRpcMethod.SOL_SIGN_MESSAGE,
params: { params: {
pubkey: senderPublicKey.toBase58(), pubkey: senderPublicKey.toBase58(),
message: bs58.encode(message), message,
}, },
}, },
}); });
const valid = nacl.sign.detached.verify( const valid = verifyMessageSignature(senderPublicKey.toBase58(), signature, message);
message,
bs58.decode(signature),
senderPublicKey.toBytes(),
);
return { return {
method: SolanaRpcMethod.SOL_SIGN_MESSAGE, method: SolanaRpcMethod.SOL_SIGN_MESSAGE,

View File

@ -1851,7 +1851,7 @@
dependencies: dependencies:
buffer "~6.0.3" 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" version "1.36.0"
resolved "https://registry.yarnpkg.com/@solana/web3.js/-/web3.js-1.36.0.tgz#79d7d5217b49b80139f4de68953adc5b9a9a264f" resolved "https://registry.yarnpkg.com/@solana/web3.js/-/web3.js-1.36.0.tgz#79d7d5217b49b80139f4de68953adc5b9a9a264f"
integrity sha512-RNT1451iRR7TyW7EJKMCrH/0OXawIe4zVm0DWQASwXlR/u1jmW6FrmH0lujIh7cGTlfOVbH+2ZU9AVUPLBFzwA== integrity sha512-RNT1451iRR7TyW7EJKMCrH/0OXawIe4zVm0DWQASwXlR/u1jmW6FrmH0lujIh7cGTlfOVbH+2ZU9AVUPLBFzwA==
@ -11481,6 +11481,15 @@ sockjs@^0.3.21:
uuid "^8.3.2" uuid "^8.3.2"
websocket-driver "^0.7.4" 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: sonic-boom@^1.0.2:
version "1.4.1" version "1.4.1"
resolved "https://registry.yarnpkg.com/sonic-boom/-/sonic-boom-1.4.1.tgz#d35d6a74076624f12e6f917ade7b9d75e918f53e" resolved "https://registry.yarnpkg.com/sonic-boom/-/sonic-boom-1.4.1.tgz#d35d6a74076624f12e6f917ade7b9d75e918f53e"