@okxweb3/coin-kaia
Version:
An kaia SDK for building Web3 wallets and applications.
178 lines • 6.35 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AccountStore = exports.Accounts = void 0;
const ethers6_1 = require("ethers6");
const js_ext_core_1 = require("@kaiachain/js-ext-core");
const signer_1 = require("./signer");
function isSameAddress(a, b) {
return (0, ethers6_1.getAddress)(a) == (0, ethers6_1.getAddress)(b);
}
function isSamePrivateKey(a, b) {
return (0, ethers6_1.computeAddress)(a) == (0, ethers6_1.computeAddress)(b);
}
class Accounts {
constructor(list) {
this.wallets = [];
for (let i = 0; i < list.length; i++) {
if (list[i] instanceof signer_1.Wallet) {
this.wallets.push(list[i]);
}
else if (Array.isArray(list[i])) {
if (list[i].length == 1) {
this.add([list[i][0]]);
}
else if (list[i].length == 2) {
this.add([list[i][0], list[i][1]]);
}
else {
throw new Error('Input has to be the array of [address, privateKey] or [privateKey]');
}
}
else {
throw new Error('Input has to be Wallet, [address, privateKey], or [privateKey]');
}
}
}
async add(account) {
let addr;
let priv;
if (account.length == 1) {
const signingKey = new ethers6_1.SigningKey(account[0]);
addr = (0, ethers6_1.computeAddress)(signingKey.compressedPublicKey);
priv = account[0];
}
else if (account.length == 2 && account[1] != undefined) {
addr = account[0];
priv = account[1];
}
else {
throw new Error('Input has to be [address, privateKey] or [privateKey]');
}
for (let i = 0; i < this.wallets.length; i++) {
if (isSameAddress(await this.wallets[i].getAddress(), addr) &&
isSamePrivateKey(this.wallets[i].privateKey, priv)) {
return false;
}
}
this.wallets.push(new signer_1.Wallet(addr, priv));
return true;
}
async remove(account) {
let addr;
let priv;
if (account.length == 1) {
const signingKey = new ethers6_1.SigningKey(account[0]);
addr = (0, ethers6_1.computeAddress)(signingKey.compressedPublicKey);
priv = account[0];
}
else if (account.length == 2 && account[1] != undefined) {
addr = account[0];
priv = account[1];
}
else {
throw new Error('Input has to be [address, privateKey] or [privateKey]');
}
for (let i = 0; i < this.wallets.length; i++) {
if (isSameAddress(await this.wallets[i].getAddress(), addr) &&
isSamePrivateKey(this.wallets[i].privateKey, priv)) {
delete this.wallets[i];
this.wallets.splice(i, 1);
return true;
}
}
return false;
}
removeAll() {
if (this.wallets.length == 0) {
return;
}
for (let i = this.wallets.length - 1; i >= 0 && i < this.wallets.length; i--) {
delete this.wallets[i];
this.wallets.splice(i, 1);
}
}
accountByKey(privateKey) {
const ret = [];
for (let i = 0; i < this.wallets.length; i++) {
if (isSamePrivateKey(this.wallets[i].privateKey, privateKey)) {
ret.push(this.wallets[i]);
}
}
return ret;
}
async accountByAddress(address) {
const ret = [];
for (let i = 0; i < this.wallets.length; i++) {
if (isSameAddress(await this.wallets[i].getAddress(), address)) {
ret.push(this.wallets[i]);
}
}
return ret;
}
}
exports.Accounts = Accounts;
class AccountStore {
constructor() {
this.signableKeyList = [];
}
async updateSignableKeyList() {
let i;
for (i = 0; this.accounts != undefined && i < this.accounts.wallets.length; i++) {
let hashedKey = await this.accounts.wallets[i].getEtherAddress();
hashedKey = String(hashedKey).toLocaleLowerCase();
if (this.hasInSignableKeyList(hashedKey) == false) {
this.signableKeyList.push(hashedKey);
}
}
}
hasInSignableKeyList(address) {
const hashedKey = String(address).toLocaleLowerCase();
return this.signableKeyList.indexOf(hashedKey) != -1;
}
hasAccountInfos(address) {
let i;
for (i = 0; this.accountInfos != undefined && i < this.accountInfos.length; i++) {
if (isSameAddress(this.accountInfos[i].address, address)) {
return true;
}
}
return false;
}
getType(address) {
let i;
for (i = 0; this.accountInfos != undefined && i < this.accountInfos.length; i++) {
if (isSameAddress(this.accountInfos[i].address, address)) {
return this.accountInfos[i].key.type;
}
}
return null;
}
getAccountInfo(address) {
let i;
for (i = 0; this.accountInfos != undefined && i < this.accountInfos.length; i++) {
if (isSameAddress(this.accountInfos[i].address, address)) {
return this.accountInfos[i];
}
}
return null;
}
getAccountInfos() {
return this.accountInfos;
}
getPubkeyInfo(x, y) {
const zeroPadX = js_ext_core_1.HexStr.zeroPad(x, 32);
const zeroPadY = js_ext_core_1.HexStr.zeroPad(y, 32);
const stripedX = String(zeroPadX).substring(2);
const stripedY = String(zeroPadY).substring(2);
const compressedKey = ethers6_1.SigningKey.computePublicKey(js_ext_core_1.HexStr.concat('0x04' + stripedX + stripedY), true);
const hashedKey = (0, ethers6_1.computeAddress)(compressedKey);
const hasPrivateKey = this.hasInSignableKeyList(hashedKey);
return {
compressed: compressedKey,
hashed: hashedKey,
hasPrivateKey: hasPrivateKey,
};
}
}
exports.AccountStore = AccountStore;
//# sourceMappingURL=accountStore.js.map