Skip to content

Commit

Permalink
Merge pull request #714 from MoralisWeb3/default-networkType
Browse files Browse the repository at this point in the history
fix: rename networkType param and set default
  • Loading branch information
ErnoW authored Sep 30, 2022
2 parents 7533a10 + cb258e5 commit 555883f
Show file tree
Hide file tree
Showing 45 changed files with 940 additions and 139 deletions.
7 changes: 7 additions & 0 deletions .changeset/pretty-mayflies-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@moralisweb3/auth': patch
'@moralisweb3/evm-utils': patch
'@moralisweb3/streams': patch
---

Rename `network` param to `networkType` for `Moralis.Streams` and `Moralis.Auth`, to communicate more clearly the purpose of this param. Also make this value optional and default to `"evm"`
4 changes: 2 additions & 2 deletions demos/firebase-auth/functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const requestMessage = functions.https.onCall(async (data: RequestMessage

const response = await Moralis.Auth.requestMessage({
chain: EvmChain.create(data.chain),
network: 'evm',
networkType: 'evm',
timeout: 15,
domain: 'mydomain.com',
uri: 'https://mydomain.com/my-uri',
Expand All @@ -53,7 +53,7 @@ interface IssueTokenData {
export const issueToken = functions.https.onCall(async (data: IssueTokenData) => {
const response = await Moralis.Auth.verify({
message: data.message,
network: 'evm',
networkType: 'evm',
signature: data.signature,
});
const uid = response.result.profileId;
Expand Down
5 changes: 3 additions & 2 deletions demos/moralis-stream/public/script.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* global document, window, axios*/
const STREAM_API_URL = 'http://localhost:3000/stream';
const HOOK_API_URL = 'http://localhost:3000/hooks';

Expand Down Expand Up @@ -37,11 +38,11 @@ const handleEventsRequest = async (method, endpoint, data, params) => {
};

const addStream = async (webhookUrl) => {
const result = await handleApiRequest('post', 'create', { webhookUrl, network: 'evm' });
const result = await handleApiRequest('post', 'create', { webhookUrl });
renderResult(result);
};
const updateStream = async (webhookUrl, id) => {
const result = await handleApiRequest('patch', `update/${id}`, { webhookUrl, network: 'evm' });
const result = await handleApiRequest('patch', `update/${id}`, { webhookUrl });
renderResult(result);
};
const getStreams = async () => {
Expand Down
4 changes: 2 additions & 2 deletions demos/moralis-stream/src/stream/streamController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export async function create(req: Request, res: Response, next: NextFunction) {
const { webhookUrl } = req.body;

const result = await addStream({
network: 'evm',
networkType: 'evm',
webhookUrl,
});

Expand All @@ -32,7 +32,7 @@ export async function update(req: Request, res: Response, next: NextFunction) {
const { id } = req.params;

const message = await updateStream(id, {
network: 'evm',
networkType: 'evm',
webhookUrl,
});

Expand Down
14 changes: 7 additions & 7 deletions demos/moralis-stream/src/stream/streamService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Moralis from 'moralis';

interface StreamOptions {
network: 'evm';
networkType: 'evm';
webhookUrl: string;
}

Expand All @@ -10,9 +10,9 @@ const TAG = 'transactions';
const CHAINIDS = ['0x3', '0x4'];

export async function addStream(options: StreamOptions) {
const { network, webhookUrl } = options;
const { networkType, webhookUrl } = options;
const result = await Moralis.Streams.add({
network,
networkType,
webhookUrl,
chains: CHAINIDS,
tag: TAG,
Expand All @@ -26,7 +26,7 @@ export async function addStream(options: StreamOptions) {
export async function getStreams() {
const result = await Moralis.Streams.getAll({
limit: 20,
network: 'evm',
networkType: 'evm',
});

return result.raw;
Expand All @@ -35,17 +35,17 @@ export async function getStreams() {
export async function deleteStream(id: string) {
const result = await Moralis.Streams.delete({
id,
network: 'evm',
networkType: 'evm',
});

return result.raw;
}

export async function updateStream(id: string, options: StreamOptions) {
const { network, webhookUrl } = options;
const { networkType, webhookUrl } = options;
const result = await Moralis.Streams.update({
id,
network,
networkType,
webhookUrl,
chains: CHAINIDS,
tag: TAG,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const Authentication = () => {

const handleAuth = async (connector?: Connector, disabled?: boolean) => {
if (disabled) {
// eslint-disable-next-line no-alert
alert('Setup it first in the Authentication.tsx');
return;
}
Expand All @@ -51,7 +52,7 @@ const Authentication = () => {

const { account, chain } = await connectAsync({ connector });

const userData = { address: account, chain: chain.id, network: 'evm' };
const userData = { address: account, chain: chain.id, networkType: 'evm' };

const { message } = await apiPost('/auth/request-message', userData);

Expand All @@ -62,7 +63,7 @@ const Authentication = () => {
// redirects to main page
push('/');
} catch (e) {
return;
// Do nothing
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const AuthenticateModal = ({ isOpen, onClose }: AuthenticateModalProps) =
const { message } = await Moralis.Cloud.run('requestMessage', {
address: account,
chain: parseInt(chainId, 16),
network: 'evm',
networkType: 'evm',
});

// Authenticate and login via parse
Expand Down
2 changes: 1 addition & 1 deletion demos/parse-server-migration/src/auth/MoralisEthAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function validateAuthData(authData: any) {
return Moralis.Auth.verify({
message: data,
signature,
network: 'evm',
networkType: 'evm',
})
.then((result) => {
const authenticated = result.toJSON();
Expand Down
14 changes: 11 additions & 3 deletions demos/parse-server-migration/src/auth/authService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Moralis from 'moralis';
export interface RequestMessage {
address: string;
chain: string;
network: string;
networkType: string;
}

const DOMAIN = 'defi.finance';
Expand All @@ -12,11 +12,19 @@ const URI = 'https://defi.finance';
const EXPIRATION_TIME = '2023-01-01T00:00:00.000Z';
const TIMEOUT = 15;

export async function requestMessage({ address, chain, network }: { address: string; chain: string; network: 'evm' }) {
export async function requestMessage({
address,
chain,
networkType,
}: {
address: string;
chain: string;
networkType: 'evm';
}) {
const result = await Moralis.Auth.requestMessage({
address,
chain,
network,
networkType,
domain: DOMAIN,
statement: STATEMENT,
uri: URI,
Expand Down
4 changes: 2 additions & 2 deletions demos/parse-server-migration/src/cloud/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import './generated/solApi';
import { requestMessage } from '../auth/authService';

Parse.Cloud.define('requestMessage', async ({ params }: any) => {
const { address, chain, network } = params;
const { address, chain, networkType } = params;

const message = await requestMessage({
address,
chain,
network,
networkType,
});

return { message };
Expand Down
8 changes: 4 additions & 4 deletions demos/parse-server/public/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ const handleApiPost = async (endpoint, params) => {
return result.data;
};

const requestMessage = (account, network, chain) =>
const requestMessage = (account, networkType, chain) =>
handleApiPost('request-message', {
address: account,
chain,
network,
networkType,
});

const verifyMessage = (message, signature, network) =>
const verifyMessage = (message, signature, networkType) =>
handleApiPost('sign-message', {
message,
signature,
network,
networkType,
});

const connectToMetamask = async () => {
Expand Down
4 changes: 2 additions & 2 deletions demos/parse-server/src/auth/authController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { NextFunction, Request, Response } from 'express';

export async function request(req: Request, res: Response, next: NextFunction) {
try {
const { address, chain, network } = req.body;
const { address, chain, networkType } = req.body;

const message = await requestMessage({
address,
chain,
network,
networkType,
});

res.status(200).json({ message });
Expand Down
28 changes: 18 additions & 10 deletions demos/parse-server/src/auth/authService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,34 @@ const TIMEOUT = 15;
export async function requestMessage({
address,
chain,
network,
networkType,
}: {
address: string;
chain?: string;
network: 'evm' | 'solana';
networkType: 'evm' | 'solana';
}) {
if (network === 'evm' && chain) {
return requestMessageEvm({ address, chain, network });
if (networkType === 'evm' && chain) {
return requestMessageEvm({ address, chain, networkType });
}
if (network === 'solana') {
return requestMessageSol({ address, network });
if (networkType === 'solana') {
return requestMessageSol({ address, networkType });
}
throw new Error('Invalid network');
}

async function requestMessageEvm({ address, chain, network }: { address: string; chain: string; network: 'evm' }) {
async function requestMessageEvm({
address,
chain,
networkType,
}: {
address: string;
chain: string;
networkType: 'evm';
}) {
const result = await Moralis.Auth.requestMessage({
address,
chain,
network,
networkType,
domain: DOMAIN,
statement: STATEMENT,
uri: URI,
Expand All @@ -56,10 +64,10 @@ async function requestMessageEvm({ address, chain, network }: { address: string;
return message;
}

async function requestMessageSol({ address, network }: { address: string; network: 'solana' }) {
async function requestMessageSol({ address, networkType }: { address: string; networkType: 'solana' }) {
const result = await Moralis.Auth.requestMessage({
address,
network,
networkType,
solNetwork: 'devnet',
domain: DOMAIN,
statement: STATEMENT,
Expand Down
11 changes: 6 additions & 5 deletions demos/supabase-auth/public/script.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* global document, window, axios, supabase, ethers */
const AUTH_API_URL = 'http://localhost:3000/api/auth';
const SUPABASE_URL = 'replace_me';
const SUPABASE_PUBLIC_ANON_KEY = 'replace_me';
Expand All @@ -24,19 +25,19 @@ let token;
const requestMessage = (account, chain) =>
handleApiPost('request-message', {
address: account,
chain: chain,
network: 'evm',
chain,
networkType: 'evm',
});

const verifyMessage = (message, signature) =>
handleApiPost('sign-message', {
message,
signature,
network: 'evm',
networkType: 'evm',
});

const getUser = async (token) => {
_supabase.auth.setAuth(token);
const getUser = async (providedToken) => {
_supabase.auth.setAuth(providedToken);
const { data } = await _supabase.from('users').select('*');
renderUser(data);
};
Expand Down
8 changes: 4 additions & 4 deletions demos/supabase-auth/src/auth/authController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { NextFunction, Request, Response } from 'express';

export async function request(req: Request, res: Response, next: NextFunction) {
try {
const { address, chain, network } = req.body;
const { address, chain, networkType } = req.body;

const message = await requestMessage({
address,
chain,
network,
networkType,
});

res.status(200).json({ message });
Expand All @@ -19,10 +19,10 @@ export async function request(req: Request, res: Response, next: NextFunction) {

export async function verify(req: Request, res: Response, next: NextFunction) {
try {
const { network, message, signature } = req.body;
const { networkType, message, signature } = req.body;

const user = await verifyMessage({
network,
networkType,
message,
signature,
});
Expand Down
Loading

0 comments on commit 555883f

Please sign in to comment.