This commit is contained in:
zerone
2024-10-18 12:24:14 +08:00
commit 336fa549ae
26 changed files with 1181 additions and 0 deletions
+63
View File
@@ -0,0 +1,63 @@
---
title: 认证
description: 了解 WealthMentor 如何在不同的开发模式和不同的提供者中处理认证。
icon: "lock"
---
# 开发模式下的认证
在开发模式下,您的应用程序使用简化版的认证以便于测试和调试。这通常不安全,不应在生产环境中使用。
在您的 `.env` 文件中,您可以使用 `NODE_ENV` 变量在开发环境和生产环境之间进行切换。当设置为 "development" 时,应用程序会在开发模式下运行。
要使应用程序使用不安全的简化版认证,请将您的 `.env` 文件中的 `NEXTAUTH_SECRET` 变量设置为一个简单的、不安全的值(例如 "changeme")。
## 访问提供者密钥
要启用不同类型的 OAuth 认证,您需要从各自的提供者那里获取密钥。以下是获取这些密钥的一般说明:
<Tabs>
<Tab title="Google">
在 Google Cloud Console 中创建一个项目。
转到 "凭据",点击 "创建凭据",然后选择 "OAuth 客户端 ID"。
配置您的 OAuth 同意屏幕和应用程序类型,然后点击 "创建"。
您的客户端 ID 和密钥将显示出来。
有关更详细的说明,请访问 Google 的官方 [OAuth 2.0 设置指南](https://developers.google.com/identity/protocols/oauth2)。
</Tab>
<Tab title="GitHub">
转到您的 GitHub 设置。
点击 "开发人员设置"。
点击 "OAuth 应用程序",然后点击 "New OAuth App"。
填写所需信息,然后点击 "注册应用程序"。
客户端 ID 和密钥将显示在下一页上。
您可以在 [GitHub OAuth 文档](https://docs.github.com/en/developers/apps/building-oauth-apps) 中找到更详细的说明。
</Tab>
<Tab title="Discord">
转到 Discord 开发者门户。
点击 "New Application"。
给您的应用程序命名,然后点击 "创建"。
转到您的应用程序设置中的 "OAuth2" 页面。
在 "重定向" 下,添加您的重定向 URI。
点击 "保存更改"。
客户端 ID 可以在 "General Information" 页面找到,密钥可以在 "OAuth2" 页面找到。
有关更详细的说明,请访问 [Discord OAuth2 文档](https://discord.com/developers/docs/topics/oauth2)。
</Tab>
</Tabs>
## 启用非本地认证
`.env` 文件包含几个提供者客户端 ID 和密钥的占位符。请使用从各自提供者那里获取的密钥替换这些占位符:
- 对于 Google,将 `GOOGLE_CLIENT_ID` 和 `GOOGLE_CLIENT_SECRET` 的 "\*\*\*" 占位符替换为从 Google 获取的客户端 ID 和密钥。
- 对于 GitHub,将 `GITHUB_CLIENT_ID` 和 `GITHUB_CLIENT_SECRET` 的 "\*\*\*" 占位符替换为从 GitHub 获取的客户端 ID 和密钥。
- 对于 Discord,将 `DISCORD_CLIENT_ID` 和 `DISCORD_CLIENT_SECRET` 的 "\*\*\*" 占位符替换为从 Discord 获取的客户端 ID 和密钥。
一旦进行了这些更改,您的应用程序将设置为使用这些提供者的 OAuth 认证。要从本地认证切换到 OAuth 认证,请将您的 `.env` 文件中的 `NEXTAUTH_URL` 变量更改为您的应用程序托管的 URL。这不能是 localhost。
+23
View File
@@ -0,0 +1,23 @@
---
title: eth_accounts
description: 返回BUZZUP拥有的地址列表
---
---
### 参数
- 该方法不接受任何参数
---
### 返回值
- <div>AddressList<span style={{marginLeft:10,color:'#707070'}}>array</span></div>
---
### Request
```js
async function getAccounts() {
const accounts = await window.ethereum.request({ method: 'eth_accounts' });
console.log('当前账户:', accounts[0]);
}
```
+23
View File
@@ -0,0 +1,23 @@
---
title: eth_chainId
description: 获取BUZZUP当前的chainId
---
---
### 参数
- 该方法不接受任何参数
---
### 返回值
- <div>ChainId<span style={{marginLeft:10,color:'#707070'}}>hex encoded bytes</span></div>
---
### Request
```js
async function getAccounts() {
const chainId = await window.ethereum.request({ method: 'eth_chainId' });
console.log(chainId);
}
```
+31
View File
@@ -0,0 +1,31 @@
---
title: eth_requestAccounts
description: 连接钱包
---
---
### 参数
- 该方法不接受任何参数
---
### 返回值
- <div>AddressList<span style={{marginLeft:10,color:'#707070'}}>array</span></div>
---
### Request
```js
async function connectWallet() {
if (window.ethereum) {
try {
const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' });
console.log('钱包连接成功',accounts);
} catch (error) {
console.error('用户拒绝了连接请求', error);
}
} else {
console.error('请安装 MetaMask 钱包');
}
}
```
+41
View File
@@ -0,0 +1,41 @@
---
title: eth_sendTransaction
description: 创建新的钱包确认,以便从用户帐户进行以太坊交易。此方法要求用户首先授予与其帐户交互的权限,因此请确保先调用eth_requestAccounts
---
---
### 参数
- from: 发送交易的地址。
- to: -(创建新合约时可选)将交易定向到的地址。
- value: 表示与此交易一起发送的值的整数。
- data: 合约的编译代码或调用的方法签名和编码参数的哈希。
- gas: (可选) 表示为交易执行提供的燃料的整数。 它将返回未使用的燃料。
---
### 返回值
- 已发送交易的交易哈希。
- TransactionHash 字符串
- 32 hex encoded bytes
---
### Request
```js
const eth_sendTransaction = () => {
try {
const hash = await window.ethereum.request({
"method": "eth_sendTransaction",
"params": [{
to: "0x4B0897b0513FdBeEc7C469D9aF4fA6C0752aBea7",
from: "0xDeaDbeefdEAdbeefdEadbEEFdeadbeefDEADbEEF",
gas: "0x76c0",
value: "0x8ac7230489e80000",
data: "0x",
gasPrice: "0x4a817c800"
}],
});
} catch (error) {
console.log('发送交易失败:',error);
}
}
```
+71
View File
@@ -0,0 +1,71 @@
---
title: eth_signTypedData_v4
description: eth_signTypedData_v4 是以太坊 JSON-RPC API 的一个方法,用于执行 EIP-712 的签名。这个方法允许 dApp 安全地请求用户对一个结构化数据的签名,以确保用户确认该数据的内容。
---
---
### 参数 TypedData
- <div>domain <span style={{color:'#707070',fontSize:14}}>object</span></div>
- <div>message <span style={{color:'#707070',fontSize:14}}>object</span></div>
- <div>primaryType <span style={{color:'#707070',fontSize:14}}>string</span></div>
- <div>types <span style={{color:'#707070',fontSize:14}}>string</span></div>
---
### 返回值
- Signature string
- hex encoded bytes
### Request
```javascript
const signTypedData = async () => {
const address = "0xYourEthereumAddress";
const msgParams = {
domain: {
chainId: 97, // Binance Smart Chain 测试网
name: "Ether Mail",
verifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
version: "1", // 版本应为字符串
},
message: {
from: {
name: 'Alice',
wallet: "0x58F401144bbd45031b9937Ec93A8b2aac8bB8972" // 确保地址为小写
},
to: {
name: 'Bob',
wallet: '0x58F401144bbd45031b9937Ec93A8b2aac8bB8972' // 确保地址为小写
},
contents: 'Hello, Bob!'
},
primaryType: "Mail",
types: {
EIP712Domain: [
{ name: 'name', type: 'string' },
{ name: 'version', type: 'string' },
{ name: 'chainId', type: 'uint256' },
{ name: 'verifyingContract', type: 'address' }
],
Person: [
{ name: 'name', type: 'string' },
{ name: 'wallet', type: 'address' }
],
Mail: [
{ name: 'from', type: 'Person' },
{ name: 'to', type: 'Person' },
{ name: 'contents', type: 'string' }
]
},
};
try {
const signature = await window.ethereum.request({
method: "eth_signTypedData_v4",
params: [address, JSON.stringify(msgParams)],
});
console.log("签名结果:", signature);
} catch (error) {
console.log("签名失败:", error);
}
};
+29
View File
@@ -0,0 +1,29 @@
---
title: personal_sign
description: 连接钱包并且签名,personal_sign- 使用此方法可以最轻松地请求不需要在链上有效处理的人性化签名。此方法要求用户首先授予与其帐户交互的权限,因此请确保先调用eth_requestAccounts
---
---
### 参数
- message: (必填) 签名数据
- address: (必填) 地址
---
### 返回值
- <div>Signature<span style={{marginLeft:10,color:'#707070'}}>hex encoded bytes</span></div>
---
```js
async function personal_sign() {
try {
const signature = await window.ethereum.request({
method: 'personal_sign',
params: [message, address],
});
console.log('签名结果:', signature);
} catch (error) {
console.log('签名失败:', signature);
}
}
```