safe-apps-sdk
Version:
SDK developed to integrate third-party apps with Safe app.
142 lines • 5.87 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Safe = void 0;
const ethers_1 = require("ethers");
const signatures_1 = require("./signatures");
const methods_1 = require("../communication/methods");
const constants_1 = require("../eth/constants");
const types_1 = require("../types");
const requirePermissions_1 = __importDefault(require("../decorators/requirePermissions"));
class Safe {
constructor(communicator) {
this.communicator = communicator;
}
async getChainInfo() {
const response = await this.communicator.send(methods_1.Methods.getChainInfo, undefined);
return response.data;
}
async getInfo() {
const response = await this.communicator.send(methods_1.Methods.getSafeInfo, undefined);
return response.data;
}
// There is a possibility that this method will change because we may add pagination to the endpoint
async experimental_getBalances({ currency = 'usd' } = {}) {
const response = await this.communicator.send(methods_1.Methods.getSafeBalances, {
currency,
});
return response.data;
}
async check1271Signature(messageHash, signature = '0x') {
const safeInfo = await this.getInfo();
const encodedIsValidSignatureCall = signatures_1.EIP_1271_INTERFACE.encodeFunctionData('isValidSignature', [
messageHash,
signature,
]);
const payload = {
call: constants_1.RPC_CALLS.eth_call,
params: [
{
to: safeInfo.safeAddress,
data: encodedIsValidSignatureCall,
},
'latest',
],
};
try {
const response = await this.communicator.send(methods_1.Methods.rpcCall, payload);
return response.data.slice(0, 10).toLowerCase() === signatures_1.MAGIC_VALUE;
}
catch (err) {
return false;
}
}
async check1271SignatureBytes(messageHash, signature = '0x') {
const safeInfo = await this.getInfo();
const msgBytes = ethers_1.ethers.utils.arrayify(messageHash);
const encodedIsValidSignatureCall = signatures_1.EIP_1271_BYTES_INTERFACE.encodeFunctionData('isValidSignature', [
msgBytes,
signature,
]);
const payload = {
call: constants_1.RPC_CALLS.eth_call,
params: [
{
to: safeInfo.safeAddress,
data: encodedIsValidSignatureCall,
},
'latest',
],
};
try {
const response = await this.communicator.send(methods_1.Methods.rpcCall, payload);
return response.data.slice(0, 10).toLowerCase() === signatures_1.MAGIC_VALUE_BYTES;
}
catch (err) {
return false;
}
}
calculateMessageHash(message) {
return ethers_1.ethers.utils.hashMessage(message);
}
calculateTypedMessageHash(typedMessage) {
return ethers_1.ethers.utils._TypedDataEncoder.hash(typedMessage.domain, typedMessage.types, typedMessage.message);
}
async getOffChainSignature(messageHash) {
const response = await this.communicator.send(methods_1.Methods.getOffChainSignature, messageHash);
return response.data;
}
async isMessageSigned(message, signature = '0x') {
let check;
if (typeof message === 'string') {
check = async () => {
const messageHash = this.calculateMessageHash(message);
const messageHashSigned = await this.isMessageHashSigned(messageHash, signature);
return messageHashSigned;
};
}
if ((0, types_1.isObjectEIP712TypedData)(message)) {
check = async () => {
const messageHash = this.calculateTypedMessageHash(message);
const messageHashSigned = await this.isMessageHashSigned(messageHash, signature);
return messageHashSigned;
};
}
if (check) {
const isValid = await check();
return isValid;
}
throw new Error('Invalid message type');
}
async isMessageHashSigned(messageHash, signature = '0x') {
const checks = [this.check1271Signature.bind(this), this.check1271SignatureBytes.bind(this)];
for (const check of checks) {
const isValid = await check(messageHash, signature);
if (isValid) {
return true;
}
}
return false;
}
async getEnvironmentInfo() {
const response = await this.communicator.send(methods_1.Methods.getEnvironmentInfo, undefined);
return response.data;
}
async requestAddressBook() {
const response = await this.communicator.send(methods_1.Methods.requestAddressBook, undefined);
return response.data;
}
}
__decorate([
(0, requirePermissions_1.default)()
], Safe.prototype, "requestAddressBook", null);
exports.Safe = Safe;
//# sourceMappingURL=index.js.map