How to implement SIWE using web3js - web3js

Is there a way to implement the client part of SIWE (Sign In With Ethereum) using Web3.js instead of ethers.js?
In SIWE's example they do:
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
const signature = await signer.signMessage(message)
But I can't figure out how to do something similar in my dapp which is build using web3.js

ethers.js signer.signMessage() takes a binary message, prepends it with the standardized prefix, and then requests the signer to sign the message.
You can achieve the same with web3js eth.sign() method.
const signature = await web3.eth.sign(message, signerAddress);

Related

Thirdweb error: This action requires a connected wallet to sign the transaction. Please pass a valid signer to the SDK

So I have this code:
import { ThirdwebSDK } from "#thirdweb-dev/sdk";
import { ConnectWallet, useAddress } from "#thirdweb-dev/react";
export default function DonationPage() {
let address = useAddress()
async function sendCoins() {
try {
if (selectedCoin == 'eth') {
} else {
const sdk = new ThirdwebSDK("mainnet");
const contract = await sdk.getContract('ADDRESS_TO_USDC_CONTRACT');
await contract.erc20.transferFrom(address || '0x',CORPORATE_WALLET || '0x', donationAmount);
}
} catch (error: any) {
alert(error.message)
}
}
return (<>...</>)
}
So i'm using ConnectWallet button to get address and using TrustWallet for this, and trying to transfer tokens from 'address' to a corporate wallet, with a specified donation amount. However, I receive this error "This action requires a connected wallet to sign the transaction. Please pass a valid signer to the SDK.".
There seems to be no documentation on this online, and ChatGPT won't help either. Does someone know how to fix this?
Also, how to send Eth to CORPORATE_WALLET using Thirdweb?
There seems to be no documentation on this online
Well there is complete documentation on the SDK on ThirdWeb website.
Since you're performing transaction on the contract you cannot initialize the SDK without a private key or signer. Using neither returns read-only contracts
Because you're performing the operations in the frontend, fromSigner method is recommended. Access the user's wallet and call for the signer, use this to initialize the SDK instance with the .fromSigner() method.
fromSigner documentation
ThirdWeb official fromSigner example with React
Also linking documentation for private key method, just in case you wish to approach that way.

Retrieving API keys from a server using Next.js

API keys are supposed to be kept secret, I have an adapter that needs you to pass the API key, in a object. How can I retrieve the API key to pass it in securely on the client?
Something sorta like this
async function connectToWallet() {
const _walletConnect = new walletEVMWalletAdapter({
apiKey: "API_KEY_HERE",
chain: BlockchainTypes.POLYGON,
})
const address = await _walletConnect.connect()
if (address) {
setAccount(address)
}
}
Now I understand if I wanted to make a request, In Next.Js i can use the API folder to keep my keys secure, but here I need to pass in the API key, to first create the connection how can I do this securely.

Ethereum Donation Function defining by tiers of NFT's (Reactjs, Web3)

Hope you all having a great week!
I'm having a few issues with NFT's and already deployed a smart contract. Here's the problems I have at the moment and where I need some clarification because I was not able to find any info about it.
I'm doing the NFT's mint function, where the contact and ABI I'm taking from already deployed smart contract. The whole NFT function's purpose is for donation. There are only 5 NFT's released and depending on NFT's trier (index) the price will differ. Because we have an index from 1 to 5.
The question is, how I can access those tiers from Smart contact and assign different prices on them when the buyer chooses from 5 different NFT's.
Here's my code where I was able to access the Metamask and make the first mint, but It does not directly target the specific tiers (index).
As well, the connection with Metamask is already handled and working.
Any help will be appreciated, or any info where I can understand how I could go around making this Minting depending on tiers function. Thanks in advance!!!
const provider = new ethers.providers.Web3Provider(window.ethereum)
const signer = provider.getSigner();
const contract = new ethers.Contract(contractAddress, ContractAbi, signer);
const mintToken = async () => {
const connection = contract.connect(signer);
const addr = connection.address;
const result = await contract.donate(addr, {
value: ethers.utils.parseEther('0.05'),
});
await result.wait();
};

Alexa check if a new session was started by accessing session.new

This is the data that is being send by alexa to my skills backend. In the backend code I would like to test if the session is new or not and based on this information I would like to produce a different output. I try to access the session.new, but I don't know how and I could now find anything about it online so far.
const { attributesManager } = handlerInput;
const requestAttributes = attributesManager.getRequestAttributes();
requestAttributes.session.new
//this leads to the error "cannot read property new of undefined"
const { attributesManager } = handlerInput;
const requestAttributes = attributesManager.getSessionAttributes();
sessionAttributes.session.new
//this leads to the same error
I finally found out that this is not possible using the AttributesManager because this only allows access to request, session and permanent attributes. But session.new is not part of those. If you want to check if the Alexa session is new you have to use the so called RequestEnvelopeUtils.
Using those you can access if the session is new or not by using the following statement.
(Alexa.isNewSession(handlerInput.requestEnvelope))

Passing user credentials to Adal AquireTokenAsync

I am using Adal (Active Directory Authentication Library) Version 3.13.x. I am doing something like below to fetch the Access token
AuthResult = await AuthContext.AcquireTokenAsync(relyingUrl, ServiceConstants.CLIENTID, ServiceConstants.RETURNURI, param);
I need to pass the UserCredentials along as well, but right now I can only pass one parameter to the UserCredential() unlike in the Versions 2.x.x of Adal.
I also tried using UserPasswordCredential as suggested in this solution , but since I want to Fetch the token from a Mobile app, I only have access to .net Core and hence can not use UserPasswordCredential
I want to pass the username and password together while acquiring the token. Is there any way i can achieve this?
Any help would be appreciated.
EDIT
After trying out the solution from Fei Xue - MSFT, i get the following 503 error.
I want to pass the username and password together while acquiring the token. Is there any way i can achieve this?
In this scenario, we can perform the REST request directly instead of using the client library. Here is a sample for your reference:
Post:https://login.microsoftonline.com/{tenant}/oauth2/token
resource={resource}&client_id={clientId}&grant_type=password&username={userName}&password={password}
Update
string authority = "https://login.microsoftonline.com/{tenantid}";
string resrouce = "https://graph.windows.net";
string clientId = "";
string userName = "";
string password = "";
UserPasswordCredential userPasswordCredential = new UserPasswordCredential(userName, password);
AuthenticationContext authContext = new AuthenticationContext(authority);
var token = authContext.AcquireTokenAsync(resrouce, clientId, userPasswordCredential).Result.AccessToken;
Console.WriteLine(token);

Resources