Mina Learning - Auro wallet

  • longerd
  • 更新于 2天前
  • 阅读 122

Mina Auro 钱包 API 介绍.

注意: ts 中需要使用 (window as Any).mina

链接钱包

安装 Auro wallet 插件后, 会存在 window.mina 对象.

if (typeof window.mina !== 'undefined') {
  console.log('Auro Wallet is installed!');
}

获取账户余额

没有找到相关 auro wallet api. 可以使用 o1js:

/**
 * @return The balance associated to the given public key.
 */
function getBalance(publicKey: PublicKey, tokenId?: Field) {
  return activeInstance.getAccount(publicKey, tokenId).balance;
}

获取账户(钱包地址)

在浏览器 console 里输入 console.log(await window.mina.requestAccounts()); 或者运行下面代码:

import {ProviderError} from '@aurowallet/mina-provider';

const account:string[]|ProviderError = await window.mina.requestAccounts()
    .catch((err: any) => err);

console.log(account)

上述代码会弹出一个窗口确认. 若不想要弹出窗口, 运行下面代码:

let account = await window.mina?.getAccounts();

console.log(account);

账户事件

这个方法通常用于监听账户变化.

window.mina?.on("accountsChanged", (accounts: string[]) => {
    console.log(accounts);
});

网络

目前支持的 networkID 为:

  • mina:mainnet
  • mina:devnet
  • zeko:testnet
    
    import {ChainInfoArgs} from '@aurowallet/mina-provider';

const network: ChainInfoArgs = await window.mina?.requestNetwork() .catch((err: any) => err); console.log(network.networkID)


### 切换网络

在浏览器 console 里输入 `console.log(await window.mina.switchChain({networkID: 'mina:devnet'.trim()}));` 或者运行下面代码: 
```ts
import {ChainInfoArgs, ProviderError} from '@aurowallet/mina-provider';

const switchResult : ChainInfoArgs|ProviderError = await window.mina
    ?.switchChain({
        networkID: networkID.trim(),
    }).catch((err: any) => err);

传递其他的 networkID 会返回 {code: 20004, message: 'Not support chain.'} 错误.

添加网络

自定义的网络, 需要配置 graphQLURL.

import {ChainInfoArgs, ProviderError} from '@aurowallet/mina-provider';

const addInfo = {
  url: encodeURIComponent("graphQLURL"),
  name: "networkName",
}

const addResult: ChainInfoArgs|ProviderError = await window.mina?.addChain(addInfo)
  .catch((err: any) => err);

网络事件

此方法用于监听网络的变化. 当 Auro 钱包的链发生变化时, 事件将被触发.

window.mina?.on("chainChanged",(chainInfo: ChainInfoArgs) => {
  console.log(chainInfo);
});

交易

发送支付交易

import {SendTransactionResult, ProviderError} from '@aurowallet/mina-provider';

let data: SendTransactionResult|ProviderError = await window.mina
    ?.sendPayment({
        amount: amount,
        to: receiveAddress,
        fee: fee, // option
        memo: memo, // option
    })
    .catch((err: any) => err);

发送委托交易

import {SendTransactionResult, ProviderError} from '@aurowallet/mina-provider';

let data: SendTransactionResult|ProviderError = await window.mina
    ?.sendStakeDelegation({
        to: vaildatorAddress,
        fee: fee, // option
        memo: memo, // option
    }).catch((err: any) => err); 

发送 zkapp 交易

import {SendTransactionResult, ProviderError, SendTransactionArgs} from '@aurowallet/mina-provider';

const updateResult: SendZkTransactionResult| ProviderError= await window.mina?
    .sendTransaction({
      onlySign: onlySign, // only sign zkCommond, not broadcast.
      transaction: transactionJSON, // this is zk commond, create by zkApp.
      feePayer: { // option.
        fee: fee,
        memo: memo
      },
    });

console.log(updateResult);

签名

签名字符串消息

import {ProviderError, SignedData, SignMessageArgs} from '@aurowallet/mina-provider';

const content = `Click "Sign" to sign in. No password needed!

This request will not trigger a blockchain transaction or cost any gas fees.

I accept the Auro Test zkApp Terms of Service: ${window.location.href}

address: ${currentAccount}
iat: ${new Date().getTime()}`;

const signContent: SignMessageArgs = {
    message:content
}

const signResult: SignedData|ProviderError = await window.mina?
     .signMessage(signContent)
     .catch((err: any) => err);

console.log(signResult);

签名 json 消息

import {ProviderError, SignedData} from '@aurowallet/mina-provider';

type JsonMessageData  = {
    label:string
    value:string
}

type SignJsonMessageArgs = {
    readonly message: Array<JsonMessageData>
}

const msgParams = [
    { label: "Label:", value: "Sign Confirm" },
    {
      label: "Message:",
      value: "Click to sign in and accept the Terms of Service",
    },
    {
      label: "URI:",
      value: window.location.href,
    },
    {
      label: "networkID:",
      value: network.networkID,
    },
    {
      label: "Chain Name:",
      value: network.name,
    },
    {
      label: "Issued At:",
      value: new Date().getTime(),
    },
    {
      label: "Resources:",
      value: "https://docs.aurowallet.com/",
    },
    ];

const signResult: SignedData|ProviderError = await window.mina
    ?.signJsonMessage({
        message: msgParams
    })
    .catch((err: any) => err);

console.log(signResult);

验签

浏览器端

import {ProviderError} from '@aurowallet/mina-provider';

let verifyResult: boolean|ProviderError = await window.mina
    ?.verifyMessage(verifyMessageBody)
    .catch((err: any) => err);

console.log(verifyResult); // If the result is successful, it will return true.

服务器端

因为服务器端没有 auro 插件, 当然也没有 window.mina 对象, 所以要用其他方式验签.

我们可以使用 mina-signer 库来进行验签, 代码如下:

// type Network = 'mainnet' | 'testnet'
var Client = require("mina-signer");
var signerClient = new Client({ network: "mainnet" });

const publicKey = 'B62qj6z7oseWTr37SQTn53mF8ebHn45cmSfRC58Sy52wG6KcaPZNWjw'

const verifyMessage = `Click "Sign" to sign in. No password needed!

  This request will not trigger a blockchain transaction or cost any gas fees.

  I accept the Auro Test zkApp Terms of Service: https://test-zkapp.aurowallet.com

  address: B62qj6z7oseWTr37SQTn53mF8ebHn45cmSfRC58Sy52wG6KcaPZNWjw
  iat: 1701069403643`;

const signature = {
    field: '25087624681052481871246375076085075176624243458008290192358519021588472251513',
    scalar: '17285357755862735391605884635523872951699156489623612197745807470058903167470'
}

const verifyBody = {
    data: verifyMessage, // Signature content that needs to be verified.
    publicKey: publicKey, // Public key that needs to be verified.
    signature: signature, // Signature results that need to be verified.
};

// When verifyResult is true, the verification is successful.
const verifyResult = signerClient.verifyMessage(verifyBody);

Bug

不知道触发了什么, auro wallet api 发送的交易在 devnet 确认后交易记录在 auro wallet 里全部消失了.

image.png

minascan 上有完整交易记录:

image.png

mina-signer

todo

Ref

点赞 2
收藏 0
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

0 条评论

请先 登录 后评论
longerd
longerd
code longer