Gnosis-Safe

使用 ethAdapter 簽署 safeTransaction 時出現“TypeError:無法讀取未定義的屬性(讀取 ‘arrayify’)”

  • February 5, 2022

使用 Safe Core SDK 簽署簡單的乙太幣轉賬交易時出現以下錯誤。

const ethAdapter = new EthersAdapter({
 ethers: ether3,
 signer: ether3.getSigner(0),
})
const safeSdk = await Safe.create({ ethAdapter, safeAddress })
let transaction = {
 to: 'receiver_address',
 data: '0x',
 value: '1000000000000000000',
}
transaction.nonce = await safeService.getNextNonce(safeAddress)
const safeTransaction = await safeSdk.createTransaction(transaction)
await safeSdk.signTransaction(safeTransaction) // this is throwing error
TypeError: Cannot read properties of undefined (reading 'arrayify')
   at EthersAdapter.signMessage (EthersAdapter.js?298f:103:1)
   at generateSignature (index.js?1e12:55:1)
   at async Safe.signTransaction (Safe.js?3512:258:1)

無法理解為什麼會發生這種情況,因為以前我使用的是 web3Adapter 並且沒有給出任何此類錯誤。

編輯(2022 年 1 月 25 日):

import { ethers } from 'ethers'

const ether3 = new ethers.providers.Web3Provider(window.ethereum) // I am using different provider but it is similar to metamask
const ethAdapter = new EthersAdapter({
 ethers: ether3,
 signer: ether3.getSigner(0),
})
const safeSdk = await Safe.create({ ethAdapter, safeAddress })
let transaction = {
 to: 'receiver_address',
 data: '0x',
 value: '1000000000000000000',
}
transaction.nonce = await safeService.getNextNonce(safeAddress)
const safeTransaction = await safeSdk.createTransaction(transaction)
await safeSdk.signTransaction(safeTransaction) // this is throwing error

signMessage來自 EthersAdapter 類的方法:

signMessage(message) {
 const messageArray = __classPrivateFieldGet(this, _EthersAdapter_ethers, "f").utils.arrayify(message);
 return __classPrivateFieldGet(this, _EthersAdapter_signer, "f").signMessage(messageArray);
}

signMessage錯誤在類的方法中引發EthersAdapter

signMessage(message: string): Promise<string> {
 const messageArray = this.#ethers.utils.arrayify(message)
 return this.#signer.signMessage(messageArray)
}

wherethis.#ethers.utils似乎是未定義的。

請確保您的ether3對像是有效的,它應該像這樣導入:

import { ethers } from "ethers"

引發此錯誤是因為0x根據 ethers 不是有效的“arrayify”。

https://github.com/ethers-io/ethers.js/blob/3de1b815014b10d223a42e524fe9c25f9087293b/packages/bytes/src.ts/index.ts#L92

改變

let transaction = {
 to: 'receiver_address',
 data: '0x',
 value: '1000000000000000000',
}

let transaction = {
 to: 'receiver_address',
 data: '0x00',
 value: '1000000000000000000',
}

引用自:https://ethereum.stackexchange.com/questions/119669