@dgpub/prime-sdk
Version:
Etherspot Prime (Account Abstraction) SDK
93 lines (92 loc) • 3.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.WalletConnect2WalletProvider = void 0;
const common_1 = require("../../common");
const dynamic_wallet_provider_1 = require("./dynamic.wallet-provider");
class WalletConnect2WalletProvider extends dynamic_wallet_provider_1.DynamicWalletProvider {
constructor(provider) {
super('WalletConnect2');
this.provider = provider;
try {
const { accounts: [address], chainId, } = provider;
this.setAddress(address);
this.setNetworkName(chainId);
}
catch (err) {
}
this.updateSessionHandler = this.updateSessionHandler.bind(this);
provider.on('connect', this.updateSessionHandler);
provider.on('session_event', this.updateSessionHandler);
provider.on('disconnect', () => {
this.setAddress(null);
this.setNetworkName(null);
});
}
async signMessage(message) {
const response = await this.provider.signer.request({
method: 'personal_sign',
params: [(0, common_1.toHex)(message), this.address],
});
return typeof response === 'string' ? response : null;
}
async signTypedData(typedData, message, accountAddress) {
const domainSeparator = {
name: "EtherspotWallet",
version: "2.0.0",
chainId: this.provider.chainId,
verifyingContract: accountAddress
};
const signature = await this.provider.signer.request({
method: 'eth_signTypedData_v4',
params: [
this.address,
{
"types": {
"EIP712Domain": [
{
"name": "name",
"type": "string"
},
{
"name": "version",
"type": "string"
},
{
"name": "chainId",
"type": "uint256"
},
{
"name": "verifyingContract",
"type": "address"
}
],
"message": typedData
},
"primaryType": "message",
"domain": domainSeparator,
"message": message
}
]
});
return typeof signature === 'string' ? signature : null;
}
updateSessionHandler(error, payload) {
let address = null;
let chainId = null;
if (!error) {
try {
({
accounts: [address],
chainId,
} = payload.params[0]);
}
catch (err) {
address = null;
chainId = null;
}
}
this.setAddress(address);
this.setNetworkName(chainId);
}
}
exports.WalletConnect2WalletProvider = WalletConnect2WalletProvider;