@supabase/gotrue-js 2.71.1 → 2.72.0-rc.2
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/README.md +6 -0
- package/dist/main/GoTrueClient.d.ts +5 -1
- package/dist/main/GoTrueClient.d.ts.map +1 -1
- package/dist/main/GoTrueClient.js +119 -7
- package/dist/main/GoTrueClient.js.map +1 -1
- package/dist/main/lib/types.d.ts +29 -2
- package/dist/main/lib/types.d.ts.map +1 -1
- package/dist/main/lib/types.js.map +1 -1
- package/dist/main/lib/version.d.ts +1 -1
- package/dist/main/lib/version.d.ts.map +1 -1
- package/dist/main/lib/version.js +1 -1
- package/dist/main/lib/version.js.map +1 -1
- package/dist/main/lib/web3/ethereum.d.ts +96 -0
- package/dist/main/lib/web3/ethereum.d.ts.map +1 -0
- package/dist/main/lib/web3/ethereum.js +67 -0
- package/dist/main/lib/web3/ethereum.js.map +1 -0
- package/dist/main/lib/web3/solana.d.ts.map +1 -0
- package/dist/main/lib/web3/solana.js.map +1 -0
- package/dist/module/GoTrueClient.d.ts +5 -1
- package/dist/module/GoTrueClient.d.ts.map +1 -1
- package/dist/module/GoTrueClient.js +118 -6
- package/dist/module/GoTrueClient.js.map +1 -1
- package/dist/module/lib/types.d.ts +29 -2
- package/dist/module/lib/types.d.ts.map +1 -1
- package/dist/module/lib/types.js.map +1 -1
- package/dist/module/lib/version.d.ts +1 -1
- package/dist/module/lib/version.d.ts.map +1 -1
- package/dist/module/lib/version.js +1 -1
- package/dist/module/lib/version.js.map +1 -1
- package/dist/module/lib/web3/ethereum.d.ts +96 -0
- package/dist/module/lib/web3/ethereum.d.ts.map +1 -0
- package/dist/module/lib/web3/ethereum.js +60 -0
- package/dist/module/lib/web3/ethereum.js.map +1 -0
- package/dist/module/lib/web3/solana.d.ts.map +1 -0
- package/dist/module/lib/web3/solana.js.map +1 -0
- package/package.json +1 -1
- package/src/GoTrueClient.ts +160 -5
- package/src/lib/types.ts +43 -2
- package/src/lib/version.ts +1 -1
- package/src/lib/web3/ethereum.ts +184 -0
- package/dist/main/lib/solana.d.ts.map +0 -1
- package/dist/main/lib/solana.js.map +0 -1
- package/dist/module/lib/solana.d.ts.map +0 -1
- package/dist/module/lib/solana.js.map +0 -1
- /package/dist/main/lib/{solana.d.ts → web3/solana.d.ts} +0 -0
- /package/dist/main/lib/{solana.js → web3/solana.js} +0 -0
- /package/dist/module/lib/{solana.d.ts → web3/solana.d.ts} +0 -0
- /package/dist/module/lib/{solana.js → web3/solana.js} +0 -0
- /package/src/lib/{solana.ts → web3/solana.ts} +0 -0
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// types and functions copied over from viem so this library doesn't depend on it
|
|
2
|
+
export function getAddress(address) {
|
|
3
|
+
if (!/^0x[a-fA-F0-9]{40}$/.test(address)) {
|
|
4
|
+
throw new Error(`@supabase/auth-js: Address "${address}" is invalid.`);
|
|
5
|
+
}
|
|
6
|
+
return address.toLowerCase();
|
|
7
|
+
}
|
|
8
|
+
export function fromHex(hex) {
|
|
9
|
+
return parseInt(hex, 16);
|
|
10
|
+
}
|
|
11
|
+
export function toHex(value) {
|
|
12
|
+
const bytes = new TextEncoder().encode(value);
|
|
13
|
+
const hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, '0')).join('');
|
|
14
|
+
return ('0x' + hex);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Creates EIP-4361 formatted message.
|
|
18
|
+
*/
|
|
19
|
+
export function createSiweMessage(parameters) {
|
|
20
|
+
var _a;
|
|
21
|
+
const { chainId, domain, expirationTime, issuedAt = new Date(), nonce, notBefore, requestId, resources, scheme, uri, version, } = parameters;
|
|
22
|
+
// Validate fields
|
|
23
|
+
{
|
|
24
|
+
if (!Number.isInteger(chainId))
|
|
25
|
+
throw new Error(`@supabase/auth-js: Invalid SIWE message field "chainId". Chain ID must be a EIP-155 chain ID. Provided value: ${chainId}`);
|
|
26
|
+
if (!domain)
|
|
27
|
+
throw new Error(`@supabase/auth-js: Invalid SIWE message field "domain". Domain must be provided.`);
|
|
28
|
+
if (nonce && nonce.length < 8)
|
|
29
|
+
throw new Error(`@supabase/auth-js: Invalid SIWE message field "nonce". Nonce must be at least 8 characters. Provided value: ${nonce}`);
|
|
30
|
+
if (!uri)
|
|
31
|
+
throw new Error(`@supabase/auth-js: Invalid SIWE message field "uri". URI must be provided.`);
|
|
32
|
+
if (version !== '1')
|
|
33
|
+
throw new Error(`@supabase/auth-js: Invalid SIWE message field "version". Version must be '1'. Provided value: ${version}`);
|
|
34
|
+
if ((_a = parameters.statement) === null || _a === void 0 ? void 0 : _a.includes('\n'))
|
|
35
|
+
throw new Error(`@supabase/auth-js: Invalid SIWE message field "statement". Statement must not include '\\n'. Provided value: ${parameters.statement}`);
|
|
36
|
+
}
|
|
37
|
+
// Construct message
|
|
38
|
+
const address = getAddress(parameters.address);
|
|
39
|
+
const origin = scheme ? `${scheme}://${domain}` : domain;
|
|
40
|
+
const statement = parameters.statement ? `${parameters.statement}\n` : '';
|
|
41
|
+
const prefix = `${origin} wants you to sign in with your Ethereum account:\n${address}\n\n${statement}`;
|
|
42
|
+
let suffix = `URI: ${uri}\nVersion: ${version}\nChain ID: ${chainId}${nonce ? `\nNonce: ${nonce}` : ''}\nIssued At: ${issuedAt.toISOString()}`;
|
|
43
|
+
if (expirationTime)
|
|
44
|
+
suffix += `\nExpiration Time: ${expirationTime.toISOString()}`;
|
|
45
|
+
if (notBefore)
|
|
46
|
+
suffix += `\nNot Before: ${notBefore.toISOString()}`;
|
|
47
|
+
if (requestId)
|
|
48
|
+
suffix += `\nRequest ID: ${requestId}`;
|
|
49
|
+
if (resources) {
|
|
50
|
+
let content = '\nResources:';
|
|
51
|
+
for (const resource of resources) {
|
|
52
|
+
if (!resource || typeof resource !== 'string')
|
|
53
|
+
throw new Error(`@supabase/auth-js: Invalid SIWE message field "resources". Every resource must be a valid string. Provided value: ${resource}`);
|
|
54
|
+
content += `\n- ${resource}`;
|
|
55
|
+
}
|
|
56
|
+
suffix += content;
|
|
57
|
+
}
|
|
58
|
+
return `${prefix}\n${suffix}`;
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=ethereum.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ethereum.js","sourceRoot":"","sources":["../../../../src/lib/web3/ethereum.ts"],"names":[],"mappings":"AAAA,iFAAiF;AA2FjF,MAAM,UAAU,UAAU,CAAC,OAAe;IACxC,IAAI,CAAC,qBAAqB,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE;QACxC,MAAM,IAAI,KAAK,CAAC,+BAA+B,OAAO,eAAe,CAAC,CAAA;KACvE;IACD,OAAO,OAAO,CAAC,WAAW,EAAa,CAAA;AACzC,CAAC;AAED,MAAM,UAAU,OAAO,CAAC,GAAQ;IAC9B,OAAO,QAAQ,CAAC,GAAG,EAAE,EAAE,CAAC,CAAA;AAC1B,CAAC;AAED,MAAM,UAAU,KAAK,CAAC,KAAa;IACjC,MAAM,KAAK,GAAG,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;IAC7C,MAAM,GAAG,GAAG,KAAK,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;IACpF,OAAO,CAAC,IAAI,GAAG,GAAG,CAAQ,CAAA;AAC5B,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,iBAAiB,CAAC,UAAuB;;IACvD,MAAM,EACJ,OAAO,EACP,MAAM,EACN,cAAc,EACd,QAAQ,GAAG,IAAI,IAAI,EAAE,EACrB,KAAK,EACL,SAAS,EACT,SAAS,EACT,SAAS,EACT,MAAM,EACN,GAAG,EACH,OAAO,GACR,GAAG,UAAU,CAAA;IAEd,kBAAkB;IAClB;QACE,IAA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CAAC,aAAa,CAAC,EAAE,SAAS,CAAA;CACnC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"solana.js","sourceRoot":"","sources":["../../../../src/lib/web3/solana.ts"],"names":[],"mappings":"AAAA,2HAA2H"}
|
package/package.json
CHANGED
package/src/GoTrueClient.ts
CHANGED
|
@@ -32,6 +32,7 @@ import {
|
|
|
32
32
|
_ssoResponse,
|
|
33
33
|
} from './lib/fetch'
|
|
34
34
|
import {
|
|
35
|
+
deepClone,
|
|
35
36
|
Deferred,
|
|
36
37
|
getItemAsync,
|
|
37
38
|
isBrowser,
|
|
@@ -110,9 +111,18 @@ import type {
|
|
|
110
111
|
SolanaWeb3Credentials,
|
|
111
112
|
SolanaWallet,
|
|
112
113
|
Web3Credentials,
|
|
114
|
+
EthereumWeb3Credentials,
|
|
115
|
+
EthereumWallet,
|
|
113
116
|
} from './lib/types'
|
|
114
117
|
import { stringToUint8Array, bytesToBase64URL } from './lib/base64url'
|
|
115
|
-
import {
|
|
118
|
+
import {
|
|
119
|
+
fromHex,
|
|
120
|
+
getAddress,
|
|
121
|
+
Hex,
|
|
122
|
+
toHex,
|
|
123
|
+
createSiweMessage,
|
|
124
|
+
SiweMessage,
|
|
125
|
+
} from './lib/web3/ethereum'
|
|
116
126
|
|
|
117
127
|
polyfillGlobalThis() // Make "globalThis" available
|
|
118
128
|
|
|
@@ -648,7 +658,10 @@ export default class GoTrueClient {
|
|
|
648
658
|
|
|
649
659
|
/**
|
|
650
660
|
* Signs in a user by verifying a message signed by the user's private key.
|
|
651
|
-
*
|
|
661
|
+
* Supports Ethereum (via Sign-In-With-Ethereum) & Solana (Sign-In-With-Solana) standards,
|
|
662
|
+
* both of which derive from the EIP-4361 standard
|
|
663
|
+
* With slight variation on Solana's side.
|
|
664
|
+
* @reference https://eips.ethereum.org/EIPS/eip-4361
|
|
652
665
|
*/
|
|
653
666
|
async signInWithWeb3(credentials: Web3Credentials): Promise<
|
|
654
667
|
| {
|
|
@@ -659,11 +672,153 @@ export default class GoTrueClient {
|
|
|
659
672
|
> {
|
|
660
673
|
const { chain } = credentials
|
|
661
674
|
|
|
662
|
-
|
|
663
|
-
|
|
675
|
+
switch (chain) {
|
|
676
|
+
case 'ethereum':
|
|
677
|
+
return await this.signInWithEthereum(credentials)
|
|
678
|
+
case 'solana':
|
|
679
|
+
return await this.signInWithSolana(credentials)
|
|
680
|
+
default:
|
|
681
|
+
throw new Error(`@supabase/auth-js: Unsupported chain "${chain}"`)
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
private async signInWithEthereum(
|
|
686
|
+
credentials: EthereumWeb3Credentials
|
|
687
|
+
): Promise<
|
|
688
|
+
| { data: { session: Session; user: User }; error: null }
|
|
689
|
+
| { data: { session: null; user: null }; error: AuthError }
|
|
690
|
+
> {
|
|
691
|
+
// TODO: flatten type
|
|
692
|
+
let message: string
|
|
693
|
+
let signature: Hex
|
|
694
|
+
|
|
695
|
+
if ('message' in credentials) {
|
|
696
|
+
message = credentials.message
|
|
697
|
+
signature = credentials.signature
|
|
698
|
+
} else {
|
|
699
|
+
const { chain, wallet, statement, options } = credentials
|
|
700
|
+
|
|
701
|
+
let resolvedWallet: EthereumWallet
|
|
702
|
+
|
|
703
|
+
if (!isBrowser()) {
|
|
704
|
+
if (typeof wallet !== 'object' || !options?.url) {
|
|
705
|
+
throw new Error(
|
|
706
|
+
'@supabase/auth-js: Both wallet and url must be specified in non-browser environments.'
|
|
707
|
+
)
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
resolvedWallet = wallet
|
|
711
|
+
} else if (typeof wallet === 'object') {
|
|
712
|
+
resolvedWallet = wallet
|
|
713
|
+
} else {
|
|
714
|
+
const windowAny = window as any
|
|
715
|
+
|
|
716
|
+
if (
|
|
717
|
+
'ethereum' in windowAny &&
|
|
718
|
+
typeof windowAny.ethereum === 'object' &&
|
|
719
|
+
'request' in windowAny.ethereum &&
|
|
720
|
+
typeof windowAny.ethereum.request === 'function'
|
|
721
|
+
) {
|
|
722
|
+
resolvedWallet = windowAny.ethereum
|
|
723
|
+
} else {
|
|
724
|
+
throw new Error(
|
|
725
|
+
`@supabase/auth-js: No compatible Ethereum wallet interface on the window object (window.ethereum) detected. Make sure the user already has a wallet installed and connected for this app. Prefer passing the wallet interface object directly to signInWithWeb3({ chain: 'ethereum', wallet: resolvedUserWallet }) instead.`
|
|
726
|
+
)
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
const url = new URL(options?.url ?? window.location.href)
|
|
731
|
+
|
|
732
|
+
const accounts = await resolvedWallet
|
|
733
|
+
.request({
|
|
734
|
+
method: 'eth_requestAccounts',
|
|
735
|
+
})
|
|
736
|
+
.then((accs) => accs as string[])
|
|
737
|
+
.catch(() => {
|
|
738
|
+
throw new Error(
|
|
739
|
+
`@supabase/auth-js: Wallet method eth_requestAccounts is missing or invalid`
|
|
740
|
+
)
|
|
741
|
+
})
|
|
742
|
+
|
|
743
|
+
if (!accounts || accounts.length === 0) {
|
|
744
|
+
throw new Error(
|
|
745
|
+
`@supabase/auth-js: No accounts available. Please ensure the wallet is connected.`
|
|
746
|
+
)
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
const address = getAddress(accounts[0])
|
|
750
|
+
|
|
751
|
+
let chainId = options?.signInWithEthereum?.chainId
|
|
752
|
+
if (!chainId) {
|
|
753
|
+
const chainIdHex = await resolvedWallet.request({
|
|
754
|
+
method: 'eth_chainId',
|
|
755
|
+
})
|
|
756
|
+
chainId = fromHex(chainIdHex as Hex)
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
const siweMessage: SiweMessage = {
|
|
760
|
+
domain: url.host,
|
|
761
|
+
address: address,
|
|
762
|
+
statement: statement,
|
|
763
|
+
uri: url.href,
|
|
764
|
+
version: '1',
|
|
765
|
+
chainId: chainId,
|
|
766
|
+
nonce: options?.signInWithEthereum?.nonce,
|
|
767
|
+
issuedAt: options?.signInWithEthereum?.issuedAt ?? new Date(),
|
|
768
|
+
expirationTime: options?.signInWithEthereum?.expirationTime,
|
|
769
|
+
notBefore: options?.signInWithEthereum?.notBefore,
|
|
770
|
+
requestId: options?.signInWithEthereum?.requestId,
|
|
771
|
+
resources: options?.signInWithEthereum?.resources,
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
message = createSiweMessage(siweMessage)
|
|
775
|
+
|
|
776
|
+
// Sign message
|
|
777
|
+
signature = (await resolvedWallet.request({
|
|
778
|
+
method: 'personal_sign',
|
|
779
|
+
params: [toHex(message), address],
|
|
780
|
+
})) as Hex
|
|
664
781
|
}
|
|
665
782
|
|
|
666
|
-
|
|
783
|
+
try {
|
|
784
|
+
const { data, error } = await _request(
|
|
785
|
+
this.fetch,
|
|
786
|
+
'POST',
|
|
787
|
+
`${this.url}/token?grant_type=web3`,
|
|
788
|
+
{
|
|
789
|
+
headers: this.headers,
|
|
790
|
+
body: {
|
|
791
|
+
chain: 'ethereum',
|
|
792
|
+
message,
|
|
793
|
+
signature,
|
|
794
|
+
...(credentials.options?.captchaToken
|
|
795
|
+
? { gotrue_meta_security: { captcha_token: credentials.options?.captchaToken } }
|
|
796
|
+
: null),
|
|
797
|
+
},
|
|
798
|
+
xform: _sessionResponse,
|
|
799
|
+
}
|
|
800
|
+
)
|
|
801
|
+
if (error) {
|
|
802
|
+
throw error
|
|
803
|
+
}
|
|
804
|
+
if (!data || !data.session || !data.user) {
|
|
805
|
+
return {
|
|
806
|
+
data: { user: null, session: null },
|
|
807
|
+
error: new AuthInvalidTokenResponseError(),
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
if (data.session) {
|
|
811
|
+
await this._saveSession(data.session)
|
|
812
|
+
await this._notifyAllSubscribers('SIGNED_IN', data.session)
|
|
813
|
+
}
|
|
814
|
+
return { data: { ...data }, error }
|
|
815
|
+
} catch (error) {
|
|
816
|
+
if (isAuthError(error)) {
|
|
817
|
+
return { data: { user: null, session: null }, error }
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
throw error
|
|
821
|
+
}
|
|
667
822
|
}
|
|
668
823
|
|
|
669
824
|
private async signInWithSolana(credentials: SolanaWeb3Credentials) {
|
package/src/lib/types.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
import { EIP1193Provider } from './web3/ethereum'
|
|
1
2
|
import { AuthError } from './errors'
|
|
2
3
|
import { Fetch } from './fetch'
|
|
3
|
-
import type { SolanaSignInInput, SolanaSignInOutput } from './solana'
|
|
4
|
+
import type { SolanaSignInInput, SolanaSignInOutput } from './web3/solana'
|
|
5
|
+
import { EthereumSignInInput, Hex } from './web3/ethereum'
|
|
4
6
|
|
|
5
7
|
/** One of the providers supported by GoTrue. */
|
|
6
8
|
export type Provider =
|
|
@@ -673,7 +675,46 @@ export type SolanaWeb3Credentials =
|
|
|
673
675
|
}
|
|
674
676
|
}
|
|
675
677
|
|
|
676
|
-
export type
|
|
678
|
+
export type EthereumWallet = EIP1193Provider
|
|
679
|
+
|
|
680
|
+
export type EthereumWeb3Credentials =
|
|
681
|
+
| {
|
|
682
|
+
chain: 'ethereum'
|
|
683
|
+
|
|
684
|
+
/** Wallet interface to use. If not specified will default to `window.solana`. */
|
|
685
|
+
wallet?: EthereumWallet
|
|
686
|
+
|
|
687
|
+
/** Optional statement to include in the Sign in with Solana message. Must not include new line characters. Most wallets like Phantom **require specifying a statement!** */
|
|
688
|
+
statement?: string
|
|
689
|
+
|
|
690
|
+
options?: {
|
|
691
|
+
/** URL to use with the wallet interface. Some wallets do not allow signing a message for URLs different from the current page. */
|
|
692
|
+
url?: string
|
|
693
|
+
|
|
694
|
+
/** Verification token received when the user completes the captcha on the site. */
|
|
695
|
+
captchaToken?: string
|
|
696
|
+
|
|
697
|
+
signInWithEthereum?: Partial<
|
|
698
|
+
Omit<EthereumSignInInput, 'version' | 'domain' | 'uri' | 'statement'>
|
|
699
|
+
>
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
| {
|
|
703
|
+
chain: 'ethereum'
|
|
704
|
+
|
|
705
|
+
/** Sign in with Ethereum compatible message. Must include `Issued At`, `URI` and `Version`. */
|
|
706
|
+
message: string
|
|
707
|
+
|
|
708
|
+
/** Ed25519 signature of the message. */
|
|
709
|
+
signature: Hex
|
|
710
|
+
|
|
711
|
+
options?: {
|
|
712
|
+
/** Verification token received when the user completes the captcha on the site. */
|
|
713
|
+
captchaToken?: string
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
export type Web3Credentials = SolanaWeb3Credentials | EthereumWeb3Credentials
|
|
677
718
|
|
|
678
719
|
export type VerifyOtpParams = VerifyMobileOtpParams | VerifyEmailOtpParams | VerifyTokenHashParams
|
|
679
720
|
export interface VerifyMobileOtpParams {
|
package/src/lib/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = '2.
|
|
1
|
+
export const version = '2.72.0-rc.2'
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
// types and functions copied over from viem so this library doesn't depend on it
|
|
2
|
+
|
|
3
|
+
export type Hex = `0x${string}`
|
|
4
|
+
|
|
5
|
+
export type Address = Hex
|
|
6
|
+
|
|
7
|
+
export type EIP1193EventMap = {
|
|
8
|
+
accountsChanged(accounts: Address[]): void
|
|
9
|
+
chainChanged(chainId: string): void
|
|
10
|
+
connect(connectInfo: { chainId: string }): void
|
|
11
|
+
disconnect(error: { code: number; message: string }): void
|
|
12
|
+
message(message: { type: string; data: unknown }): void
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type EIP1193Events = {
|
|
16
|
+
on<event extends keyof EIP1193EventMap>(event: event, listener: EIP1193EventMap[event]): void
|
|
17
|
+
removeListener<event extends keyof EIP1193EventMap>(
|
|
18
|
+
event: event,
|
|
19
|
+
listener: EIP1193EventMap[event]
|
|
20
|
+
): void
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export type EIP1193RequestFn = (args: { method: string; params?: unknown }) => Promise<unknown>
|
|
24
|
+
|
|
25
|
+
export type EIP1193Provider = EIP1193Events & {
|
|
26
|
+
address: string
|
|
27
|
+
request: EIP1193RequestFn
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export type EthereumWallet = EIP1193Provider
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* EIP-4361 message fields
|
|
34
|
+
*/
|
|
35
|
+
export type SiweMessage = {
|
|
36
|
+
/**
|
|
37
|
+
* The Ethereum address performing the signing.
|
|
38
|
+
*/
|
|
39
|
+
address: Address
|
|
40
|
+
/**
|
|
41
|
+
* The [EIP-155](https://eips.ethereum.org/EIPS/eip-155) Chain ID to which the session is bound,
|
|
42
|
+
*/
|
|
43
|
+
chainId: number
|
|
44
|
+
/**
|
|
45
|
+
* [RFC 3986](https://www.rfc-editor.org/rfc/rfc3986) authority that is requesting the signing.
|
|
46
|
+
*/
|
|
47
|
+
domain: string
|
|
48
|
+
/**
|
|
49
|
+
* Time when the signed authentication message is no longer valid.
|
|
50
|
+
*/
|
|
51
|
+
expirationTime?: Date | undefined
|
|
52
|
+
/**
|
|
53
|
+
* Time when the message was generated, typically the current time.
|
|
54
|
+
*/
|
|
55
|
+
issuedAt?: Date | undefined
|
|
56
|
+
/**
|
|
57
|
+
* A random string typically chosen by the relying party and used to prevent replay attacks.
|
|
58
|
+
*/
|
|
59
|
+
nonce?: string
|
|
60
|
+
/**
|
|
61
|
+
* Time when the signed authentication message will become valid.
|
|
62
|
+
*/
|
|
63
|
+
notBefore?: Date | undefined
|
|
64
|
+
/**
|
|
65
|
+
* A system-specific identifier that may be used to uniquely refer to the sign-in request.
|
|
66
|
+
*/
|
|
67
|
+
requestId?: string | undefined
|
|
68
|
+
/**
|
|
69
|
+
* A list of information or references to information the user wishes to have resolved as part of authentication by the relying party.
|
|
70
|
+
*/
|
|
71
|
+
resources?: string[] | undefined
|
|
72
|
+
/**
|
|
73
|
+
* [RFC 3986](https://www.rfc-editor.org/rfc/rfc3986#section-3.1) URI scheme of the origin of the request.
|
|
74
|
+
*/
|
|
75
|
+
scheme?: string | undefined
|
|
76
|
+
/**
|
|
77
|
+
* A human-readable ASCII assertion that the user will sign.
|
|
78
|
+
*/
|
|
79
|
+
statement?: string | undefined
|
|
80
|
+
/**
|
|
81
|
+
* [RFC 3986](https://www.rfc-editor.org/rfc/rfc3986) URI referring to the resource that is the subject of the signing (as in the subject of a claim).
|
|
82
|
+
*/
|
|
83
|
+
uri: string
|
|
84
|
+
/**
|
|
85
|
+
* The current version of the SIWE Message.
|
|
86
|
+
*/
|
|
87
|
+
version: '1'
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export type EthereumSignInInput = SiweMessage
|
|
91
|
+
|
|
92
|
+
export function getAddress(address: string): Address {
|
|
93
|
+
if (!/^0x[a-fA-F0-9]{40}$/.test(address)) {
|
|
94
|
+
throw new Error(`@supabase/auth-js: Address "${address}" is invalid.`)
|
|
95
|
+
}
|
|
96
|
+
return address.toLowerCase() as Address
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function fromHex(hex: Hex): number {
|
|
100
|
+
return parseInt(hex, 16)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function toHex(value: string): Hex {
|
|
104
|
+
const bytes = new TextEncoder().encode(value)
|
|
105
|
+
const hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, '0')).join('')
|
|
106
|
+
return ('0x' + hex) as Hex
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Creates EIP-4361 formatted message.
|
|
111
|
+
*/
|
|
112
|
+
export function createSiweMessage(parameters: SiweMessage): string {
|
|
113
|
+
const {
|
|
114
|
+
chainId,
|
|
115
|
+
domain,
|
|
116
|
+
expirationTime,
|
|
117
|
+
issuedAt = new Date(),
|
|
118
|
+
nonce,
|
|
119
|
+
notBefore,
|
|
120
|
+
requestId,
|
|
121
|
+
resources,
|
|
122
|
+
scheme,
|
|
123
|
+
uri,
|
|
124
|
+
version,
|
|
125
|
+
} = parameters
|
|
126
|
+
|
|
127
|
+
// Validate fields
|
|
128
|
+
{
|
|
129
|
+
if (!Number.isInteger(chainId))
|
|
130
|
+
throw new Error(
|
|
131
|
+
`@supabase/auth-js: Invalid SIWE message field "chainId". Chain ID must be a EIP-155 chain ID. Provided value: ${chainId}`
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
if (!domain)
|
|
135
|
+
throw new Error(
|
|
136
|
+
`@supabase/auth-js: Invalid SIWE message field "domain". Domain must be provided.`
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
if (nonce && nonce.length < 8)
|
|
140
|
+
throw new Error(
|
|
141
|
+
`@supabase/auth-js: Invalid SIWE message field "nonce". Nonce must be at least 8 characters. Provided value: ${nonce}`
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
if (!uri)
|
|
145
|
+
throw new Error(`@supabase/auth-js: Invalid SIWE message field "uri". URI must be provided.`)
|
|
146
|
+
|
|
147
|
+
if (version !== '1')
|
|
148
|
+
throw new Error(
|
|
149
|
+
`@supabase/auth-js: Invalid SIWE message field "version". Version must be '1'. Provided value: ${version}`
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
if (parameters.statement?.includes('\n'))
|
|
153
|
+
throw new Error(
|
|
154
|
+
`@supabase/auth-js: Invalid SIWE message field "statement". Statement must not include '\\n'. Provided value: ${parameters.statement}`
|
|
155
|
+
)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Construct message
|
|
159
|
+
const address = getAddress(parameters.address)
|
|
160
|
+
const origin = scheme ? `${scheme}://${domain}` : domain
|
|
161
|
+
const statement = parameters.statement ? `${parameters.statement}\n` : ''
|
|
162
|
+
const prefix = `${origin} wants you to sign in with your Ethereum account:\n${address}\n\n${statement}`
|
|
163
|
+
|
|
164
|
+
let suffix = `URI: ${uri}\nVersion: ${version}\nChain ID: ${chainId}${
|
|
165
|
+
nonce ? `\nNonce: ${nonce}` : ''
|
|
166
|
+
}\nIssued At: ${issuedAt.toISOString()}`
|
|
167
|
+
|
|
168
|
+
if (expirationTime) suffix += `\nExpiration Time: ${expirationTime.toISOString()}`
|
|
169
|
+
if (notBefore) suffix += `\nNot Before: ${notBefore.toISOString()}`
|
|
170
|
+
if (requestId) suffix += `\nRequest ID: ${requestId}`
|
|
171
|
+
if (resources) {
|
|
172
|
+
let content = '\nResources:'
|
|
173
|
+
for (const resource of resources) {
|
|
174
|
+
if (!resource || typeof resource !== 'string')
|
|
175
|
+
throw new Error(
|
|
176
|
+
`@supabase/auth-js: Invalid SIWE message field "resources". Every resource must be a valid string. Provided value: ${resource}`
|
|
177
|
+
)
|
|
178
|
+
content += `\n- ${resource}`
|
|
179
|
+
}
|
|
180
|
+
suffix += content
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return `${prefix}\n${suffix}`
|
|
184
|
+
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"solana.d.ts","sourceRoot":"","sources":["../../../src/lib/solana.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,oBAAY,gBAAgB,GAAG,GAAG,MAAM,IAAI,MAAM,EAAE,CAAA;AAEpD;;;;;;;GAOG;AACH,oBAAY,eAAe,GAAG,SAAS,gBAAgB,EAAE,CAAA;AAEzD;;;;;;;;;;;GAWG;AACH,oBAAY,aAAa,GAAG,OAAO,CAAA;AAEnC;;;;;;GAMG;AACH,oBAAY,UAAU,GAAG,cAAc,SAAS,GAAG,MAAM,GAAG,KAAK,GAAG,KAAK,WAAW,MAAM,EAAE,CAAA;AAE5F;;;;;;;;;;;;GAYG;AACH,MAAM,WAAW,aAAa;IAC5B,+DAA+D;IAC/D,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;IAExB,yEAAyE;IACzE,QAAQ,CAAC,SAAS,EAAE,UAAU,CAAA;IAE9B;;;;OAIG;IACH,QAAQ,CAAC,MAAM,EAAE,eAAe,CAAA;IAEhC;;;;OAIG;IACH,QAAQ,CAAC,QAAQ,EAAE,eAAe,CAAA;IAElC,0GAA0G;IA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@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"solana.js","sourceRoot":"","sources":["../../../src/lib/solana.ts"],"names":[],"mappings":";AAAA,2HAA2H"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"solana.d.ts","sourceRoot":"","sources":["../../../src/lib/solana.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,oBAAY,gBAAgB,GAAG,GAAG,MAAM,IAAI,MAAM,EAAE,CAAA;AAEpD;;;;;;;GAOG;AACH,oBAAY,eAAe,GAAG,SAAS,gBAAgB,EAAE,CAAA;AAEzD;;;;;;;;;;;GAWG;AACH,oBAAY,aAAa,GAAG,OAAO,CAAA;AAEnC;;;;;;GAMG;AACH,oBAAY,UAAU,GAAG,cAAc,SAAS,GAAG,MAAM,GAAG,KAAK,GAAG,KAAK,WAAW,MAAM,EAAE,CAAA;AAE5F;;;;;;;;;;;;GAYG;AACH,MAAM,WAAW,aAAa;IAC5B,+DAA+D;IAC/D,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;IAExB,yEAAyE;IACzE,QAAQ,CAAC,SAAS,EAAE,UAAU,CAAA;IAE9B;;;;OAIG;IACH,QAAQ,CAAC,MAAM,EAAE,eAAe,CAAA;IAEhC;;;;OAIG;IACH,QAAQ,CAAC,QAAQ,EAAE,eAAe,CAAA;IAElC,0GAA0G;IAC1G,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAA;IAEvB,qFAAqF;IACrF,QAAQ,CAAC,IAAI,CAAC,EAAE,UAAU,CAAA;CAC3B;AAED,4BAA4B;AAC5B,MAAM,WAAW,iBAAiB;IAChC;;;OAGG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAA;IAExB;;;OAGG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAA;IAEzB;;;OAGG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAA;IAE3B;;;OAGG;IACH,QAAQ,CAAC,GAAG,CAAC,EAAE,MAAM,CAAA;IAErB;;;OAGG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAA;IAEzB;;;OAGG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAA;IAEzB;;;OAGG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAA;IAEvB;;;OAGG;IACH,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;IAE1B;;;OAGG;IACH,QAAQ,CAAC,cAAc,CAAC,EAAE,MAAM,CAAA;IAEhC;;;OAGG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAA;IAE3B;;;OAGG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAA;IAE3B;;;OAGG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,SAAS,MAAM,EAAE,CAAA;CACvC;AAED,4BAA4B;AAC5B,MAAM,WAAW,kBAAkB;IACjC;;;OAGG;IACH,QAAQ,CAAC,OAAO,EAAE,aAAa,CAAA;IAE/B;;;OAGG;IACH,QAAQ,CAAC,aAAa,EAAE,UAAU,CAAA;IAElC;;;OAGG;IACH,QAAQ,CAAC,SAAS,EAAE,UAAU,CAAA;IAE9B;;;OAGG;IACH,QAAQ,CAAC,aAAa,CAAC,EAAE,SAAS,CAAA;CACnC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"solana.js","sourceRoot":"","sources":["../../../src/lib/solana.ts"],"names":[],"mappings":"AAAA,2HAA2H"}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|