@prosopo/account 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/dist/cjs/common/dist/array.cjs +8 -0
  2. package/dist/cjs/common/dist/asyncFactory.cjs +13 -0
  3. package/dist/cjs/common/dist/custom.cjs +1 -0
  4. package/dist/cjs/common/dist/error.cjs +92 -0
  5. package/dist/cjs/common/dist/hash.cjs +14 -0
  6. package/dist/cjs/common/dist/i18n.cjs +29 -0
  7. package/dist/cjs/common/dist/index.cjs +40 -0
  8. package/dist/cjs/common/dist/locales/en.json.cjs +191 -0
  9. package/dist/cjs/common/dist/logger.cjs +91 -0
  10. package/dist/cjs/common/dist/node/UrlConverter.cjs +215 -0
  11. package/dist/cjs/common/dist/node/i18nMiddleware.cjs +8 -0
  12. package/dist/cjs/common/dist/node/index.cjs +4 -0
  13. package/dist/cjs/common/dist/react/index.cjs +4 -0
  14. package/dist/cjs/common/dist/react/useTranslation.cjs +8 -0
  15. package/dist/cjs/common/dist/string.cjs +6 -0
  16. package/dist/cjs/common/dist/utils.cjs +38 -0
  17. package/dist/cjs/extension/Extension.cjs +5 -0
  18. package/dist/cjs/extension/ExtensionWeb2.cjs +104 -0
  19. package/dist/cjs/index.cjs +6 -0
  20. package/dist/cjs/util/dist/canvas.cjs +326 -0
  21. package/dist/cjs/util/dist/index.cjs +26 -0
  22. package/dist/cjs/util/dist/isMain.cjs +13 -0
  23. package/dist/cjs/util/dist/lodash.cjs +35 -0
  24. package/dist/cjs/util/dist/ofLen.cjs +526 -0
  25. package/dist/cjs/util/dist/url.cjs +9 -0
  26. package/dist/cjs/util/dist/util.cjs +153 -0
  27. package/dist/extension/Extension.d.ts +5 -0
  28. package/dist/extension/Extension.d.ts.map +1 -0
  29. package/dist/extension/Extension.js +3 -0
  30. package/dist/extension/Extension.js.map +1 -0
  31. package/dist/extension/ExtensionWeb2.d.ts +18 -0
  32. package/dist/extension/ExtensionWeb2.d.ts.map +1 -0
  33. package/dist/extension/ExtensionWeb2.js +100 -0
  34. package/dist/extension/ExtensionWeb2.js.map +1 -0
  35. package/dist/index.d.ts +3 -0
  36. package/dist/index.d.ts.map +1 -0
  37. package/dist/index.js +3 -0
  38. package/dist/index.js.map +1 -0
  39. package/package.json +58 -0
  40. package/vite.cjs.config.ts +6 -0
@@ -0,0 +1,215 @@
1
+ "use strict";
2
+ Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
3
+ require("../index.cjs");
4
+ const error = require("../error.cjs");
5
+ class UrlConverter {
6
+ constructor() {
7
+ this.symbols = [
8
+ "",
9
+ "0",
10
+ "1",
11
+ "2",
12
+ "3",
13
+ "4",
14
+ "5",
15
+ "6",
16
+ "7",
17
+ "8",
18
+ "9",
19
+ "a",
20
+ "b",
21
+ "c",
22
+ "d",
23
+ "e",
24
+ "f",
25
+ "g",
26
+ "h",
27
+ "i",
28
+ "j",
29
+ "k",
30
+ "l",
31
+ "m",
32
+ "n",
33
+ "o",
34
+ "p",
35
+ "q",
36
+ "r",
37
+ "s",
38
+ "t",
39
+ "u",
40
+ "v",
41
+ "w",
42
+ "x",
43
+ "y",
44
+ "z",
45
+ "/",
46
+ ".",
47
+ "-",
48
+ "_",
49
+ "%",
50
+ ":",
51
+ "http://",
52
+ "https://",
53
+ "www.",
54
+ ".com",
55
+ ".net",
56
+ ".org",
57
+ ".co.uk",
58
+ ".io"
59
+ ];
60
+ this.symbolNBits = 6;
61
+ this.byteNBits = 8;
62
+ this.symbolToNumMap = this.symbols.reduce((obj, symb, i, arr) => {
63
+ obj[symb] = i;
64
+ return obj;
65
+ }, {});
66
+ this.numToSymbolMap = this.symbols.reduce((obj, symb, i, arr) => {
67
+ obj[i] = symb;
68
+ return obj;
69
+ }, {});
70
+ this.longestSymbolLength = this.symbols.reduce((longest, symb) => {
71
+ return Math.max(longest, symb.length);
72
+ }, 0);
73
+ const maxSymbols = Math.pow(2, this.symbolNBits);
74
+ if (this.symbols.length > maxSymbols) {
75
+ throw new error.ProsopoError("DEVELOPER.GENERAL", {
76
+ context: {
77
+ error: `Cannot encode more than ${maxSymbols} symbols`,
78
+ context: "only built to encode 64 symbols. Need to adjust the encoding and decoding scheme for more symbols"
79
+ }
80
+ });
81
+ }
82
+ const symbols = this.getSymbols();
83
+ if (symbols.length !== new Set(symbols).size) {
84
+ throw new error.ProsopoError("DEVELOPER.GENERAL", { context: { error: "Symbols must be unique" } });
85
+ }
86
+ }
87
+ symbolToNum(symb) {
88
+ const num = this.symbolToNumNull(symb);
89
+ if (num === void 0) {
90
+ throw new error.ProsopoError("DEVELOPER.GENERAL", {
91
+ context: { error: `Could not find number for symbol '${symb}'` }
92
+ });
93
+ }
94
+ return num;
95
+ }
96
+ symbolToNumNull(symb) {
97
+ symb = symb.toLowerCase();
98
+ const num = this.symbolToNumMap[symb];
99
+ return num;
100
+ }
101
+ numToSymbol(num) {
102
+ const symb = this.numToSymbolNull(num);
103
+ if (symb === void 0) {
104
+ throw new error.ProsopoError("DEVELOPER.GENERAL", {
105
+ context: { error: `Could not find symbol for number ${num}` }
106
+ });
107
+ }
108
+ return symb;
109
+ }
110
+ numToSymbolNull(num) {
111
+ const symb = this.numToSymbolMap[num];
112
+ return symb;
113
+ }
114
+ encode(url) {
115
+ url = url.toLowerCase();
116
+ const nums = [];
117
+ const origUrl = url;
118
+ while (url.length > 0) {
119
+ let len = Math.min(url.length, this.longestSymbolLength);
120
+ let num = void 0;
121
+ while (num === void 0 && len > 0) {
122
+ const str = url.slice(0, len);
123
+ num = this.symbolToNumNull(str);
124
+ if (num === void 0) {
125
+ len--;
126
+ }
127
+ }
128
+ if (num === void 0) {
129
+ throw new error.ProsopoError("DEVELOPER.GENERAL", {
130
+ context: { error: `Could not find symbol at '${url}' of '${origUrl}'` }
131
+ });
132
+ }
133
+ nums.push(num);
134
+ url = url.slice(len);
135
+ }
136
+ const nBits = nums.length * this.symbolNBits;
137
+ const nBytes = Math.ceil(nBits / this.byteNBits);
138
+ const bytes = new Uint8Array(nBytes);
139
+ for (let bitCount = 0; bitCount < nBits; ) {
140
+ const numIndex = bitCount / this.symbolNBits | 0;
141
+ const num = nums[numIndex];
142
+ if (num === void 0) {
143
+ throw new error.ProsopoError("DEVELOPER.GENERAL", {
144
+ context: { error: `Could not find number at index ${numIndex} of '${nums}'` }
145
+ });
146
+ }
147
+ const byteIndex = bitCount / this.byteNBits | 0;
148
+ const usedBitsInByte = bitCount % this.byteNBits;
149
+ const unusedBitsInByte = this.byteNBits - usedBitsInByte;
150
+ const shift = this.symbolNBits - unusedBitsInByte;
151
+ if (shift < 0) {
152
+ const usedBitsInSymbol = bitCount % this.symbolNBits;
153
+ const unusedBitsInSymbol = this.symbolNBits - usedBitsInSymbol;
154
+ const max = Math.pow(2, unusedBitsInSymbol);
155
+ const remNum = num % max;
156
+ const shift2 = this.byteNBits - unusedBitsInSymbol;
157
+ const shiftedNum = remNum << shift2;
158
+ bytes[byteIndex] |= shiftedNum;
159
+ bitCount += unusedBitsInSymbol;
160
+ } else {
161
+ bytes[byteIndex] |= num >> shift;
162
+ bitCount += unusedBitsInByte;
163
+ }
164
+ }
165
+ return bytes;
166
+ }
167
+ decode(bytes) {
168
+ const arr = [];
169
+ const nBits = bytes.length * this.byteNBits;
170
+ let num = 0;
171
+ let nBitsNum = 0;
172
+ for (let bitCount = 0; bitCount < nBits; ) {
173
+ const byteIndex = bitCount / this.byteNBits | 0;
174
+ const byte = bytes[byteIndex];
175
+ if (byte === void 0) {
176
+ throw new error.ProsopoError("DEVELOPER.GENERAL", {
177
+ context: { error: `Could not find byte at index ${byteIndex} of '${bytes}'` }
178
+ });
179
+ }
180
+ const usedBitsInByte = bitCount % this.byteNBits;
181
+ const unusedBitsInByte = this.byteNBits - usedBitsInByte;
182
+ const unusedBitsInSymbol = this.symbolNBits - nBitsNum;
183
+ const consumeNBits = Math.min(unusedBitsInByte, unusedBitsInSymbol);
184
+ const slice = this.bitSlice(byte, usedBitsInByte, consumeNBits);
185
+ num = num << consumeNBits | slice;
186
+ nBitsNum += consumeNBits;
187
+ if (nBitsNum >= this.symbolNBits) {
188
+ const symbol = this.numToSymbol(num);
189
+ arr.push(symbol);
190
+ num = 0;
191
+ nBitsNum = 0;
192
+ }
193
+ bitCount += consumeNBits;
194
+ }
195
+ return arr.join("");
196
+ }
197
+ bitSlice(num, startBit, lenBit) {
198
+ const truncedLeft = this.bitTruncLeft(num, startBit);
199
+ const truncedLen = this.bitTruncRight(truncedLeft, Math.max(0, this.byteNBits - lenBit - startBit));
200
+ return truncedLen;
201
+ }
202
+ bitTruncRight(num, nBits) {
203
+ return num >> nBits;
204
+ }
205
+ bitTruncLeft(num, nBits) {
206
+ const threshNBits = this.byteNBits - nBits;
207
+ const thresh = Math.pow(2, threshNBits);
208
+ return thresh <= 0 ? 0 : num % thresh;
209
+ }
210
+ getSymbols() {
211
+ return this.symbols;
212
+ }
213
+ }
214
+ exports.UrlConverter = UrlConverter;
215
+ exports.default = UrlConverter;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ const i18nextHttpMiddleware = require("i18next-http-middleware");
3
+ require("../i18n.cjs");
4
+ const i18n = require("i18next");
5
+ function i18nMiddleware(options) {
6
+ return i18nextHttpMiddleware.handle(i18n, { ...options });
7
+ }
8
+ module.exports = i18nMiddleware;
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const i18nMiddleware = require("./i18nMiddleware.cjs");
4
+ exports.i18nMiddleware = i18nMiddleware;
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const useTranslation = require("./useTranslation.cjs");
4
+ exports.useTranslation = useTranslation;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ const reactI18next = require("react-i18next");
3
+ require("../i18n.cjs");
4
+ const i18n = require("i18next");
5
+ function useTranslation(options) {
6
+ return reactI18next.useTranslation("translation", { i18n, ...options });
7
+ }
8
+ module.exports = useTranslation;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ function capitaliseFirstLetter(s) {
4
+ return s.charAt(0).toUpperCase() + s.slice(1);
5
+ }
6
+ exports.capitaliseFirstLetter = capitaliseFirstLetter;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const error = require("./error.cjs");
4
+ const hex = require("@polkadot/util/hex");
5
+ const en = require("./locales/en.json.cjs");
6
+ function isClientSide() {
7
+ return !!(typeof window !== "undefined" && window.document && window.document.createElement);
8
+ }
9
+ function getLeafFieldPath(obj) {
10
+ if (typeof obj === "string") {
11
+ return [obj];
12
+ }
13
+ return Object.keys(obj).reduce((arr, key) => {
14
+ const value = obj[key];
15
+ if (value === void 0) {
16
+ throw new error.ProsopoError("DEVELOPER.KEY_ERROR", { context: { error: `Undefined value for key ${key}` } });
17
+ }
18
+ const children = getLeafFieldPath(value);
19
+ return arr.concat(children.map((child) => {
20
+ return `${key}.${child}`;
21
+ }));
22
+ }, []);
23
+ }
24
+ const translationKeys = getLeafFieldPath(en.default);
25
+ const trimProviderUrl = (url) => {
26
+ return hex.hexToString(url).replace(/\0/g, "");
27
+ };
28
+ function snakeToCamelCase(str) {
29
+ return str.replace(/([-_][a-z])/g, (group) => group.toUpperCase().replace("-", "").replace("_", ""));
30
+ }
31
+ function reverseHexString(str) {
32
+ return `0x${str.match(/.{1,2}/g)?.reverse().join("") || ""}`;
33
+ }
34
+ exports.isClientSide = isClientSide;
35
+ exports.reverseHexString = reverseHexString;
36
+ exports.snakeToCamelCase = snakeToCamelCase;
37
+ exports.translationKeys = translationKeys;
38
+ exports.trimProviderUrl = trimProviderUrl;
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ class Extension {
4
+ }
5
+ exports.Extension = Extension;
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const Api = require("@polkadot/api/promise/Api");
4
+ const Extension = require("./Extension.cjs");
5
+ const keyring = require("@polkadot/keyring");
6
+ require("../common/dist/index.cjs");
7
+ const ws = require("@polkadot/rpc-provider/ws");
8
+ const address = require("@polkadot/util-crypto/address");
9
+ const bip39 = require("@polkadot/util-crypto/mnemonic/bip39");
10
+ const fingerprintjs = require("@fingerprintjs/fingerprintjs");
11
+ require("../util/dist/index.cjs");
12
+ const string = require("@polkadot/util/string");
13
+ const u8a = require("@polkadot/util/u8a");
14
+ const Signer = require("@polkadot/extension-base/page/Signer");
15
+ const error = require("../common/dist/error.cjs");
16
+ const canvas = require("../util/dist/canvas.cjs");
17
+ const hash = require("../common/dist/hash.cjs");
18
+ class ExtensionWeb2 extends Extension.Extension {
19
+ constructor() {
20
+ super(...arguments);
21
+ this.getNetwork = (config) => {
22
+ const network = config.networks[config.defaultNetwork];
23
+ if (!network) {
24
+ throw new error.ProsopoEnvError("DEVELOPER.NETWORK_NOT_FOUND", {
25
+ context: { error: `No network found for environment ${config.defaultEnvironment}` }
26
+ });
27
+ }
28
+ return network;
29
+ };
30
+ }
31
+ async getAccount(config) {
32
+ const network = this.getNetwork(config);
33
+ const wsProvider = new ws.WsProvider(network.endpoint);
34
+ const account = await this.createAccount(wsProvider);
35
+ const extension = await this.createExtension(account);
36
+ return {
37
+ account,
38
+ extension
39
+ };
40
+ }
41
+ async createExtension(account) {
42
+ const signer = new Signer(async () => {
43
+ return;
44
+ });
45
+ signer.signRaw = async (payload) => {
46
+ const signature = account.keypair.sign(payload.data);
47
+ return {
48
+ id: 1,
49
+ // the id of the request to sign. This should be incremented each time and adjust the signature, but we're hacking around this. Hence the signature will always be the same given the same payload.
50
+ signature: u8a.u8aToHex(signature)
51
+ };
52
+ };
53
+ return {
54
+ accounts: {
55
+ get: async () => {
56
+ return [account];
57
+ },
58
+ subscribe: () => {
59
+ return () => {
60
+ return;
61
+ };
62
+ }
63
+ },
64
+ name: "procaptcha-web2",
65
+ version: "0.1.11",
66
+ signer
67
+ };
68
+ }
69
+ async createAccount(wsProvider) {
70
+ const params = {
71
+ area: { width: 300, height: 300 },
72
+ offsetParameter: 2001000001,
73
+ multiplier: 15e3,
74
+ fontSizeFactor: 1.5,
75
+ maxShadowBlur: 50,
76
+ numberOfRounds: 5,
77
+ seed: 42
78
+ };
79
+ const browserEntropy = await this.getFingerprint();
80
+ const canvasEntropy = canvas.picassoCanvas(params.numberOfRounds, params.seed, params);
81
+ const entropy = hash.hexHash([canvasEntropy, browserEntropy].join(""), 128).slice(2);
82
+ const u8Entropy = string.stringToU8a(entropy);
83
+ const mnemonic = bip39.entropyToMnemonic(u8Entropy);
84
+ const api = await Api.ApiPromise.create({ provider: wsProvider, initWasm: false });
85
+ const type = "ed25519";
86
+ const keyring$1 = new keyring.Keyring({ type, ss58Format: api.registry.chainSS58 });
87
+ const keypair = keyring$1.addFromMnemonic(mnemonic);
88
+ const address$1 = keypair.address.length === 42 ? keypair.address : address.encodeAddress(address.decodeAddress(keypair.address), api.registry.chainSS58);
89
+ return {
90
+ address: address$1,
91
+ type,
92
+ name: address$1,
93
+ keypair
94
+ };
95
+ }
96
+ async getFingerprint() {
97
+ const fpPromise = fingerprintjs.load();
98
+ const fp = await fpPromise;
99
+ const result = await fp.get();
100
+ const { screenFrame, ...componentsReduced } = result.components;
101
+ return fingerprintjs.hashComponents(componentsReduced);
102
+ }
103
+ }
104
+ exports.ExtensionWeb2 = ExtensionWeb2;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const Extension = require("./extension/Extension.cjs");
4
+ const ExtensionWeb2 = require("./extension/ExtensionWeb2.cjs");
5
+ exports.Extension = Extension.Extension;
6
+ exports.ExtensionWeb2 = ExtensionWeb2.ExtensionWeb2;