You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何创建Vec<u8>类型签名供智能合约调用?解决类型匹配报错

修复"invalid type: map, expected a sequence"错误的解决方案

错误根源分析

你遇到的错误是因为前端传递的参数类型和智能合约期望的类型不匹配:

  • 合约中_signature和_signer定义为Option<Vec<u8>>(字节数组的可选类型),但前端传递的Buffer对象会被序列化为JSON对象(map),而非合约期望的字节序列(数组);
  • memo参数合约定义为Option(应为Option<String>),你传递了字符串"None",但合约期望的是null(表示无值)或实际字符串。

具体修复步骤

  1. 将_signature转换为字节数组
    把Buffer转换成Uint8Array数组,这样JSON序列化后会是合约期望的序列格式:

    "_signature": Array.from(Buffer.from(signature.signature, 'hex')),
    
  2. 将_signer转换为UTF-8字节数组
    合约期望_signer是字节数组,需要把账户ID字符串编码为UTF-8字节数组:

    "_signer": Array.from(new TextEncoder().encode("tungleanh.testnet")),
    
  3. 修正memo参数的None表示
    合约中memo是Option<String>,要表示无值直接传null,而非字符串"None":

    "memo": null,
    
  4. (可选)确认其他Option参数的传递
    其他如_advisor、_amount_per_minute等Option类型参数,若要传递有效值直接传对应类型即可;若要表示无值,同样传null。

修复后的完整args代码段

args: {
    "receiver_id": "dev-1669022432132-58804567266197",
    "amount": "0",
    "memo": null,
    "msg": "create_room",
    "_advisor": "dev-1669018198583-29903555965521",
    "_amount_per_minute": "1",
    "_room_id": "1",
    "_minutes_lasts": 5,
    "_signature": Array.from(Buffer.from(signature.signature, 'hex')),
    "_signer": Array.from(new TextEncoder().encode("tungleanh.testnet")),
}

内容的提问来源于stack exchange,提问作者marethyu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 06:20:21