-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwallet.js
68 lines (56 loc) · 2.19 KB
/
wallet.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import {
checkAddress,
decodeAddress,
encodeAddress,
createKeyMulti,
} from "https://cdn.jsdelivr.net/npm/@polkadot/util-crypto@13.1.1/+esm";
import { web3Accounts } from "https://cdn.jsdelivr.net/npm/@polkadot/extension-dapp@0.53.1/+esm";
export class AddressError extends Error {}
let cachedAccounts = null;
let cachedAccountsMs = null;
export const getAccounts = async (prefix, multisigCheck = false) => {
if (!cachedAccounts) cachedAccounts = await web3Accounts();
if (!multisigCheck) return cachedAccounts;
if (cachedAccountsMs) return cachedAccountsMs;
try {
const addresses = getMultisigAddress(prefix);
if (!addresses || !addresses[2]) return [];
const msSet = new Set(addresses[2].map((x) => encodeAddress(x, prefix)));
return (cachedAccountsMs = cachedAccounts.filter((x) => msSet.has(encodeAddress(x.address, prefix))));
} catch (e) {
console.error("Error getting acounts", e);
return [];
}
};
export function getMultisigAddress(prefix) {
const multisigThreshold = parseInt(document.getElementById("multisigThreshold")?.value || 1);
const senderAddress = document.getElementById("sender")?.value;
const multisigSignatories = document
.getElementById("multisigSignatories")
.value.split("\n")
.map((x) => x.trim())
.filter((x) => !!x)
.filter((x) => x !== senderAddress);
if (senderAddress) multisigSignatories.push(senderAddress);
if (multisigThreshold > multisigSignatories.length) {
throw new AddressError(`Multisig setup is invalid. Wrong threshold or bad signatories.`);
}
multisigSignatories.forEach((signatory) => {
const check = checkAddress(signatory, prefix);
if (!check[0]) {
throw new AddressError(`Signatory address "${signatory}" is invalid: ${check[1] || "unknown"}`);
}
});
const multisigAddress = encodeAddress(createKeyMulti(multisigSignatories, multisigThreshold), prefix);
return [multisigAddress, multisigThreshold, multisigSignatories];
}
function arrayEquality(a, b) {
if (a.length !== b.length) return false;
for (const i in a) {
if (a[i] !== b[i]) return false;
}
return true;
}
export function addressEquality(a, b) {
return arrayEquality(decodeAddress(a), decodeAddress(b));
}