Skip to content
Snippets Groups Projects
Commit 6e41f854 authored by Yuri's avatar Yuri
Browse files

Add polkadot api deps

parent 11c5cd26
No related branches found
No related tags found
No related merge requests found
...@@ -35,3 +35,4 @@ yarn-error.log* ...@@ -35,3 +35,4 @@ yarn-error.log*
next-env.d.ts next-env.d.ts
todo.md todo.md
.idea
# Add files here to ignore them from prettier formatting
/dist
/coverage
{
"trailingComma": "all",
"singleQuote": true,
"bracketSpacing": true,
"bracketSameLine": false,
"parser": "typescript",
"printWidth": 100,
"tabWidth": 2,
"useTabs": false,
"semi": true
}
import '@polkadot/api-augment';
...@@ -9,6 +9,8 @@ ...@@ -9,6 +9,8 @@
"lint": "next lint" "lint": "next lint"
}, },
"dependencies": { "dependencies": {
"@polkadot/api": "^10.3.4",
"@polkadot/api-augment": "^10.3.4",
"@polkadot/extension-dapp": "^0.45.5", "@polkadot/extension-dapp": "^0.45.5",
"@polkadot/react-identicon": "^3.1.4", "@polkadot/react-identicon": "^3.1.4",
"@talismn/connect-wallets": "^1.2.3", "@talismn/connect-wallets": "^1.2.3",
...@@ -29,5 +31,8 @@ ...@@ -29,5 +31,8 @@
"tsparticles-engine": "^2.9.3", "tsparticles-engine": "^2.9.3",
"tsparticles-preset-confetti": "^2.9.3", "tsparticles-preset-confetti": "^2.9.3",
"typescript": "5.0.4" "typescript": "5.0.4"
},
"devDependencies": {
"prettier": "^2.8.7"
} }
} }
import NextAuth, { NextAuthOptions } from 'next-auth' import NextAuth, { NextAuthOptions } from 'next-auth';
import CredentialsProvider from 'next-auth/providers/credentials' import CredentialsProvider from 'next-auth/providers/credentials';
import "@polkadot/api-augment"
import { signatureVerify } from '@polkadot/util-crypto'; import { signatureVerify } from '@polkadot/util-crypto';
import { encodeAddress } from '@polkadot/keyring' import { encodeAddress } from '@polkadot/keyring';
import { ApiPromise, WsProvider } from "@polkadot/api" import { ApiPromise, WsProvider } from '@polkadot/api';
import { BN } from '@polkadot/util' import { BN } from '@polkadot/util';
declare module 'next-auth' { declare module 'next-auth' {
interface Session { interface Session {
address: string | undefined address: string | undefined;
ksmAddress: string ksmAddress: string;
freeBalance: BN freeBalance: BN;
} }
interface User { interface User {
id: string id: string;
ksmAddress: string ksmAddress: string;
freeBalance: BN freeBalance: BN;
} }
interface credentials { interface credentials {
address: string address: string;
message: string message: string;
signature: string signature: string;
csrfToken: string, csrfToken: string;
} }
} }
...@@ -38,59 +37,67 @@ export const authOptions: NextAuthOptions = { ...@@ -38,59 +37,67 @@ export const authOptions: NextAuthOptions = {
placeholder: '0x0', placeholder: '0x0',
}, },
message: { message: {
label: "Message", label: 'Message',
type: "text", type: 'text',
placeholder: "0x0", placeholder: '0x0',
}, },
signature: { signature: {
label: "Signature", label: 'Signature',
type: "text", type: 'text',
placeholder: "0x0", placeholder: '0x0',
}, },
csrfToken: { csrfToken: {
label: "CSRF Token", label: 'CSRF Token',
type: "text", type: 'text',
placeholder: "0x0", placeholder: '0x0',
}, },
name: { name: {
label: "Name", label: 'Name',
type: "text", type: 'text',
placeholder: "name", placeholder: 'name',
} },
}, },
async authorize(credentials): Promise<any | null> { async authorize(credentials): Promise<any | null> {
if (credentials === undefined) { return null } if (credentials === undefined) {
return null;
}
try { try {
const message = JSON.parse(credentials.message) const message = JSON.parse(credentials.message);
//verify the message is from the same domain //verify the message is from the same domain
console.log( message.uri, message.uri ) console.log(message.uri, message.uri);
console.log( 'process.env.NEXTAUTH_URL', process.env.NEXTAUTH_URL ) console.log('process.env.NEXTAUTH_URL', process.env.NEXTAUTH_URL);
if (message.uri !== process.env.NEXTAUTH_URL) { if (message.uri !== process.env.NEXTAUTH_URL) {
return Promise.reject(new Error('🚫 You shall not pass!')) return Promise.reject(new Error('🚫 You shall not pass!'));
} }
// verify the message was not compromised // verify the message was not compromised
console.log( message.nonce, message.nonce ) console.log(message.nonce, message.nonce);
console.log( 'credentials.csrfToken', credentials.csrfToken ) console.log('credentials.csrfToken', credentials.csrfToken);
if (message.nonce !== credentials.csrfToken) { if (message.nonce !== credentials.csrfToken) {
return Promise.reject(new Error('🚫 You shall not pass!')) return Promise.reject(new Error('🚫 You shall not pass!'));
} }
// verify signature of the message // verify signature of the message
const { isValid } = signatureVerify(credentials.message, credentials.signature, credentials.address); const { isValid } = signatureVerify(
credentials.message,
credentials.signature,
credentials.address,
);
if (!isValid) { if (!isValid) {
return Promise.reject(new Error('🚫 Invalid Signature')) return Promise.reject(new Error('🚫 Invalid Signature'));
} }
// verify the account has the defined token // verify the account has the defined token
const wsProvider = new WsProvider( process.env.RPC_ENDPOINT ?? 'wss://kusama-rpc.dwellir.com' ) const wsProvider = new WsProvider(
process.env.RPC_ENDPOINT ?? 'wss://kusama-rpc.dwellir.com',
);
const api = await ApiPromise.create({ provider: wsProvider }); const api = await ApiPromise.create({ provider: wsProvider });
if (credentials?.address) { if (credentials?.address) {
const ksmAddress = encodeAddress( credentials.address, 2 ) const ksmAddress = encodeAddress(credentials.address, 2);
const accountInfo = await api.query.system.account( ksmAddress ) const accountInfo = await api.query.system.account(ksmAddress);
if (accountInfo.data.free.gt(new BN(1_000_000_000_000))) { if (accountInfo.data.free.gt(new BN(1_000_000_000_000))) {
// if the user has a free balance > 1 KSM, we let them in // if the user has a free balance > 1 KSM, we let them in
...@@ -99,15 +106,15 @@ export const authOptions: NextAuthOptions = { ...@@ -99,15 +106,15 @@ export const authOptions: NextAuthOptions = {
name: credentials.name, name: credentials.name,
freeBalance: accountInfo.data.free, freeBalance: accountInfo.data.free,
ksmAddress, ksmAddress,
} };
} else { } else {
return Promise.reject(new Error('🚫 The gate is closed for you')) return Promise.reject(new Error('🚫 The gate is closed for you'));
} }
} }
return Promise.reject(new Error('🚫 API Error')) return Promise.reject(new Error('🚫 API Error'));
} catch (e) { } catch (e) {
return null return null;
} }
}, },
}), }),
...@@ -122,23 +129,23 @@ export const authOptions: NextAuthOptions = { ...@@ -122,23 +129,23 @@ export const authOptions: NextAuthOptions = {
callbacks: { callbacks: {
async jwt({ token, user }) { async jwt({ token, user }) {
if (user) { if (user) {
token.freeBalance = user.freeBalance token.freeBalance = user.freeBalance;
} }
return token return token;
}, },
async session(sessionData) { async session(sessionData) {
const { session, token } = sessionData const { session, token } = sessionData;
session.address = token.sub session.address = token.sub;
if (session.address) { if (session.address) {
session.ksmAddress = encodeAddress( session.address, 2 ) session.ksmAddress = encodeAddress(session.address, 2);
} }
// as we already queried it, we can add whatever token to the session, // as we already queried it, we can add whatever token to the session,
// so pages can use it without an extra query // so pages can use it without an extra query
session.freeBalance = token.freeBalance as BN session.freeBalance = token.freeBalance as BN;
return session return session;
}, },
}, },
secret: process.env.NEXTAUTH_SECRET, secret: process.env.NEXTAUTH_SECRET,
...@@ -148,7 +155,6 @@ export const authOptions: NextAuthOptions = { ...@@ -148,7 +155,6 @@ export const authOptions: NextAuthOptions = {
error: '/', error: '/',
newUser: '/', newUser: '/',
}, },
} };
export default NextAuth(authOptions) export default NextAuth(authOptions);
yarn.lock 0 → 100644
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment