Skip to Content
👋 嘿,欢迎使用uCNY电子现金! 了解详情
开发文档rootwallet-api.js主要方法

主要方法

getAddress

获取与扩展程序关联的钱包地址。

请求

此函数不需要任何参数。

响应

响应是一个Promise,解析为一个包含typeresult属性的对象。

  • type: "response" | "reject"
  • result:
    • address: 钱包的经典地址。
type: "response"; result: { address: string; }

type: "reject"; result: undefined;

错误处理

如果发生错误,将抛出异常。

示例

import { getAddress } from "@xhbmygod/rootwallet-api"; getAddress().then((response) => { console.log(response.result?.address); });

以下是一个React Web应用的示例:

import { isInstalled, getAddress } from "@xhbmygod/rootwallet-api"; function App() { const handleConnect = () => { isInstalled().then((response) => { if (response.result.isInstalled) { getAddress().then((response) => { console.log(`您的地址: ${response.result?.address}`); }); } }); }; return ( <div className="App"> <button onClick={handleConnect}>点击我!</button> </div> ); } export default App;

getNetwork

获取与扩展程序关联的网络信息。

请求

此函数不需要任何参数。

响应

响应是一个Promise,解析为一个包含typeresult属性的对象。

  • type: "response" | "reject"
  • result:
    • chain: 链名称,例如XRPL
    • network: 网络名称。
    • websocket: WebSocket URL。

网络名称返回:

  • Mainnet 如果用户连接到主网
  • Testnet 如果用户连接到测试网
  • Devnet 如果用户连接到开发网
type: "response"; result: { chain: string; network: string; websocket: string; }

type: "reject"; result: undefined;

错误处理

如果发生错误,将抛出异常。

示例

import { getNetwork } from "@xhbmygod/rootwallet-api"; getNetwork().then((response) => { console.log(response.result?.network); });

以下是一个React Web应用的示例:

import { isInstalled, getNetwork } from "@xhbmygod/rootwallet-api"; function App() { const handleConnect = () => { isInstalled().then((response) => { if (response.result.isInstalled) { getNetwork().then((response) => { console.log(`您的网络: ${response.result?.network}`); }); } }); }; return ( <div className="App"> <button onClick={handleConnect}>点击我!</button> </div> ); } export default App;

getNFT

获取与钱包关联的NFTs。

请求

可选 - 此函数需要一个可选的payload参数,其属性由GetNFTRequest定义。

  • limit: 返回的NFTs的最大数量。
  • marker: 来自之前分页响应的值。用于从之前响应中断的地方继续获取数据。
interface GetNFTRequest { // 限制要检索的NFTokens数量。 limit?: number; // 来自之前分页响应的值。用于从之前响应中断的地方继续获取数据。 marker?: unknown; }

响应

响应是一个Promise,解析为一个包含typeresult属性的对象。

  • type: "response" | "reject"
  • result:
    • account_nfts: AccountNFToken[] - 与钱包关联的NFTs数组。
    • marker: 用于后续请求的标记值。
type: "response"; result: { account_nfts: AccountNFToken[]; marker: unknown; }
interface AccountNFToken { Flags: number; Issuer: string; NFTokenID: string; NFTokenTaxon: number; URI?: string; nft_serial: number; }

type: "reject"; result: undefined;

错误处理

如果发生错误,将抛出异常。

示例

import { getNFT } from "@xhbmygod/rootwallet-api"; getNFT({ limit: 10 }).then((response) => { console.log(response.result?.account_nfts); });

以下是一个实现示例:

import { isInstalled, getNFT } from "@xhbmygod/rootwallet-api"; function App() { const handleNFTs = () => { isInstalled().then((response) => { if (response.result.isInstalled) { getNFT().then((result) => { console.log("您的NFTs: ", result.result?.account_nfts); }); } }); }; return ( <div className="App"> <button onClick={handleNFTs}>显示我的NFTs!</button> </div> ); } export default App;

getPublicKey

获取与钱包关联的公钥。

请求

此函数不需要任何参数。

响应

响应是一个Promise,解析为一个包含typeresult属性的对象。

  • type: "response" | "reject"
  • result:
    • address: 钱包的经典地址。
    • publicKey: 钱包的公钥。
type: "response"; result: { address: string; publicKey: string; }

type: "reject"; result: undefined;

错误处理

如果发生错误,将抛出异常。

示例

import { getPublicKey } from "@xhbmygod/rootwallet-api"; getPublicKey().then((response) => { console.log(`${response.result?.address} - ${response.result?.publicKey}`); });

以下是一个React Web应用的示例:

import { isInstalled, getPublicKey } from "@xhbmygod/rootwallet-api"; function App() { const handleConnect = () => { isInstalled().then((response) => { if (response.result.isInstalled) { getPublicKey().then((response) => { console.log( `${response.result?.address} - ${response.result?.publicKey}` ); }); } }); }; return ( <div className="App"> <button onClick={handleConnect}>点击我!</button> </div> ); } export default App;

isInstalled

检查用户的浏览器中是否安装了GemWallet扩展。

:::tip 我们强烈建议在使用其他方法之前,先检查用户是否安装了GemWallet。 :::

请求

此函数不需要任何参数。

响应

result: { isInstalled: boolean; }
  • isInstalled: true 如果用户安装了GemWallet扩展,否则为false

示例

import { isInstalled } from "@xhbmygod/rootwallet-api"; isInstalled().then((response) => { console.log(response.result.isInstalled); });

以下是一个React Web应用的示例:

import { isInstalled } from "@xhbmygod/rootwallet-api"; function App() { const handleConnect = () => { isInstalled().then((response) => { if (!response.result.isInstalled) { console.log("GemWallet未安装"); } else { console.log("GemWallet已安装"); } }); }; return ( <div className="App"> <button onClick={handleConnect}>点击我!</button> </div> ); } export default App;

sendPayment

通过扩展程序发起支付交易。

请求

必填 - 该函数接受一个payload对象作为输入参数,其属性由SendPaymentRequest定义。

  • BaseTransactionRequest中的所有字段。
  • amount: 要支付的金额,格式如下:
    • 表示要支付的CNY数量的字符串,以drops为单位。
    • 一个对象,其中’value’是表示要支付的代币数量的字符串。
    • 有关金额格式的更多技术细节,请参见这里
  • destination: 接收支付的账户的唯一地址。
  • destinationTag: 附加到交易的目标标签。
  • flags: 设置在交易上的标志。
export interface SendPaymentRequest extends BaseTransactionRequest { // 要支付的金额,格式如下: // - 表示要支付的CNY数量的字符串,以drops为单位。 // - 一个对象,其中'value'是表示要支付的代币数量的字符串。 amount: Amount; // 接收支付的账户的唯一地址 destination: string; // 附加到交易的目标标签 destinationTag?: number; // 设置在交易上的标志 flags?: PaymentFlags; }
type Amount = { currency: string; issuer: string; value: string; } | string;

有关金额格式的更多详细信息,请参见这里

interface Memo { memo: { memoType?: string; memoData?: string; memoFormat?: string; }; }

有关memos的更多技术细节,请参见这里

type PaymentFlags = { tfNoDirectRipple?: boolean; tfPartialPayment?: boolean; tfLimitQuality?: boolean; } | number;

有关标志的更多详细信息,请参见这里

响应

响应是一个Promise,解析为一个包含typeresult属性的对象。

  • type: "response" | "reject"
  • result:
    • hash: 交易的哈希值。
type: "response"; result: { hash: string; }

type: "reject"; result: undefined;

错误处理

如果发生错误,将抛出异常。

示例

import { sendPayment } from "@xhbmygod/rootwallet-api"; const payload = { amount: "1000000", // 以drops为单位(1 CNY) destination: "rLWQskMM8EoPxaLsmuQxE5rYeP4uX7dhym", memos: [ { memo: { memoType: "4465736372697074696f6e", memoData: "54657374206d656d6f", }, }, ], destinationTag: 12, fee: "199", flags: { tfNoDirectRipple: false, tfPartialPayment: false, tfLimitQuality: false, }, }; sendPayment(payload).then((response) => { console.log(response.result?.hash); });

以下是一个CNY支付的React Web应用示例:

import { isInstalled, sendPayment } from "@xhbmygod/rootwallet-api"; function App() { const handleConnect = () => { isInstalled().then((response) => { if (response.result.isInstalled) { const payment = { amount: "1000000", // 以drops为单位(1 CNY) destination: "rLWQskMM8EoPxaLsmuQxE5rYeP4uX7dhym", }; sendPayment(payment).then((response) => { console.log("交易哈希: ", response.result?.hash); }); } }); }; return ( <div className="App"> <button onClick={handleConnect}>点击我!</button> </div> ); } export default App;

以下是一个ETH支付的React Web应用示例:

import { isInstalled, sendPayment } from "@xhbmygod/rootwallet-api"; function App() { const handleConnect = () => { isInstalled().then((response) => { if (response.result.isInstalled) { const payment = { amount: { currency: "ETH", value: "0.01", // 以货币为单位 issuer: "rnm76Qgz4G9G4gZBJVuXVvkbt7gVD7szey", }, destination: "rLWQskMM8EoPxaLsmuQxE5rYeP4uX7dhym", }; sendPayment(payment).then((trHash) => { console.log("交易哈希: ", trHash); }); } }); }; return ( <div className="App"> <button onClick={handleConnect}>点击我!</button> </div> ); } export default App;

setAccount

通过扩展程序设置账户。

请求

必填 - 该函数接受一个类型为SetAccountRequest的payload作为输入参数。

  • BaseTransactionRequest中的所有字段。
  • flags: 设置在交易上的标志。
  • clearFlag: 要为此账户禁用的标志的唯一标识符。
  • domain: 拥有此账户的域,作为表示小写ASCII域的十六进制字符串。
    • 长度不能超过256字节。
  • emailHash: 一个任意的128位值。
    • 通常,客户端将其视为电子邮件地址的md5哈希,用于显示Gravatar图像。
  • messageKey: 用于向此账户发送加密消息的公钥。
    • 要设置密钥,它必须正好为33字节,第一个字节表示密钥类型:0x02或0x03表示secp256k1密钥,0xED表示Ed25519密钥。
    • 要删除密钥,请使用空值。
  • NFTokenMinter: 可以为您铸造NFTokens的另一个账户。
  • setFlag: 要为此账户启用的整数标志。
  • transferRate: 当用户转移此账户的代币时收取的费用,表示为单位的十亿分之一。
    • 不能超过2000000000或小于1000000000,特殊情况0表示不收费。
  • tickSize: 用于涉及此地址发行的货币的报价的刻度大小。
    • 这些报价的汇率四舍五入到此有效数字。
    • 有效值为3到15(含),或0表示禁用。
interface SetAccountRequest extends BaseTransactionRequest { flags?: SetAccountFlags; // 要为此账户禁用的标志的唯一标识符。 clearFlag?: number; // 拥有此账户的域,作为表示小写ASCII域的十六进制字符串。 // 长度不能超过256字节。 domain?: string; // 一个任意的128位值。通常,客户端将其视为电子邮件地址的md5哈希,用于显示Gravatar图像。 emailHash?: string; // 用于向此账户发送加密消息的公钥。要设置密钥,它必须正好为33字节,第一个字节表示密钥类型:0x02或0x03表示secp256k1密钥,0xED表示Ed25519密钥。 // 要删除密钥,请使用空值。 messageKey?: string; // 可以为您铸造NFTokens的另一个账户。 NFTokenMinter?: string; // 要为此账户启用的整数标志。 setFlag?: AccountSetAsfFlags; // 当用户转移此账户的代币时收取的费用,表示为单位的十亿分之一。 // 不能超过2000000000或小于1000000000,特殊情况0表示不收费。 transferRate?: number; // 用于涉及此地址发行的货币的报价的刻度大小。 // 这些报价的汇率四舍五入到此有效数字。 // 有效值为3到15(含),或0表示禁用。 tickSize?: number; }
type AccountSetFlagsInterface = { tfRequireDestTag?: boolean; tfOptionalDestTag?: boolean; tfRequireAuth?: boolean; tfOptionalAuth?: boolean; tfDisallowXRP?: boolean; tfAllowXRP?: boolean; } | number;
enum AccountSetAsfFlags { asfRequireDest = 1, asfRequireAuth = 2, asfDisallowXRP = 3, asfDisableMaster = 4, asfAccountTxnID = 5, asfNoFreeze = 6, asfGlobalFreeze = 7, asfDefaultRipple = 8, asfDepositAuth = 9, asfAuthorizedNFTokenMinter = 10, asfDisallowIncomingNFTokenOffer = 12, asfDisallowIncomingCheck = 13, asfDisallowIncomingPayChan = 14, asfDisallowIncomingTrustline = 15 }

响应

响应是一个Promise,解析为一个包含typeresult属性的对象。

  • type: "response" | "reject"
  • result:
    • hash: 交易的哈希值。
type: "response"; result: { hash: string; }

type: "reject"; result: undefined;

错误处理

如果发生错误,将抛出异常。

示例

import { setAccount } from "@xhbmygod/rootwallet-api"; const payload = { emailHash: "1D1382344586ECFF844DACFF698C2EFB", fee: "199", flags: { tfAllowXRP: true }, memos: [ { memo: { memoType: "4465736372697074696f6e", memoData: "54657374206d656d6f" } } ] }; setAccount(payload).then((response) => { console.log("交易哈希: ", response.result?.hash); });

以下是一个React Web应用的示例:

import { isInstalled, setAccount } from "@xhbmygod/rootwallet-api"; function App() { const handleSetAccount = () => { isInstalled().then((response) => { if (response.result.isInstalled) { const payload = { emailHash: "1D1382344586ECFF844DACFF698C2EFB", fee: "199", flags: { tfAllowXRP: true }, memos: [ { memo: { memoType: "4465736372697074696f6e", memoData: "54657374206d656d6f" } } ] }; setAccount(payload).then((response) => { console.log("交易哈希: ", response.result?.hash); }); } }); }; return ( <div className="App"> <button onClick={handleSetAccount}>设置账户</button> </div> ); } export default App;

setRegularKey

通过扩展程序设置常规密钥。

请求

必填 - 该函数接受一个类型为SetRegularKeyRequest的payload作为输入参数。

  • BaseTransactionRequest中的所有字段。
  • regularKey (可选): 一个base-58编码的地址,表示要分配给账户的常规密钥对。如果省略,则从账户中删除任何现有的常规密钥对。不得与地址的主密钥对匹配。
interface SetRegularKeyRequest extends BaseTransactionRequest { // 一个base-58编码的地址,表示要分配给账户的常规密钥对。如果省略,则从账户中删除任何现有的常规密钥对。 // 不得与地址的主密钥对匹配。 regularKey?: string; }

响应

响应是一个Promise,解析为一个包含typeresult属性的对象。

  • type: "response" | "reject"
  • result:
    • hash: 交易的哈希值。
type: "response"; result: { hash: string; }

type: "reject"; result: undefined;

错误处理

如果发生错误,将抛出异常。

示例

import { setRegularKey } from "@xhbmygod/rootwallet-api"; const payload = { regularKey: "rNvFCZXpDtGeQ3bVas95wGLN6N2stGmA9o", memos: [ { memo: { memoType: "4465736372697074696f6e", memoData: "54657374206d656d6f" } } ] }; setRegularKey(payload).then((response) => { console.log("交易哈希: ", response.result?.hash); });

以下是一个React Web应用的示例:

import { isInstalled, setRegularKey } from "@xhbmygod/rootwallet-api"; function App() { const handleSetRegularKey = () => { isInstalled().then((response) => { if (response.result.isInstalled) { const payload = { regularKey: "rNvFCZXpDtGeQ3bVas95wGLN6N2stGmA9o", memos: [ { memo: { memoType: "4465736372697074696f6e", memoData: "54657374206d656d6f" } } ] }; setRegularKey(payload).then((response) => { console.log("交易哈希: ", response.result?.hash); }); } }); }; return ( <div className="App"> <button onClick={handleSetRegularKey}>设置账户</button> </div> ); } export default App;

setTrustLine

在钱包中添加或编辑信任线。

请求

必填 - 该函数接受一个类型为SetTrustlineRequest的payload作为输入参数。

  • BaseTransactionRequest中的所有字段。
  • limitAmount: 可以交换到信任线的最大货币金额。
    • 有关金额格式的更多技术细节,请参见这里
  • flags: 设置在交易上的标志。
interface SetTrustlineRequest extends BaseTransactionRequest { // 可以交换到信任线的最大货币金额 limitAmount: IssuedCurrencyAmount; // 设置在交易上的标志 flags?: TrustSetFlags; }
interface IssuedCurrencyAmount { currency: string; issuer: string; value: string; }

有关货币金额格式的更多技术细节,请参见这里

interface Memo { memo: { memoType?: string; memoData?: string; memoFormat?: string; }; }

有关memos的更多技术细节,请参见这里

type TrustSetFlags = { tfSetfAuth?: boolean; tfSetNoRipple?: boolean; tfClearNoRipple?: boolean; tfSetFreeze?: boolean; tfClearFreeze?: boolean; } | number;

有关标志的更多详细信息,请参见这里

响应

响应是一个Promise,解析为一个包含typeresult属性的对象。

  • type: "response" | "reject"
  • result:
    • hash: 交易的哈希值。
type: "response"; result: { hash: string; }

type: "reject"; result: undefined;

错误处理

如果发生错误,将抛出异常。

示例

import { setTrustline } from "@xhbmygod/rootwallet-api"; const trustline = { limitAmount: { currency: "ETH", issuer: "rnm76Qgz4G9G4gZBJVuXVvkbt7gVD7szey", value: "10000000", }, memos: [ { memo: { memoType: "4465736372697074696f6e", memoData: "54657374206d656d6f", }, }, ], fee: "199", flags: { tfClearFreeze: false, tfClearNoRipple: false, tfSetFreeze: true, tfSetNoRipple: true, tfSetfAuth: false, }, }; setTrustline(trustline).then((response) => { console.log(response.result?.hash); });

以下是一个React Web应用的示例:

import { isInstalled, addTrustline } from "@xhbmygod/rootwallet-api"; function App() { const handleConnect = () => { isInstalled().then((response) => { if (response.result.isInstalled) { const transaction = { limitAmount: { currency: "ETH", issuer: "rnm76Qgz4G9G4gZBJVuXVvkbt7gVD7szey", value: "10000000", }, }; addTrustline(transaction).then((response) => { console.log("交易哈希: ", response.result?.hash); }); } }); }; return ( <div className="App"> <button onClick={handleConnect}>点击我!</button> </div> ); } export default App;

signMessage

使用用户钱包的私钥对消息进行签名。

请求

该函数接受一个消息字符串作为输入参数,以及一个可选的布尔标志,用于确定消息是否为十六进制编码(默认:false)。 当您需要对已经十六进制编码的消息进行签名时,请将此标志设置为true

// 普通字符串消息 signMessage('Hello World!'); // 十六进制编码的消息 signMessage('48656c6c6f20576f726c6421', true);

响应

响应是一个Promise,解析为一个包含typeresult属性的对象。

  • type: "response" | "reject"
  • result:
    • signedMessage: 签名后的消息。
type: "response"; result: { signedMessage: string; }

type: "reject"; result: undefined;

错误处理

如果发生错误,将抛出异常。

示例

import { signMessage } from "@xhbmygod/rootwallet-api"; const message = "Hello, World!"; signMessage(message).then((response) => { console.log(response.result?.signedMessage); });

以下是一个React Web应用的示例:

import { isInstalled, signMessage } from "@xhbmygod/rootwallet-api"; function App() { const handleConnect = () => { isInstalled().then((response) => { if (response.result.isInstalled) { signMessage("The message I want to get signed").then((response) => { console.log("Signed message: ", response.result?.signedMessage); }); } }); }; return ( <div className="App"> <button onClick={handleConnect}>Click me!</button> </div> ); } export default App;
最后更新于: