@etherplay/connect-core 0.0.0
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.
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +77 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +43 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +55 -0
- package/src/index.ts +86 -0
- package/src/types.ts +39 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { HDKey } from '@scure/bip32';
|
|
2
|
+
export declare function originKeyMessage(orig: string): string;
|
|
3
|
+
export declare function localKeyMessage(): string;
|
|
4
|
+
export declare function originPublicKeyPublicationMessage(orig: string, publicKey: `0x${string}`): string;
|
|
5
|
+
export declare function fromEntropyKeyToMnemonic(entropyKey: `0x${string}`): string;
|
|
6
|
+
export declare function fromSignatureToKey(signature: `0x${string}`): `0x${string}`;
|
|
7
|
+
export declare function fromMnemonicToHDKey(mnemonic: string, index: number): HDKey;
|
|
8
|
+
export declare function strip0x(hex: string): string;
|
|
9
|
+
export declare function add0x(hex: string): string;
|
|
10
|
+
export declare function astr(str: unknown): void;
|
|
11
|
+
export declare function parse(address: string): {
|
|
12
|
+
hasPrefix: boolean;
|
|
13
|
+
data: string;
|
|
14
|
+
};
|
|
15
|
+
export declare function addChecksum(nonChecksummedAddress: string): string;
|
|
16
|
+
export declare function fromPublicKey(key: string | Uint8Array): string;
|
|
17
|
+
export declare function fromPrivateKey(key: string | Uint8Array): string;
|
|
18
|
+
export type * from './types.js';
|
|
19
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAC,KAAK,EAAC,MAAM,cAAc,CAAC;AAInC,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAErD;AACD,wBAAgB,eAAe,IAAI,MAAM,CAExC;AACD,wBAAgB,iCAAiC,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,KAAK,MAAM,EAAE,GAAG,MAAM,CAEhG;AAED,wBAAgB,wBAAwB,CAAC,UAAU,EAAE,KAAK,MAAM,EAAE,GAAG,MAAM,CAE1E;AAED,wBAAgB,kBAAkB,CAAC,SAAS,EAAE,KAAK,MAAM,EAAE,GAAG,KAAK,MAAM,EAAE,CAG1E;AAED,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,KAAK,CAI1E;AAMD,wBAAgB,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAE3C;AACD,wBAAgB,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAEzC;AAED,wBAAgB,IAAI,CAAC,GAAG,EAAE,OAAO,QAEhC;AAGD,wBAAgB,KAAK,CAAC,OAAO,EAAE,MAAM;;;EAUpC;AAED,wBAAgB,WAAW,CAAC,qBAAqB,EAAE,MAAM,GAAG,MAAM,CAUjE;AAED,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,GAAG,MAAM,CAK9D;AAED,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,GAAG,MAAM,CAG/D;AAGD,mBAAmB,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { entropyToMnemonic, mnemonicToSeedSync } from '@scure/bip39';
|
|
2
|
+
import { hexToBytes, bytesToHex } from '@noble/hashes/utils';
|
|
3
|
+
import { wordlist } from '@scure/bip39/wordlists/english';
|
|
4
|
+
import { HDKey } from '@scure/bip32';
|
|
5
|
+
import { keccak_256 } from '@noble/hashes/sha3';
|
|
6
|
+
import { secp256k1 } from '@noble/curves/secp256k1';
|
|
7
|
+
export function originKeyMessage(orig) {
|
|
8
|
+
return `Origin: ${orig}\n\nIMPORTANT: Only sign on trusted websites.\n\nThis grants access to your private session account.\n\nVerify before proceeding.`;
|
|
9
|
+
}
|
|
10
|
+
export function localKeyMessage() {
|
|
11
|
+
return 'DO NOT ACCEPT THIS SIGNATURE REQUEST! This used by Etherplay Wallet to generate your seed phrase.';
|
|
12
|
+
}
|
|
13
|
+
export function originPublicKeyPublicationMessage(orig, publicKey) {
|
|
14
|
+
return `Origin: ${orig}\n\nIMPORTANT: Only sign on trusted websites.\n\nThis authorizes the following Public Key to represent your account:\n\n${publicKey}\n\nOthers can use this key to write encrypted messages to you securely.`;
|
|
15
|
+
}
|
|
16
|
+
export function fromEntropyKeyToMnemonic(entropyKey) {
|
|
17
|
+
return entropyToMnemonic(hexToBytes(entropyKey.slice(2)), wordlist);
|
|
18
|
+
}
|
|
19
|
+
export function fromSignatureToKey(signature) {
|
|
20
|
+
const hash = keccak_256(hexToBytes(signature.slice(2)));
|
|
21
|
+
return `0x${bytesToHex(hash)}`;
|
|
22
|
+
}
|
|
23
|
+
export function fromMnemonicToHDKey(mnemonic, index) {
|
|
24
|
+
const seed = mnemonicToSeedSync(mnemonic);
|
|
25
|
+
const hd = HDKey.fromMasterSeed(seed);
|
|
26
|
+
return hd.derive(`m/44'/60'/0'/0/${index}`);
|
|
27
|
+
}
|
|
28
|
+
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
29
|
+
// TAKEN FROM https://github.com/paulmillr/micro-eth-signer/
|
|
30
|
+
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
31
|
+
const ethHexStartRe = /^0[xX]/;
|
|
32
|
+
export function strip0x(hex) {
|
|
33
|
+
return hex.replace(ethHexStartRe, '');
|
|
34
|
+
}
|
|
35
|
+
export function add0x(hex) {
|
|
36
|
+
return ethHexStartRe.test(hex) ? hex : `0x${hex}`;
|
|
37
|
+
}
|
|
38
|
+
export function astr(str) {
|
|
39
|
+
if (typeof str !== 'string')
|
|
40
|
+
throw new Error('string expected');
|
|
41
|
+
}
|
|
42
|
+
const RE = /^(0[xX])?([0-9a-fA-F]{40})?$/;
|
|
43
|
+
export function parse(address) {
|
|
44
|
+
astr(address);
|
|
45
|
+
const res = address.match(RE) || [];
|
|
46
|
+
const hasPrefix = res[1] != null;
|
|
47
|
+
const data = res[2];
|
|
48
|
+
if (!data) {
|
|
49
|
+
const len = hasPrefix ? 42 : 40;
|
|
50
|
+
throw new Error(`address must be ${len}-char hex, got ${address.length}-char ${address}`);
|
|
51
|
+
}
|
|
52
|
+
return { hasPrefix, data };
|
|
53
|
+
}
|
|
54
|
+
export function addChecksum(nonChecksummedAddress) {
|
|
55
|
+
const low = parse(nonChecksummedAddress).data.toLowerCase();
|
|
56
|
+
const hash = bytesToHex(keccak_256(low));
|
|
57
|
+
let checksummed = '';
|
|
58
|
+
for (let i = 0; i < low.length; i++) {
|
|
59
|
+
const hi = Number.parseInt(hash[i], 16);
|
|
60
|
+
const li = low[i];
|
|
61
|
+
checksummed += hi <= 7 ? li : li.toUpperCase(); // if char is 9-f, upcase it
|
|
62
|
+
}
|
|
63
|
+
return add0x(checksummed);
|
|
64
|
+
}
|
|
65
|
+
export function fromPublicKey(key) {
|
|
66
|
+
if (!key)
|
|
67
|
+
throw new Error('invalid public key: ' + key);
|
|
68
|
+
const pub65b = secp256k1.ProjectivePoint.fromHex(key).toRawBytes(false);
|
|
69
|
+
const hashed = keccak_256(pub65b.subarray(1, 65));
|
|
70
|
+
return addChecksum(bytesToHex(hashed).slice(24)); // slice 24..64
|
|
71
|
+
}
|
|
72
|
+
export function fromPrivateKey(key) {
|
|
73
|
+
if (typeof key === 'string')
|
|
74
|
+
key = strip0x(key);
|
|
75
|
+
return fromPublicKey(secp256k1.getPublicKey(key, false));
|
|
76
|
+
}
|
|
77
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"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,43 @@
|
|
|
1
|
+
export type MnemonicMechanism<T extends number | undefined> = {
|
|
2
|
+
type: 'mnemonic';
|
|
3
|
+
mnemonic: string;
|
|
4
|
+
index: T;
|
|
5
|
+
};
|
|
6
|
+
export type OauthMechanism = {
|
|
7
|
+
type: 'oauth';
|
|
8
|
+
provider: {
|
|
9
|
+
id: 'google' | 'facebook';
|
|
10
|
+
} | {
|
|
11
|
+
id: 'auth0';
|
|
12
|
+
connection: string;
|
|
13
|
+
};
|
|
14
|
+
} & ({
|
|
15
|
+
usePopup: true;
|
|
16
|
+
} | {
|
|
17
|
+
usePopup: false;
|
|
18
|
+
});
|
|
19
|
+
export type EmailMechanism<T extends string | undefined> = {
|
|
20
|
+
type: 'email';
|
|
21
|
+
email: T;
|
|
22
|
+
mode: 'otp';
|
|
23
|
+
};
|
|
24
|
+
export type AlchemyMechanism = EmailMechanism<string | undefined> | OauthMechanism | MnemonicMechanism<number | undefined>;
|
|
25
|
+
export type OriginAccount = {
|
|
26
|
+
address: `0x${string}`;
|
|
27
|
+
signer: {
|
|
28
|
+
origin: string;
|
|
29
|
+
address: `0x${string}`;
|
|
30
|
+
publicKey: `0x${string}`;
|
|
31
|
+
privateKey: `0x${string}`;
|
|
32
|
+
mnemonicKey: `0x${string}`;
|
|
33
|
+
};
|
|
34
|
+
metadata: {
|
|
35
|
+
email?: string;
|
|
36
|
+
};
|
|
37
|
+
mechanismUsed: AlchemyMechanism | {
|
|
38
|
+
type: string;
|
|
39
|
+
};
|
|
40
|
+
savedPublicKeyPublicationSignature?: `0x${string}`;
|
|
41
|
+
accountType: string;
|
|
42
|
+
};
|
|
43
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,iBAAiB,CAAC,CAAC,SAAS,MAAM,GAAG,SAAS,IAAI;IAC7D,IAAI,EAAE,UAAU,CAAC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dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@etherplay/connect-core",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "core functionality for @etherplay/connect and other",
|
|
5
|
+
"homepage": "https://github.com/wighawag/etherplay-connect#readme",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/wighawag/etherplay-connect.git",
|
|
9
|
+
"directory": "packages/etherplay-connect-utils"
|
|
10
|
+
},
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/wighawag/etherplay-connect/issues"
|
|
13
|
+
},
|
|
14
|
+
"author": "Ronan Sandford",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"type": "module",
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"default": "./dist/index.js"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"dist",
|
|
28
|
+
"src"
|
|
29
|
+
],
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@noble/curves": "^1.8.1",
|
|
32
|
+
"@noble/hashes": "^1.7.1",
|
|
33
|
+
"@noble/secp256k1": "^2.2.3",
|
|
34
|
+
"@scure/bip32": "^1.6.2",
|
|
35
|
+
"@scure/bip39": "^1.5.4"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@vitest/coverage-v8": "^4.0.18",
|
|
39
|
+
"as-soon": "^0.0.11",
|
|
40
|
+
"eip-1193": "^0.6.3",
|
|
41
|
+
"ldenv": "^0.3.12",
|
|
42
|
+
"prettier": "^3.5.3",
|
|
43
|
+
"typescript": "^5.8.3",
|
|
44
|
+
"vitest": "^4.0.18"
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"build": "tsc --project tsconfig.json",
|
|
48
|
+
"dev": "as-soon -w src pnpm build",
|
|
49
|
+
"format:check": "prettier --check .",
|
|
50
|
+
"format": "prettier --write .",
|
|
51
|
+
"test:watch": "vitest",
|
|
52
|
+
"test": "vitest run",
|
|
53
|
+
"test:coverage": "vitest run --coverage"
|
|
54
|
+
}
|
|
55
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import {entropyToMnemonic, mnemonicToSeedSync} from '@scure/bip39';
|
|
2
|
+
import {hexToBytes, bytesToHex} from '@noble/hashes/utils';
|
|
3
|
+
import {wordlist} from '@scure/bip39/wordlists/english';
|
|
4
|
+
import {HDKey} from '@scure/bip32';
|
|
5
|
+
import {keccak_256} from '@noble/hashes/sha3';
|
|
6
|
+
import {secp256k1} from '@noble/curves/secp256k1';
|
|
7
|
+
|
|
8
|
+
export function originKeyMessage(orig: string): string {
|
|
9
|
+
return `Origin: ${orig}\n\nIMPORTANT: Only sign on trusted websites.\n\nThis grants access to your private session account.\n\nVerify before proceeding.`;
|
|
10
|
+
}
|
|
11
|
+
export function localKeyMessage(): string {
|
|
12
|
+
return 'DO NOT ACCEPT THIS SIGNATURE REQUEST! This used by Etherplay Wallet to generate your seed phrase.';
|
|
13
|
+
}
|
|
14
|
+
export function originPublicKeyPublicationMessage(orig: string, publicKey: `0x${string}`): string {
|
|
15
|
+
return `Origin: ${orig}\n\nIMPORTANT: Only sign on trusted websites.\n\nThis authorizes the following Public Key to represent your account:\n\n${publicKey}\n\nOthers can use this key to write encrypted messages to you securely.`;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function fromEntropyKeyToMnemonic(entropyKey: `0x${string}`): string {
|
|
19
|
+
return entropyToMnemonic(hexToBytes(entropyKey.slice(2)), wordlist);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function fromSignatureToKey(signature: `0x${string}`): `0x${string}` {
|
|
23
|
+
const hash = keccak_256(hexToBytes(signature.slice(2)));
|
|
24
|
+
return `0x${bytesToHex(hash)}`;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function fromMnemonicToHDKey(mnemonic: string, index: number): HDKey {
|
|
28
|
+
const seed = mnemonicToSeedSync(mnemonic);
|
|
29
|
+
const hd = HDKey.fromMasterSeed(seed);
|
|
30
|
+
return hd.derive(`m/44'/60'/0'/0/${index}`);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
34
|
+
// TAKEN FROM https://github.com/paulmillr/micro-eth-signer/
|
|
35
|
+
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
36
|
+
const ethHexStartRe = /^0[xX]/;
|
|
37
|
+
export function strip0x(hex: string): string {
|
|
38
|
+
return hex.replace(ethHexStartRe, '');
|
|
39
|
+
}
|
|
40
|
+
export function add0x(hex: string): string {
|
|
41
|
+
return ethHexStartRe.test(hex) ? hex : `0x${hex}`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function astr(str: unknown) {
|
|
45
|
+
if (typeof str !== 'string') throw new Error('string expected');
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const RE = /^(0[xX])?([0-9a-fA-F]{40})?$/;
|
|
49
|
+
export function parse(address: string) {
|
|
50
|
+
astr(address);
|
|
51
|
+
const res = address.match(RE) || [];
|
|
52
|
+
const hasPrefix = res[1] != null;
|
|
53
|
+
const data = res[2];
|
|
54
|
+
if (!data) {
|
|
55
|
+
const len = hasPrefix ? 42 : 40;
|
|
56
|
+
throw new Error(`address must be ${len}-char hex, got ${address.length}-char ${address}`);
|
|
57
|
+
}
|
|
58
|
+
return {hasPrefix, data};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function addChecksum(nonChecksummedAddress: string): string {
|
|
62
|
+
const low = parse(nonChecksummedAddress).data.toLowerCase();
|
|
63
|
+
const hash = bytesToHex(keccak_256(low));
|
|
64
|
+
let checksummed = '';
|
|
65
|
+
for (let i = 0; i < low.length; i++) {
|
|
66
|
+
const hi = Number.parseInt(hash[i], 16);
|
|
67
|
+
const li = low[i];
|
|
68
|
+
checksummed += hi <= 7 ? li : li.toUpperCase(); // if char is 9-f, upcase it
|
|
69
|
+
}
|
|
70
|
+
return add0x(checksummed);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function fromPublicKey(key: string | Uint8Array): string {
|
|
74
|
+
if (!key) throw new Error('invalid public key: ' + key);
|
|
75
|
+
const pub65b = secp256k1.ProjectivePoint.fromHex(key).toRawBytes(false);
|
|
76
|
+
const hashed = keccak_256(pub65b.subarray(1, 65));
|
|
77
|
+
return addChecksum(bytesToHex(hashed).slice(24)); // slice 24..64
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function fromPrivateKey(key: string | Uint8Array): string {
|
|
81
|
+
if (typeof key === 'string') key = strip0x(key);
|
|
82
|
+
return fromPublicKey(secp256k1.getPublicKey(key, false));
|
|
83
|
+
}
|
|
84
|
+
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
85
|
+
|
|
86
|
+
export type * from './types.js';
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export type MnemonicMechanism<T extends number | undefined> = {
|
|
2
|
+
type: 'mnemonic';
|
|
3
|
+
mnemonic: string;
|
|
4
|
+
index: T;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
export type OauthMechanism = {
|
|
8
|
+
type: 'oauth';
|
|
9
|
+
|
|
10
|
+
provider: {id: 'google' | 'facebook'} | {id: 'auth0'; connection: string};
|
|
11
|
+
} & ({usePopup: true} | {usePopup: false}); // redirection: { origin: string; requestID: string }
|
|
12
|
+
|
|
13
|
+
export type EmailMechanism<T extends string | undefined> = {
|
|
14
|
+
type: 'email';
|
|
15
|
+
email: T;
|
|
16
|
+
mode: 'otp';
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type AlchemyMechanism =
|
|
20
|
+
| EmailMechanism<string | undefined>
|
|
21
|
+
| OauthMechanism
|
|
22
|
+
| MnemonicMechanism<number | undefined>;
|
|
23
|
+
|
|
24
|
+
export type OriginAccount = {
|
|
25
|
+
address: `0x${string}`;
|
|
26
|
+
signer: {
|
|
27
|
+
origin: string;
|
|
28
|
+
address: `0x${string}`;
|
|
29
|
+
publicKey: `0x${string}`;
|
|
30
|
+
privateKey: `0x${string}`;
|
|
31
|
+
mnemonicKey: `0x${string}`;
|
|
32
|
+
};
|
|
33
|
+
metadata: {
|
|
34
|
+
email?: string;
|
|
35
|
+
};
|
|
36
|
+
mechanismUsed: AlchemyMechanism | {type: string};
|
|
37
|
+
savedPublicKeyPublicationSignature?: `0x${string}`;
|
|
38
|
+
accountType: string;
|
|
39
|
+
};
|