|
2 years ago | |
---|---|---|
assets | 2 years ago | |
example-dapp | 2 years ago | |
README.md | 2 years ago |
Sign In With Solana (SIWS) is a new feature that lets applications authenticate their users and prove ownership of their addresses. The feature aims at standardizing message formats to improve the authentication UX and security, and replaces the traditionally clunky connect
+ signMessage
flow with a one-click signIn
method.
Centralised entities like Google, Facebook and X dictate our digital identities. They define our digital identifiers (usernames, email addresses) and exercise control over our data, reputations, and digital footprints. This presents two significant concerns:
A decentralised authentication approach widely used to tackle the above revolves around signing off-chain messages. This approach is safer than entering passwords and entrusting authentication control to the dapp, however, it faces some significant challenges:
Sign-In With Solana offers a comprehensive solution to these challenges and more. The technical specification for SIWS is modeled after EIP-4361 (Sign In With Ethereum) but extends beyond it’s capabilities. SIWS shifts the responsibility of message construction from dapps to the wallet, resulting in consistent, user-friendly inlterfaces and enhanced end-user security.
Additionally, SIWS standardises the message format, which enables wallets to scrutinize message data to ensure its legitimacy or raise red flags for suspicious activity. Domain binding is a key feature of SIWS, enabling wallets to alert users if a website is impersonating another entity
To make a sign-in request, dapps do not need to construct a message themselves, unlike EIP-4361 or legacy Solana messages. Rather, dapps construct the signInInput
object containing a set of standard message parameters. All these fields are optional strings. Dapps can optionally send a minimal (empty) signInInput
object to the wallet.
domain
: Optional EIP-4361 domain requesting the sign-in. The domain includes the subdomain, top-level domain and the port number if present. If not provided, the wallet must determine the domain to include in the message.address
: Optional Solana address performing the sign-in. The address is case-sensitive. If not provided, the wallet must determine the Address to include in the message.statement
: Optional EIP-4361 Statement. The statement is a human readable string and should not have new-line characters (\n
). If not provided, the wallet must not include Statement in the message.uri
: Optional EIP-4361 URI. The URL that is requesting the sign-in. If not provided, the wallet must not include URI in the message.version
: Optional EIP-4361 version, hardcoded to 1
if provided in the current spec. If not provided, the wallet must not include Version in the message.chainId
: Optional EIP-4361 Chain ID. The chainId can be one of the following: mainnet
, testnet
, devnet
, localnet
, solana:mainnet
, solana:testnet
, solana:devnet
. If not provided, the wallet must not include Chain ID in the message.nonce
: Optional EIP-4361 Nonce. It should be an alphanumeric string containing a minimum of 8 characters. If not provided, the wallet must not include Nonce in the message.issuedAt
: Optional ISO 8601 datetime string. This represents the time at which the sign-in request was issued to the wallet. If not provided, the wallet must not include Issued At in the message.expirationTime
: Optional ISO 8601 datetime string. This represents the time at which the sign-in request should expire. If not provided, the wallet must not include Expiration Time in the message.notBefore
: Optional ISO 8601 datetime string. This represents the time at which the sign-in request becomes valid. If not provided, the wallet must not include Not Before in the message.requestId
: Optional EIP-4361 Request ID. This field can be used to provide uniqueness or an identifier to the sign-in request, and can be used by dapps as an extra layer of request verification. If not provided, the wallet must not include Request ID in the message.resources
: Optional EIP-4361 Resources. Usually a list of references in the form of URIs that the dapp wants the user to be aware of. These URIs should be separated by \n-
, ie, URIs in new lines starting with the character -
. If not provided, the wallet must not include Resources in the message.
Once the user is successfully signed-in, the wallet returns back the signInOutput
object to the dapp, which can be used for verifying the sign-in process.
account
[WalletAccount
]: Account that was signed in. The address of the account may be different from the provided input Address.signedMessage
[Uint8Array
]: Message bytes that were signed. The wallet is responsible for constructing this message using the signInInput
.signature
[Uint8Array
]: Message signature produced. If the signature type is provided, the signature must be Ed25519.signatureType
[ed25519
]: Optional type of the message signature produced. If not provided, the signature must be Ed25519.The Sign-In With Solana message constructed by the wallet using signInInput
should follow the sign-in-with-solana
Augment Backus–Naur Form expression:
sign-in-with-solana =
message-domain %s" wants you to sign in with your Solana account:" LF
message-address
[ LF LF message-statement ]
[ LF advanced-fields ]
advanced-fields =
[ LF %s"URI: " message-uri ]
[ LF %s"Version: " message-version ]
[ LF %s"Chain ID: " message-chain-id ]
[ LF %s"Nonce: " message-nonce ]
[ LF %s"Issued At: " message-issued-at ]
[ LF %s"Expiration Time: " message-expiration-time ]
[ LF %s"Not Before: " message-not-before ]
[ LF %s"Request ID: " message-request-id ]
[ LF %s"Resources:" message-resources ]
message-domain = authority
message-address = 32*44( %x31-39 / %x41-48 / %x4A-4E / %x50-5A / %x61-6B / %x6D-7A )
message-statement = 1*( reserved / unreserved / " " )
message-uri = URI
message-version = "1"
message-chain-id = %s"mainnet" / %s"testnet" / %s"devnet" / %s"localnet" / %s"solana:mainnet" / %s"solana:testnet" / %s"solana:devnet"
message-nonce = 8*( ALPHA / DIGIT )
message-issued-at = date-time
message-expiration-time = date-time
message-not-before = date-time
message-request-id = *pchar
message-resources = *( LF "- " URI )
If the dapp sends an empty signInInput
, the wallet should construct a minimal sign-in message using the requesting domain and address as follows:
${domain} wants you to sign in with your Solana account:
${address}
This is an informal format in which the wallet should construct the message if all optional fields are provided in signInInput
:
${domain} wants you to sign in with your Solana account:
${address}
${statement}
URI: ${uri}
Version: ${version}
Chain ID: ${chain-id}
Nonce: ${nonce}
Issued At: ${issued-at}
Expiration Time: ${expiration-time}
Not Before: ${not-before}
Request ID: ${request-id}
Resources:
- ${resources[0]}
- ${resources[1]}
...
- ${resources[n]}
SIWS comes with first-class support in both the Solana Wallet Standard and Solana Wallet Adapter libraries. If your dapp makes use of the Solana Wallet Adapter, migration is easy:
Add this snippet in your ContextProvider
:
import { type SolanaSignInInput } from '@solana/wallet-standard-features';
import { verifySignIn } from '@solana/wallet-standard-util';
const autoSignIn = useCallback(async (adapter: Adapter) => {
if (!('signIn' in adapter)) return true;
// For demo purposes only: The signInInput should be generated server-side.
const input: SolanaSignInInput = {
statement: "Welcome to Drip!"
};
const output = await adapter.signIn(input);
// For demo purposes only: The sign-in verification should happen server-side.
if (!verifySignIn(input, output)) throw new Error('Sign In verification failed!');
return false;
}, []);
This callback function determines whether a user should be auto-connected (returns true
) or prompted to sign-in (returns false
). It does this by:
signIn
feature.
signIn
is not available, this callback returns true
signIn
is available, checking if:
SolanaSignInInput
object is preparedsignIn
methodverifySignIn
methodfalse
and the user is prompted to sign-inPass autoSignIn
to WalletProvider
like so:
<WalletProvider
wallets={wallets}
onError={onError}
autoConnect={autoSignIn}
>
SolanaSignInInput
generation and SolanaSignInOutput
verification should happen server-side. Client-side verification should not be relied upon due to its inherent security flaws. Developers can make use of the asynchronous nature of the autoSignIn
callback to make a request to the server to obtain SIWS params, and make another request to the server to perform verification of the SIWS input and output.SolanaSignInInput
, developers should include as many fields as possible. Each field adds an extra layer of security to protect users against various forms of attacks. Examples of these fields include nonce
, chainId
, issuedAt
, expirationTime
, and requestId
.issuedAt
` — the time at which wahateve, — Phantom currently accepts _+10 min but subject to changenonce
to avoid replay attacks, dapps can also choose to include a unique signature in the requestId
. Once the wallet returns the signed message, dapps can then verify this signature against the state to add an additional, strong layer of security.These dependencies need to be added to your package.json
:
"@solana/wallet-adapter-base": "0.9.23",
"@solana/wallet-adapter-react": "0.15.34",
"@solana/wallet-standard-features": "1.1.0",
"@solana/wallet-standard-util": "1.1.0",
The following configs will be important while testing as some packages like @solana/wallet-adapter-material-ui
and @solana/wallet-adapter-react-ui
use older versions of the react and base packages and may cause conflicts.
"resolutions": {
"@solana/wallet-adapter-react": "0.15.34",
"@solana/wallet-adapter-base": "0.9.23"
},
"overrides": {
"@solana/wallet-adapter-react": "0.15.34",
"@solana/wallet-adapter-base": "0.9.23"
}
https://www.loom.com/share/228d2a4820fb44f69fb10c4fb5f2b55a?sid=21478434-bd5f-438d-a526-0b2439b2cde8