@useorbis/db-sdk
Version:
Orbis' Typescript SDK for building open-data experiences.
90 lines (89 loc) • 3.05 kB
JavaScript
import { SupportedChains, } from "../types/index.js";
import { keccak_256 } from "@noble/hashes/sha3";
import { catchError } from "../util/tryit.js";
function uint8ToHex(uint8) {
return Array.from(uint8)
.map((x) => x.toString(16).padStart(2, "0"))
.join("");
}
class OrbisEVMProvider {
genericSignerId = "orbis-evm";
chain = SupportedChains.evm;
#provider;
constructor(provider) {
this.#provider = provider;
}
async connect() {
// Assume if we can fetch the address, we are connected
// Skips unnecessary request attempts with PKP provider
const [address, _] = await catchError(() => this.getAddress());
if (address) {
return;
}
if (typeof this.#provider.enable === "function") {
await this.#provider.enable();
return;
}
if (typeof this.#provider.request === "function") {
await this.#provider.request({
method: "eth_requestAccounts",
params: [],
});
}
}
#checksumAddress(_address) {
const address = _address.replace(/^0x/i, "").toLowerCase();
const hash = keccak_256(address);
const hex = uint8ToHex(hash);
if (!hex ||
hex ===
"0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470")
return _address;
let checksumAddress = "0x";
for (let i = 0; i < address.length; i += 1) {
if (parseInt(hex[i], 16) > 7) {
checksumAddress += address[i].toUpperCase();
}
else {
checksumAddress += address[i];
}
}
return checksumAddress;
}
async getAddress() {
if (typeof this.#provider.getAddress === "function") {
const address = await this.#provider.getAddress();
if (typeof address === "string") {
return this.#checksumAddress(address);
}
if (!address || !address.length) {
throw "No eth accounts found";
}
return this.#checksumAddress(address[0]);
}
const accounts = await this.#provider.request({
method: "eth_accounts",
});
if (!accounts.length) {
throw "No eth accounts found";
}
return this.#checksumAddress(accounts[0]);
}
async signMessage(message) {
if (typeof this.#provider.signMessage === "function") {
const signature = await this.#provider.signMessage(message);
if (typeof signature === "string") {
return signature;
}
return signature.signature;
}
const signature = await this.#provider.request({
method: "personal_sign",
params: [message, await this.getAddress()],
});
return signature;
}
}
export function normalizeEVMProvider({ provider, }) {
return new OrbisEVMProvider(provider);
}