import { customAlphabet } from 'nanoid' ; import Web3 from 'web3' ; const nanoid = customAlphabet ( '0123456789abcdefghijklmnopqrstuvwxyz' , 12 ) ; import { UserInputError } from 'common/errors' ; import { GQLCryptoWalletSignaturePurpose , MutationToGenerateSigningMessageResolver , } from 'definitions' ; const resolver = async ( _ , { address } , { viewer , dataSources : { atomService } } ) => { if ( ! address || ! Web3 . utils . isAddress ( address ) ) { throw new UserInputError ( 'address is invalid' ) ; } const table = 'crypto_wallet_signature' ; const lastUsed = await atomService . findFirst ( { table , where : { address , purpose : GQLCryptoWalletSignaturePurpose . signup , } , orderBy : [ { column : 'id' , order : 'desc' } ] , } ) ; const nonce = nanoid ( ) ; const createdAt = new Date ( ) ; const expiredAt = new Date ( + createdAt + 10 * 60e3 ) ; const signingMessage = `Matters.News wants you to sign in with your Ethereum account:
${ address }

I accept the Matters.News Terms of Service: https://matters.news/tos

URI: https://matters.news/login
Version: 1
Chain ID: 1
Nonce: ${ nonce }
Issued At: ${ createdAt . toISOString ( ) }
Expiration Time: ${ expiredAt . toISOString ( ) }
Resources:
- https://matters.news/community
- ...` ; console . log ( new Date ( ) , 'lastUsed:' , lastUsed ) ; const purpose = lastUsed ?. userId ? GQLCryptoWalletSignaturePurpose . login : GQLCryptoWalletSignaturePurpose . signup ; const res = await atomService . create ( { table , data : { address , signedMessage : signingMessage , nonce , purpose , createdAt , expiredAt , } , } ) ; console . log ( 'created signup/login:' , res ) ; return { nonce , signingMessage , createdAt , expiredAt , } ; } ; export default resolver ;