@etherplay/connect-core 0.0.0 → 0.2.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/crypto.d.ts +7 -0
- package/dist/crypto.d.ts.map +1 -0
- package/dist/crypto.js +23 -0
- package/dist/crypto.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +52 -0
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +92 -6
- package/dist/types.d.ts.map +1 -1
- package/package.json +3 -2
- package/src/crypto.ts +42 -0
- package/src/index.ts +75 -0
- package/src/types.ts +130 -7
package/dist/crypto.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const bufToB64: (buf: ArrayBuffer | Uint8Array) => string;
|
|
2
|
+
export declare const b64ToBuf: (b64: string) => Uint8Array;
|
|
3
|
+
export declare function generateEcdhKeyPair(): Promise<CryptoKeyPair>;
|
|
4
|
+
export declare function exportPublicKeyB64(key: CryptoKey): Promise<string>;
|
|
5
|
+
export declare function importPublicKeyB64(b64: string): Promise<CryptoKey>;
|
|
6
|
+
export declare function deriveAesKey(privateKey: CryptoKey, otherPublicKey: CryptoKey, usage: KeyUsage[]): Promise<CryptoKey>;
|
|
7
|
+
//# sourceMappingURL=crypto.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["../src/crypto.ts"],"names":[],"mappings":"AAMA,eAAO,MAAM,QAAQ,GAAI,KAAK,WAAW,GAAG,UAAU,KAAG,MACQ,CAAC;AAElE,eAAO,MAAM,QAAQ,GAAI,KAAK,MAAM,KAAG,UAAgE,CAAC;AAExG,wBAAsB,mBAAmB,IAAI,OAAO,CAAC,aAAa,CAAC,CAMlE;AAED,wBAAsB,kBAAkB,CAAC,GAAG,EAAE,SAAS,GAAG,OAAO,CAAC,MAAM,CAAC,CAGxE;AAED,wBAAsB,kBAAkB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,CAAC,CAGxE;AAED,wBAAsB,YAAY,CACjC,UAAU,EAAE,SAAS,EACrB,cAAc,EAAE,SAAS,EACzB,KAAK,EAAE,QAAQ,EAAE,GACf,OAAO,CAAC,SAAS,CAAC,CAQpB"}
|
package/dist/crypto.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Zero-dependency crypto helpers built on the native Web Crypto API.
|
|
2
|
+
// Used by the Same-Origin Callback Bridge (domain-redirect fallback) to
|
|
3
|
+
// encrypt the credential exchange between the login popup and the parent window.
|
|
4
|
+
//
|
|
5
|
+
// Scheme: ECDH (P-256) for key agreement + AES-GCM (256-bit) for payload encryption.
|
|
6
|
+
export const bufToB64 = (buf) => btoa(String.fromCharCode(...new Uint8Array(buf)));
|
|
7
|
+
export const b64ToBuf = (b64) => Uint8Array.from(atob(b64), (c) => c.charCodeAt(0));
|
|
8
|
+
export async function generateEcdhKeyPair() {
|
|
9
|
+
return window.crypto.subtle.generateKey({ name: 'ECDH', namedCurve: 'P-256' }, true, // MUST be true so the public key is exportable
|
|
10
|
+
['deriveKey']);
|
|
11
|
+
}
|
|
12
|
+
export async function exportPublicKeyB64(key) {
|
|
13
|
+
const jwk = await window.crypto.subtle.exportKey('jwk', key);
|
|
14
|
+
return btoa(JSON.stringify(jwk));
|
|
15
|
+
}
|
|
16
|
+
export async function importPublicKeyB64(b64) {
|
|
17
|
+
const jwk = JSON.parse(atob(b64));
|
|
18
|
+
return window.crypto.subtle.importKey('jwk', jwk, { name: 'ECDH', namedCurve: 'P-256' }, true, []);
|
|
19
|
+
}
|
|
20
|
+
export async function deriveAesKey(privateKey, otherPublicKey, usage) {
|
|
21
|
+
return window.crypto.subtle.deriveKey({ name: 'ECDH', public: otherPublicKey }, privateKey, { name: 'AES-GCM', length: 256 }, false, usage);
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=crypto.js.map
|
|
@@ -0,0 +1 @@
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OAAO,EAAC,EAAE,IAAI,EAAE,EAAE,CAAC,CAAC;AAClG,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,YAAY,CACjC,UAAqB,EACrB,cAAyB,EACzB,KAAiB;IAEjB,OAAO,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,SAAS,CACpC,EAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,cAAc,EAAC,EACtC,UAAU,EACV,EAAC,IAAI,EAAE,SAAS,EAAE,MAAM,EAAE,GAAG,EAAC,EAC9B,KAAK,EACL,KAAK,CACL,CAAC;AACH,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
import { HDKey } from '@scure/bip32';
|
|
2
|
+
import type { AccountGenerator } from '@etherplay/wallet-connector';
|
|
3
|
+
import type { AuthMechanism, EtherplayAccount, OriginAccount } from './types.js';
|
|
2
4
|
export declare function originKeyMessage(orig: string): string;
|
|
3
5
|
export declare function localKeyMessage(): string;
|
|
4
6
|
export declare function originPublicKeyPublicationMessage(orig: string, publicKey: `0x${string}`): string;
|
|
7
|
+
export declare function originDelegationMessage(orig: string, delegate: `0x${string}`): string;
|
|
5
8
|
export declare function fromEntropyKeyToMnemonic(entropyKey: `0x${string}`): string;
|
|
6
9
|
export declare function fromSignatureToKey(signature: `0x${string}`): `0x${string}`;
|
|
7
10
|
export declare function fromMnemonicToHDKey(mnemonic: string, index: number): HDKey;
|
|
11
|
+
export declare function deriveEtherplayAccount(key: `0x${string}`, mechanism: AuthMechanism, accountGenerator: AccountGenerator): EtherplayAccount;
|
|
12
|
+
export declare function deriveOriginAccount(origin: string, account: EtherplayAccount, accountGenerator: AccountGenerator): Promise<OriginAccount>;
|
|
8
13
|
export declare function strip0x(hex: string): string;
|
|
9
14
|
export declare function add0x(hex: string): string;
|
|
10
15
|
export declare function astr(str: unknown): void;
|
|
@@ -16,4 +21,5 @@ export declare function addChecksum(nonChecksummedAddress: string): string;
|
|
|
16
21
|
export declare function fromPublicKey(key: string | Uint8Array): string;
|
|
17
22
|
export declare function fromPrivateKey(key: string | Uint8Array): string;
|
|
18
23
|
export type * from './types.js';
|
|
24
|
+
export * from './crypto.js';
|
|
19
25
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAC,KAAK,EAAC,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAC,KAAK,EAAC,MAAM,cAAc,CAAC;AAGnC,OAAO,KAAK,EAAC,gBAAgB,EAAC,MAAM,6BAA6B,CAAC;AAClE,OAAO,KAAK,EAAC,aAAa,EAAE,gBAAgB,EAAE,aAAa,EAAC,MAAM,YAAY,CAAC;AAE/E,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;AAKD,wBAAgB,uBAAuB,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,KAAK,MAAM,EAAE,GAAG,MAAM,CAErF;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;AAED,wBAAgB,sBAAsB,CACrC,GAAG,EAAE,KAAK,MAAM,EAAE,EAClB,SAAS,EAAE,aAAa,EACxB,gBAAgB,EAAE,gBAAgB,GAChC,gBAAgB,CAalB;AAED,wBAAsB,mBAAmB,CACxC,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,gBAAgB,EACzB,gBAAgB,EAAE,gBAAgB,GAChC,OAAO,CAAC,aAAa,CAAC,CAwCxB;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;AAChC,cAAc,aAAa,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -13,6 +13,13 @@ export function localKeyMessage() {
|
|
|
13
13
|
export function originPublicKeyPublicationMessage(orig, publicKey) {
|
|
14
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
15
|
}
|
|
16
|
+
// The verifying contract reproduces this wording literally and renders the delegate address in
|
|
17
|
+
// lowercase, so both the text and the casing are consensus, not style: changing either invalidates
|
|
18
|
+
// every signature ever generated. `delegate` is lowercased here rather than at the call site so no
|
|
19
|
+
// caller can hand us an EIP-55 checksummed spelling that then fails to verify onchain.
|
|
20
|
+
export function originDelegationMessage(orig, delegate) {
|
|
21
|
+
return `Origin: ${orig}\n\nIMPORTANT: Only sign on trusted websites.\n\nThis authorizes the following address to act on your behalf onchain:\n\n${delegate.toLowerCase()}\n\nApps at this origin can use it to send transactions in your name.`;
|
|
22
|
+
}
|
|
16
23
|
export function fromEntropyKeyToMnemonic(entropyKey) {
|
|
17
24
|
return entropyToMnemonic(hexToBytes(entropyKey.slice(2)), wordlist);
|
|
18
25
|
}
|
|
@@ -25,6 +32,50 @@ export function fromMnemonicToHDKey(mnemonic, index) {
|
|
|
25
32
|
const hd = HDKey.fromMasterSeed(seed);
|
|
26
33
|
return hd.derive(`m/44'/60'/0'/0/${index}`);
|
|
27
34
|
}
|
|
35
|
+
export function deriveEtherplayAccount(key, mechanism, accountGenerator) {
|
|
36
|
+
const mnemonic = fromEntropyKeyToMnemonic(key);
|
|
37
|
+
return {
|
|
38
|
+
localAccount: {
|
|
39
|
+
address: accountGenerator.fromMnemonicToAccount(mnemonic, 0).address,
|
|
40
|
+
index: 0,
|
|
41
|
+
key,
|
|
42
|
+
},
|
|
43
|
+
signer: {
|
|
44
|
+
mechanismUsed: mechanism,
|
|
45
|
+
},
|
|
46
|
+
accountType: accountGenerator.type,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
export async function deriveOriginAccount(origin, account, accountGenerator) {
|
|
50
|
+
const accountMnemonic = fromEntropyKeyToMnemonic(account.localAccount.key);
|
|
51
|
+
const accountObject = accountGenerator.fromMnemonicToAccount(accountMnemonic, account.localAccount.index);
|
|
52
|
+
const originKeySignature = await accountGenerator.signTextMessage(originKeyMessage(origin), accountObject.privateKey);
|
|
53
|
+
const originKey = fromSignatureToKey(originKeySignature);
|
|
54
|
+
const originMnemonic = fromEntropyKeyToMnemonic(originKey);
|
|
55
|
+
const originAccount = accountGenerator.fromMnemonicToAccount(originMnemonic, 0);
|
|
56
|
+
const savedPublicKeyPublicationSignature = await accountGenerator.signTextMessage(originPublicKeyPublicationMessage(origin, originAccount.publicKey), accountObject.privateKey);
|
|
57
|
+
// Pre-generated because a hosted account holds its key at the wallet host and exposes no live
|
|
58
|
+
// arbitrary-signing capability: sign-in is the only moment this can be produced. It needs no
|
|
59
|
+
// nonce/expiry/chainId, since the signer is a pure function of the account key and the origin
|
|
60
|
+
// (deterministic ECDSA), so it asserts a permanent fact and replaying it changes nothing.
|
|
61
|
+
// Revocation is onchain, via a withdrawal flag the account sets itself.
|
|
62
|
+
const savedDelegationSignature = await accountGenerator.signTextMessage(originDelegationMessage(origin, originAccount.address), accountObject.privateKey);
|
|
63
|
+
return {
|
|
64
|
+
address: account.localAccount.address,
|
|
65
|
+
signer: {
|
|
66
|
+
origin,
|
|
67
|
+
publicKey: originAccount.publicKey,
|
|
68
|
+
address: originAccount.address,
|
|
69
|
+
privateKey: originAccount.privateKey,
|
|
70
|
+
mnemonicKey: originKey,
|
|
71
|
+
},
|
|
72
|
+
metadata: {},
|
|
73
|
+
mechanismUsed: account.signer.mechanismUsed,
|
|
74
|
+
savedPublicKeyPublicationSignature,
|
|
75
|
+
savedDelegationSignature,
|
|
76
|
+
accountType: accountGenerator.type,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
28
79
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
29
80
|
// TAKEN FROM https://github.com/paulmillr/micro-eth-signer/
|
|
30
81
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
@@ -74,4 +125,5 @@ export function fromPrivateKey(key) {
|
|
|
74
125
|
key = strip0x(key);
|
|
75
126
|
return fromPublicKey(secp256k1.getPublicKey(key, false));
|
|
76
127
|
}
|
|
128
|
+
export * from './crypto.js';
|
|
77
129
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,iBAAiB,EAAE,kBAAkB,EAAC,MAAM,cAAc,CAAC;AACnE,OAAO,EAAC,UAAU,EAAE,UAAU,EAAC,MAAM,qBAAqB,CAAC;AAC3D,OAAO,EAAC,QAAQ,EAAC,MAAM,gCAAgC,C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AG,CAAC,CAAC,CAAC,CAAC;QAClB,WAAW,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,WAAW,EAAE,CAAC,CAAC,4BAA4B;IAC7E,CAAC;IACD,OAAO,KAAK,CAAC,WAAW,CAAC,CAAC;AAC3B,CAAC;AAED,MAAM,UAAU,aAAa,CAAC,GAAwB;IACrD,IAAI,CAAC,GAAG;QAAE,MAAM,IAAI,KAAK,CAAC,sBAAsB,GAAG,GAAG,CAAC,CAAC;IACxD,MAAM,MAAM,GAAG,SAAS,CAAC,eAAe,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;IACxE,MAAM,MAAM,GAAG,UAAU,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;IAClD,OAAO,WAAW,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,eAAe;AAClE,CAAC;AAED,MAAM,UAAU,cAAc,CAAC,GAAwB;IACtD,IAAI,OAAO,GAAG,KAAK,QAAQ;QAAE,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC;IAChD,OAAO,aAAa,CAAC,SAAS,CAAC,YAAY,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC;AAC1D,CAAC;AAID,cAAc,aAAa,CAAC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -6,11 +6,10 @@ export type MnemonicMechanism<T extends number | undefined> = {
|
|
|
6
6
|
export type OauthMechanism = {
|
|
7
7
|
type: 'oauth';
|
|
8
8
|
provider: {
|
|
9
|
-
id:
|
|
10
|
-
} | {
|
|
11
|
-
id: 'auth0';
|
|
9
|
+
id: string;
|
|
10
|
+
} & ({} | {
|
|
12
11
|
connection: string;
|
|
13
|
-
};
|
|
12
|
+
});
|
|
14
13
|
} & ({
|
|
15
14
|
usePopup: true;
|
|
16
15
|
} | {
|
|
@@ -21,7 +20,7 @@ export type EmailMechanism<T extends string | undefined> = {
|
|
|
21
20
|
email: T;
|
|
22
21
|
mode: 'otp';
|
|
23
22
|
};
|
|
24
|
-
export type
|
|
23
|
+
export type AuthMechanism = EmailMechanism<string | undefined> | OauthMechanism | MnemonicMechanism<number | undefined>;
|
|
25
24
|
export type OriginAccount = {
|
|
26
25
|
address: `0x${string}`;
|
|
27
26
|
signer: {
|
|
@@ -34,10 +33,97 @@ export type OriginAccount = {
|
|
|
34
33
|
metadata: {
|
|
35
34
|
email?: string;
|
|
36
35
|
};
|
|
37
|
-
mechanismUsed:
|
|
36
|
+
mechanismUsed: AuthMechanism | {
|
|
38
37
|
type: string;
|
|
39
38
|
};
|
|
40
39
|
savedPublicKeyPublicationSignature?: `0x${string}`;
|
|
40
|
+
savedDelegationSignature?: `0x${string}`;
|
|
41
|
+
accountType: string;
|
|
42
|
+
};
|
|
43
|
+
export type AuthProviderSettings = {
|
|
44
|
+
[key: string]: unknown;
|
|
45
|
+
};
|
|
46
|
+
export type EtherplayAccount = {
|
|
47
|
+
localAccount: {
|
|
48
|
+
address: `0x${string}`;
|
|
49
|
+
index: number;
|
|
50
|
+
key: `0x${string}`;
|
|
51
|
+
};
|
|
52
|
+
signer: {
|
|
53
|
+
mechanismUsed: AuthMechanism;
|
|
54
|
+
[key: string]: unknown;
|
|
55
|
+
};
|
|
41
56
|
accountType: string;
|
|
42
57
|
};
|
|
58
|
+
export type AuthState = {
|
|
59
|
+
error?: {
|
|
60
|
+
message: string;
|
|
61
|
+
cause?: any;
|
|
62
|
+
};
|
|
63
|
+
} & ({
|
|
64
|
+
step: 'Idle';
|
|
65
|
+
} | {
|
|
66
|
+
step: 'Initialising';
|
|
67
|
+
auto: boolean;
|
|
68
|
+
} | {
|
|
69
|
+
step: 'Initialised';
|
|
70
|
+
} | {
|
|
71
|
+
step: 'MechanismToChoose';
|
|
72
|
+
} | {
|
|
73
|
+
step: 'InitialisingMechanism';
|
|
74
|
+
mechanism: AuthMechanism;
|
|
75
|
+
} | {
|
|
76
|
+
step: 'MechanismChosen';
|
|
77
|
+
mechanism: AuthMechanism;
|
|
78
|
+
} | {
|
|
79
|
+
step: 'EmailToProvide';
|
|
80
|
+
mechanism: EmailMechanism<undefined>;
|
|
81
|
+
} | {
|
|
82
|
+
step: 'WaitingForOTP';
|
|
83
|
+
mechanism: EmailMechanism<string>;
|
|
84
|
+
} | {
|
|
85
|
+
step: 'VerifyingOTP';
|
|
86
|
+
mechanism: EmailMechanism<string>;
|
|
87
|
+
} | {
|
|
88
|
+
step: 'InitializingOAuthPopup';
|
|
89
|
+
mechanism: OauthMechanism;
|
|
90
|
+
} | {
|
|
91
|
+
step: 'ConfirmOAuth';
|
|
92
|
+
mechanism: OauthMechanism;
|
|
93
|
+
} | {
|
|
94
|
+
step: 'WaitingForOAuthResponse';
|
|
95
|
+
mechanism: OauthMechanism;
|
|
96
|
+
} | {
|
|
97
|
+
step: 'MnemonicIndexToProvide';
|
|
98
|
+
mechanism: MnemonicMechanism<undefined>;
|
|
99
|
+
} | {
|
|
100
|
+
step: 'GeneratingAccount';
|
|
101
|
+
mechanism: AuthMechanism;
|
|
102
|
+
} | {
|
|
103
|
+
step: 'SignedIn';
|
|
104
|
+
mechanism: AuthMechanism;
|
|
105
|
+
account: EtherplayAccount;
|
|
106
|
+
requireOriginApproval: false | {
|
|
107
|
+
windowOrigin: string;
|
|
108
|
+
signingOrigin: string;
|
|
109
|
+
requestingAccess: boolean;
|
|
110
|
+
};
|
|
111
|
+
});
|
|
112
|
+
type Readable<T> = {
|
|
113
|
+
subscribe(func: (state: T) => void): () => void;
|
|
114
|
+
};
|
|
115
|
+
export type Redirection = {
|
|
116
|
+
windowOrigin: string;
|
|
117
|
+
signingOrigin: string;
|
|
118
|
+
id: string;
|
|
119
|
+
};
|
|
120
|
+
export interface AuthProvider extends Readable<AuthState> {
|
|
121
|
+
init(settings?: AuthProviderSettings): Promise<void>;
|
|
122
|
+
connect(mechanism: AuthMechanism, redirection?: Redirection): Promise<void>;
|
|
123
|
+
provideEmail: (email: string) => Promise<void>;
|
|
124
|
+
provideOTP(otp: string): Promise<void>;
|
|
125
|
+
provideMnemonicIndex: (index: number) => Promise<void>;
|
|
126
|
+
confirmOAuth(mechanism: OauthMechanism, searchParams: URLSearchParams, redirection: Redirection): Promise<void>;
|
|
127
|
+
}
|
|
128
|
+
export {};
|
|
43
129
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +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;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,CAAC,CAAC;CACT,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG;IAC5B,IAAI,EAAE,OAAO,CAAC;IAEd,QAAQ,EAAE;QAAC,EAAE,EAAE,
|
|
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;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,CAAC,CAAC;CACT,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG;IAC5B,IAAI,EAAE,OAAO,CAAC;IAEd,QAAQ,EAAE;QAAC,EAAE,EAAE,MAAM,CAAA;KAAC,GAAG,CAAC,EAAE,GAAG;QAAC,UAAU,EAAE,MAAM,CAAA;KAAC,CAAC,CAAC;CACrD,GAAG,CAAC;IAAC,QAAQ,EAAE,IAAI,CAAA;CAAC,GAAG;IAAC,QAAQ,EAAE,KAAK,CAAA;CAAC,CAAC,CAAC;A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
"description": "core functionality for @etherplay/connect and other",
|
|
5
5
|
"homepage": "https://github.com/wighawag/etherplay-connect#readme",
|
|
6
6
|
"repository": {
|
|
@@ -32,7 +32,8 @@
|
|
|
32
32
|
"@noble/hashes": "^1.7.1",
|
|
33
33
|
"@noble/secp256k1": "^2.2.3",
|
|
34
34
|
"@scure/bip32": "^1.6.2",
|
|
35
|
-
"@scure/bip39": "^1.5.4"
|
|
35
|
+
"@scure/bip39": "^1.5.4",
|
|
36
|
+
"@etherplay/wallet-connector": "0.0.5"
|
|
36
37
|
},
|
|
37
38
|
"devDependencies": {
|
|
38
39
|
"@vitest/coverage-v8": "^4.0.18",
|
package/src/crypto.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// Zero-dependency crypto helpers built on the native Web Crypto API.
|
|
2
|
+
// Used by the Same-Origin Callback Bridge (domain-redirect fallback) to
|
|
3
|
+
// encrypt the credential exchange between the login popup and the parent window.
|
|
4
|
+
//
|
|
5
|
+
// Scheme: ECDH (P-256) for key agreement + AES-GCM (256-bit) for payload encryption.
|
|
6
|
+
|
|
7
|
+
export const bufToB64 = (buf: ArrayBuffer | Uint8Array): string =>
|
|
8
|
+
btoa(String.fromCharCode(...new Uint8Array(buf as ArrayBuffer)));
|
|
9
|
+
|
|
10
|
+
export const b64ToBuf = (b64: string): Uint8Array => Uint8Array.from(atob(b64), (c) => c.charCodeAt(0));
|
|
11
|
+
|
|
12
|
+
export async function generateEcdhKeyPair(): Promise<CryptoKeyPair> {
|
|
13
|
+
return window.crypto.subtle.generateKey(
|
|
14
|
+
{name: 'ECDH', namedCurve: 'P-256'},
|
|
15
|
+
true, // MUST be true so the public key is exportable
|
|
16
|
+
['deriveKey'],
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export async function exportPublicKeyB64(key: CryptoKey): Promise<string> {
|
|
21
|
+
const jwk = await window.crypto.subtle.exportKey('jwk', key);
|
|
22
|
+
return btoa(JSON.stringify(jwk));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export async function importPublicKeyB64(b64: string): Promise<CryptoKey> {
|
|
26
|
+
const jwk = JSON.parse(atob(b64));
|
|
27
|
+
return window.crypto.subtle.importKey('jwk', jwk, {name: 'ECDH', namedCurve: 'P-256'}, true, []);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export async function deriveAesKey(
|
|
31
|
+
privateKey: CryptoKey,
|
|
32
|
+
otherPublicKey: CryptoKey,
|
|
33
|
+
usage: KeyUsage[],
|
|
34
|
+
): Promise<CryptoKey> {
|
|
35
|
+
return window.crypto.subtle.deriveKey(
|
|
36
|
+
{name: 'ECDH', public: otherPublicKey},
|
|
37
|
+
privateKey,
|
|
38
|
+
{name: 'AES-GCM', length: 256},
|
|
39
|
+
false,
|
|
40
|
+
usage,
|
|
41
|
+
);
|
|
42
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -4,6 +4,8 @@ import {wordlist} from '@scure/bip39/wordlists/english';
|
|
|
4
4
|
import {HDKey} from '@scure/bip32';
|
|
5
5
|
import {keccak_256} from '@noble/hashes/sha3';
|
|
6
6
|
import {secp256k1} from '@noble/curves/secp256k1';
|
|
7
|
+
import type {AccountGenerator} from '@etherplay/wallet-connector';
|
|
8
|
+
import type {AuthMechanism, EtherplayAccount, OriginAccount} from './types.js';
|
|
7
9
|
|
|
8
10
|
export function originKeyMessage(orig: string): string {
|
|
9
11
|
return `Origin: ${orig}\n\nIMPORTANT: Only sign on trusted websites.\n\nThis grants access to your private session account.\n\nVerify before proceeding.`;
|
|
@@ -14,6 +16,13 @@ export function localKeyMessage(): string {
|
|
|
14
16
|
export function originPublicKeyPublicationMessage(orig: string, publicKey: `0x${string}`): string {
|
|
15
17
|
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
18
|
}
|
|
19
|
+
// The verifying contract reproduces this wording literally and renders the delegate address in
|
|
20
|
+
// lowercase, so both the text and the casing are consensus, not style: changing either invalidates
|
|
21
|
+
// every signature ever generated. `delegate` is lowercased here rather than at the call site so no
|
|
22
|
+
// caller can hand us an EIP-55 checksummed spelling that then fails to verify onchain.
|
|
23
|
+
export function originDelegationMessage(orig: string, delegate: `0x${string}`): string {
|
|
24
|
+
return `Origin: ${orig}\n\nIMPORTANT: Only sign on trusted websites.\n\nThis authorizes the following address to act on your behalf onchain:\n\n${delegate.toLowerCase()}\n\nApps at this origin can use it to send transactions in your name.`;
|
|
25
|
+
}
|
|
17
26
|
|
|
18
27
|
export function fromEntropyKeyToMnemonic(entropyKey: `0x${string}`): string {
|
|
19
28
|
return entropyToMnemonic(hexToBytes(entropyKey.slice(2)), wordlist);
|
|
@@ -30,6 +39,71 @@ export function fromMnemonicToHDKey(mnemonic: string, index: number): HDKey {
|
|
|
30
39
|
return hd.derive(`m/44'/60'/0'/0/${index}`);
|
|
31
40
|
}
|
|
32
41
|
|
|
42
|
+
export function deriveEtherplayAccount(
|
|
43
|
+
key: `0x${string}`,
|
|
44
|
+
mechanism: AuthMechanism,
|
|
45
|
+
accountGenerator: AccountGenerator,
|
|
46
|
+
): EtherplayAccount {
|
|
47
|
+
const mnemonic = fromEntropyKeyToMnemonic(key);
|
|
48
|
+
return {
|
|
49
|
+
localAccount: {
|
|
50
|
+
address: accountGenerator.fromMnemonicToAccount(mnemonic, 0).address,
|
|
51
|
+
index: 0,
|
|
52
|
+
key,
|
|
53
|
+
},
|
|
54
|
+
signer: {
|
|
55
|
+
mechanismUsed: mechanism,
|
|
56
|
+
},
|
|
57
|
+
accountType: accountGenerator.type,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export async function deriveOriginAccount(
|
|
62
|
+
origin: string,
|
|
63
|
+
account: EtherplayAccount,
|
|
64
|
+
accountGenerator: AccountGenerator,
|
|
65
|
+
): Promise<OriginAccount> {
|
|
66
|
+
const accountMnemonic = fromEntropyKeyToMnemonic(account.localAccount.key);
|
|
67
|
+
const accountObject = accountGenerator.fromMnemonicToAccount(accountMnemonic, account.localAccount.index);
|
|
68
|
+
|
|
69
|
+
const originKeySignature = await accountGenerator.signTextMessage(originKeyMessage(origin), accountObject.privateKey);
|
|
70
|
+
|
|
71
|
+
const originKey = fromSignatureToKey(originKeySignature);
|
|
72
|
+
const originMnemonic = fromEntropyKeyToMnemonic(originKey);
|
|
73
|
+
const originAccount = accountGenerator.fromMnemonicToAccount(originMnemonic, 0);
|
|
74
|
+
|
|
75
|
+
const savedPublicKeyPublicationSignature = await accountGenerator.signTextMessage(
|
|
76
|
+
originPublicKeyPublicationMessage(origin, originAccount.publicKey),
|
|
77
|
+
accountObject.privateKey,
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
// Pre-generated because a hosted account holds its key at the wallet host and exposes no live
|
|
81
|
+
// arbitrary-signing capability: sign-in is the only moment this can be produced. It needs no
|
|
82
|
+
// nonce/expiry/chainId, since the signer is a pure function of the account key and the origin
|
|
83
|
+
// (deterministic ECDSA), so it asserts a permanent fact and replaying it changes nothing.
|
|
84
|
+
// Revocation is onchain, via a withdrawal flag the account sets itself.
|
|
85
|
+
const savedDelegationSignature = await accountGenerator.signTextMessage(
|
|
86
|
+
originDelegationMessage(origin, originAccount.address),
|
|
87
|
+
accountObject.privateKey,
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
return {
|
|
91
|
+
address: account.localAccount.address,
|
|
92
|
+
signer: {
|
|
93
|
+
origin,
|
|
94
|
+
publicKey: originAccount.publicKey,
|
|
95
|
+
address: originAccount.address,
|
|
96
|
+
privateKey: originAccount.privateKey,
|
|
97
|
+
mnemonicKey: originKey,
|
|
98
|
+
},
|
|
99
|
+
metadata: {},
|
|
100
|
+
mechanismUsed: account.signer.mechanismUsed,
|
|
101
|
+
savedPublicKeyPublicationSignature,
|
|
102
|
+
savedDelegationSignature,
|
|
103
|
+
accountType: accountGenerator.type,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
33
107
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
34
108
|
// TAKEN FROM https://github.com/paulmillr/micro-eth-signer/
|
|
35
109
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
@@ -84,3 +158,4 @@ export function fromPrivateKey(key: string | Uint8Array): string {
|
|
|
84
158
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
85
159
|
|
|
86
160
|
export type * from './types.js';
|
|
161
|
+
export * from './crypto.js';
|
package/src/types.ts
CHANGED
|
@@ -7,8 +7,8 @@ export type MnemonicMechanism<T extends number | undefined> = {
|
|
|
7
7
|
export type OauthMechanism = {
|
|
8
8
|
type: 'oauth';
|
|
9
9
|
|
|
10
|
-
provider: {id:
|
|
11
|
-
} & ({usePopup: true} | {usePopup: false});
|
|
10
|
+
provider: {id: string} & ({} | {connection: string});
|
|
11
|
+
} & ({usePopup: true} | {usePopup: false});
|
|
12
12
|
|
|
13
13
|
export type EmailMechanism<T extends string | undefined> = {
|
|
14
14
|
type: 'email';
|
|
@@ -16,10 +16,7 @@ export type EmailMechanism<T extends string | undefined> = {
|
|
|
16
16
|
mode: 'otp';
|
|
17
17
|
};
|
|
18
18
|
|
|
19
|
-
export type
|
|
20
|
-
| EmailMechanism<string | undefined>
|
|
21
|
-
| OauthMechanism
|
|
22
|
-
| MnemonicMechanism<number | undefined>;
|
|
19
|
+
export type AuthMechanism = EmailMechanism<string | undefined> | OauthMechanism | MnemonicMechanism<number | undefined>;
|
|
23
20
|
|
|
24
21
|
export type OriginAccount = {
|
|
25
22
|
address: `0x${string}`;
|
|
@@ -33,7 +30,133 @@ export type OriginAccount = {
|
|
|
33
30
|
metadata: {
|
|
34
31
|
email?: string;
|
|
35
32
|
};
|
|
36
|
-
mechanismUsed:
|
|
33
|
+
mechanismUsed: AuthMechanism | {type: string};
|
|
37
34
|
savedPublicKeyPublicationSignature?: `0x${string}`;
|
|
35
|
+
// authorizes `signer.address` to act onchain on this account's behalf, over
|
|
36
|
+
// `originDelegationMessage(signer.origin, signer.address)`. Optional because accounts restored
|
|
37
|
+
// from storage, or built by paths that never derived one, will not have it.
|
|
38
|
+
savedDelegationSignature?: `0x${string}`;
|
|
38
39
|
accountType: string;
|
|
39
40
|
};
|
|
41
|
+
|
|
42
|
+
// --- AUTH
|
|
43
|
+
|
|
44
|
+
export type AuthProviderSettings = {
|
|
45
|
+
[key: string]: unknown;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export type EtherplayAccount = {
|
|
49
|
+
localAccount: {
|
|
50
|
+
address: `0x${string}`;
|
|
51
|
+
index: number;
|
|
52
|
+
key: `0x${string}`;
|
|
53
|
+
};
|
|
54
|
+
signer: {
|
|
55
|
+
mechanismUsed: AuthMechanism;
|
|
56
|
+
[key: string]: unknown;
|
|
57
|
+
};
|
|
58
|
+
accountType: string;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export type AuthState = {error?: {message: string; cause?: any}} & (
|
|
62
|
+
| {
|
|
63
|
+
step: 'Idle';
|
|
64
|
+
}
|
|
65
|
+
| {
|
|
66
|
+
step: 'Initialising';
|
|
67
|
+
auto: boolean;
|
|
68
|
+
}
|
|
69
|
+
| {
|
|
70
|
+
step: 'Initialised';
|
|
71
|
+
}
|
|
72
|
+
| {
|
|
73
|
+
step: 'MechanismToChoose';
|
|
74
|
+
}
|
|
75
|
+
| {
|
|
76
|
+
step: 'InitialisingMechanism';
|
|
77
|
+
mechanism: AuthMechanism;
|
|
78
|
+
}
|
|
79
|
+
| {
|
|
80
|
+
step: 'MechanismChosen';
|
|
81
|
+
mechanism: AuthMechanism;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// --------------------------------------------------------------------------------------------
|
|
85
|
+
// Email
|
|
86
|
+
// --------------------------------------------------------------------------------------------
|
|
87
|
+
| {
|
|
88
|
+
step: 'EmailToProvide';
|
|
89
|
+
mechanism: EmailMechanism<undefined>;
|
|
90
|
+
}
|
|
91
|
+
| {
|
|
92
|
+
step: 'WaitingForOTP';
|
|
93
|
+
mechanism: EmailMechanism<string>;
|
|
94
|
+
}
|
|
95
|
+
| {
|
|
96
|
+
step: 'VerifyingOTP';
|
|
97
|
+
mechanism: EmailMechanism<string>;
|
|
98
|
+
}
|
|
99
|
+
// --------------------------------------------------------------------------------------------
|
|
100
|
+
|
|
101
|
+
// --------------------------------------------------------------------------------------------
|
|
102
|
+
// OAuth
|
|
103
|
+
// --------------------------------------------------------------------------------------------
|
|
104
|
+
| {
|
|
105
|
+
step: 'InitializingOAuthPopup';
|
|
106
|
+
mechanism: OauthMechanism;
|
|
107
|
+
}
|
|
108
|
+
| {
|
|
109
|
+
step: 'ConfirmOAuth';
|
|
110
|
+
mechanism: OauthMechanism;
|
|
111
|
+
}
|
|
112
|
+
| {
|
|
113
|
+
step: 'WaitingForOAuthResponse';
|
|
114
|
+
mechanism: OauthMechanism;
|
|
115
|
+
}
|
|
116
|
+
// --------------------------------------------------------------------------------------------
|
|
117
|
+
|
|
118
|
+
// --------------------------------------------------------------------------------------------
|
|
119
|
+
// Mnemonic
|
|
120
|
+
// --------------------------------------------------------------------------------------------
|
|
121
|
+
| {
|
|
122
|
+
step: 'MnemonicIndexToProvide';
|
|
123
|
+
mechanism: MnemonicMechanism<undefined>;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// --------------------------------------------------------------------------------------------
|
|
127
|
+
|
|
128
|
+
// --------------------------------------------------------------------------------------------
|
|
129
|
+
// Final Success
|
|
130
|
+
// --------------------------------------------------------------------------------------------
|
|
131
|
+
| {
|
|
132
|
+
step: 'GeneratingAccount';
|
|
133
|
+
mechanism: AuthMechanism;
|
|
134
|
+
}
|
|
135
|
+
| {
|
|
136
|
+
step: 'SignedIn';
|
|
137
|
+
mechanism: AuthMechanism;
|
|
138
|
+
account: EtherplayAccount;
|
|
139
|
+
requireOriginApproval:
|
|
140
|
+
| false
|
|
141
|
+
| {
|
|
142
|
+
windowOrigin: string;
|
|
143
|
+
signingOrigin: string;
|
|
144
|
+
requestingAccess: boolean;
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
type Readable<T> = {
|
|
150
|
+
subscribe(func: (state: T) => void): () => void;
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
export type Redirection = {windowOrigin: string; signingOrigin: string; id: string};
|
|
154
|
+
|
|
155
|
+
export interface AuthProvider extends Readable<AuthState> {
|
|
156
|
+
init(settings?: AuthProviderSettings): Promise<void>;
|
|
157
|
+
connect(mechanism: AuthMechanism, redirection?: Redirection): Promise<void>;
|
|
158
|
+
provideEmail: (email: string) => Promise<void>;
|
|
159
|
+
provideOTP(otp: string): Promise<void>;
|
|
160
|
+
provideMnemonicIndex: (index: number) => Promise<void>;
|
|
161
|
+
confirmOAuth(mechanism: OauthMechanism, searchParams: URLSearchParams, redirection: Redirection): Promise<void>;
|
|
162
|
+
}
|