Skip to content

Instantly share code, notes, and snippets.

@PBillingsby
Created December 20, 2023 17:41
Show Gist options
  • Select an option

  • Save PBillingsby/6eff8a8a03fc106c6209c03de372f92d to your computer and use it in GitHub Desktop.

Select an option

Save PBillingsby/6eff8a8a03fc106c6209c03de372f92d to your computer and use it in GitHub Desktop.
web3.js v1 flow
// Send transaction
import { Magic } from 'magic-sdk';
import Web3 from 'web3';
const magic = new Magic('YOUR_PUBLISHABLE_API_KEY');
const web3 = new Web3(magic.rpcProvider);
// ⭐️ After user is successfully authenticated
// Get user's Ethereum public address
const fromAddress = (await web3.eth.getAccounts())[0];
const destination = '0xE0cef4417a772512E6C95cEf366403839b0D6D6D';
const amount = web3.utils.toWei(1); // Convert 1 ether to wei
// Submit transaction to the blockchain and wait for it to be mined
const receipt = await web3.eth.sendTransaction({
from: fromAddress,
to: destination,
value: amount,
});
// personal_sign
import { Magic } from 'magic-sdk';
import Web3 from 'web3';
const magic = new Magic('YOUR_PUBLISHABLE_API_KEY');
const web3 = new Web3(magic.rpcProvider);
// ⭐️ After user is successfully authenticated
// Get user's Ethereum public address
const fromAddress = (await web3.eth.getAccounts())[0];
const originalMessage = 'YOUR_MESSAGE';
const signedMessage = await web3.eth.personal.sign(originalMessage, fromAddress);
// sign data type v1
import { Magic } from 'magic-sdk';
import Web3 from 'web3';
const magic = new Magic('YOUR_PUBLISHABLE_API_KEY');
const web3 = new Web3(magic.rpcProvider);
// ⭐️ After user is successfully authenticated
// Get user's Ethereum public address
const fromAddress = (await web3.eth.getAccounts())[0];
const originalMessage = [
{
type: 'string',
name: 'fullName',
value: 'John Doe',
},
{
type: 'uint32',
name: 'userId',
value: '1234',
},
];
const params = [originalMessage, fromAddress];
const method = 'eth_signTypedData';
const signedMessage = await web3.currentProvider.sendAsync({
id: 1,
method,
params,
fromAddress,
});
// sign typed data v3
import { Magic } from 'magic-sdk';
import Web3 from 'web3';
const magic = new Magic('YOUR_PUBLISHABLE_API_KEY');
const web3 = new Web3(magic.rpcProvider);
// ⭐️ After user is successfully authenticated
// Get user's Ethereum public address
const fromAddress = (await web3.eth.getAccounts())[0];
const originalMessage = {
types: {
EIP712Domain: [
{
name: 'name',
type: 'string',
},
{
name: 'version',
type: 'string',
},
{
name: 'verifyingContract',
type: 'address',
},
],
Greeting: [
{
name: 'contents',
type: 'string',
},
],
},
primaryType: 'Greeting',
domain: {
name: 'Magic',
version: '1',
verifyingContract: '0xE0cef4417a772512E6C95cEf366403839b0D6D6D',
},
message: {
contents: 'Hello, from Magic!',
},
};
const params = [fromAddress, originalMessage];
const method = 'eth_signTypedData_v3';
const signedMessage = await web3.currentProvider.sendAsync({
id: 1,
method,
params,
fromAddress,
});
// sign typed data v4
/*
Sign Typed Data v4 adds support for
arrays and recursive data types.
Otherwise, it works the same as Sign Typed Data v3.
*/
import { Magic } from 'magic-sdk';
import Web3 from 'web3';
const magic = new Magic('YOUR_PUBLISHABLE_API_KEY');
const web3 = new Web3(magic.rpcProvider);
// ⭐️ After user is successfully authenticated
// Get user's Ethereum public address
const fromAddress = (await web3.eth.getAccounts())[0];
const originalMessage = {
types: {
EIP712Domain: [
{
name: 'name',
type: 'string',
},
{
name: 'version',
type: 'string',
},
{
name: 'verifyingContract',
type: 'address',
},
],
Greeting: [
{
name: 'contents',
type: 'string',
},
],
},
primaryType: 'Greeting',
domain: {
name: 'Magic',
version: '1',
verifyingContract: '0xE0cef4417a772512E6C95cEf366403839b0D6D6D',
},
message: {
contents: 'Hello, from Magic!',
},
};
const params = [fromAddress, originalMessage];
const method = 'eth_signTypedData_v4';
const signedMessage = await web3.currentProvider.sendAsync({
id: 1,
method,
params,
fromAddress,
});
// Get balance
import { Magic } from 'magic-sdk';
import Web3 from 'web3';
const magic = new Magic('YOUR_PUBLISHABLE_API_KEY');
const web3 = new Web3(magic.rpcProvider);
// Get user's Ethereum public address
const address = (await web3.eth.getAccounts())[0];
// Get user's balance in ether
const balance = web3.utils.fromWei(
await web3.eth.getBalance(address), // Balance is in wei
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment