diff --git a/examples/index.ts b/examples/index.ts index 73b6b70..5f91f77 100644 --- a/examples/index.ts +++ b/examples/index.ts @@ -50,8 +50,7 @@ const accountAddressInputElement = document.getElementById( document.getElementById('login-btn')!.onclick = async () => { clearResults() const result = await sdk.request({ - loginWithoutChallenge: {}, - reset: { accounts: true }, + loginWithChallenge: { challenge: 'test' }, }) displayResults(result) diff --git a/lib/IO/decode-wallet-response.ts b/lib/IO/decode-wallet-response.ts index 302428b..eca5c1f 100644 --- a/lib/IO/decode-wallet-response.ts +++ b/lib/IO/decode-wallet-response.ts @@ -9,11 +9,9 @@ export const decodeWalletResponse = ( switch (key) { case RequestTypeSchema.auth.value: { const { discriminator: authDiscriminator, ...auth } = value - if (authDiscriminator === 'usePersona') { - return { ...acc, persona: auth.persona } - } else if (authDiscriminator === 'loginWithChallenge') { - const { persona, ...signedChallenge } = auth - return { ...acc, persona, signedChallenge } + if (authDiscriminator === 'loginWithChallenge') { + const { persona, proof, challenge } = auth + return { ...acc, persona, challenge, proof } } else { return { ...acc, persona: auth.persona } } diff --git a/lib/IO/request-items/login.ts b/lib/IO/request-items/login.ts index a02b3bb..698f4e0 100644 --- a/lib/IO/request-items/login.ts +++ b/lib/IO/request-items/login.ts @@ -1,5 +1,6 @@ import { - AuthLoginRequestItem, + AuthLoginWithChallengeRequestItem, + AuthLoginWithoutChallengeRequestItem, AuthLoginWithChallengeRequestResponseItem, AuthLoginWithoutChallengeRequestResponseItem, Persona, @@ -8,7 +9,7 @@ import { export type Login = { WithoutChallenge: { wallet: { - request: AuthLoginRequestItem + request: AuthLoginWithoutChallengeRequestItem response: AuthLoginWithoutChallengeRequestResponseItem } method: { @@ -20,14 +21,15 @@ export type Login = { } WithChallenge: { wallet: { - request: AuthLoginRequestItem + request: AuthLoginWithChallengeRequestItem response: AuthLoginWithChallengeRequestResponseItem } method: { output: { persona: Persona - signedChallenge: { - challenge: string + challenge: string + proof: { + curve: string publicKey: string signature: string } @@ -49,13 +51,13 @@ export const login = { (input: I extends NotAllowedKeys ? never : I) => ({ ...input, loginWithoutChallenge: { - discriminator: 'login', + discriminator: 'loginWithoutChallenge', }, }), withChallenge: (challenge: string) => (input: I extends NotAllowedKeys ? never : I) => ({ ...input, - loginWithChallenge: { discriminator: 'login', challenge }, + loginWithChallenge: { discriminator: 'loginWithChallenge', challenge }, }), } diff --git a/lib/IO/schemas.ts b/lib/IO/schemas.ts index b9d5d1e..52b4293 100644 --- a/lib/IO/schemas.ts +++ b/lib/IO/schemas.ts @@ -217,12 +217,21 @@ export type AuthUsePersonaRequestResponseItem = z.infer< typeof AuthUsePersonaRequestResponseItemSchema > -const AuthLoginRequestItemSchema = object({ - discriminator: literal('login'), - challenge: string().optional(), +const AuthLoginWithChallengeRequestItemSchema = object({ + discriminator: literal('loginWithChallenge'), + challenge: string(), }) -export type AuthLoginRequestItem = z.infer +const AuthLoginWithoutChallengeRequestItemSchema = object({ + discriminator: literal('loginWithoutChallenge'), +}) + +export type AuthLoginWithChallengeRequestItem = z.infer< + typeof AuthLoginWithChallengeRequestItemSchema +> +export type AuthLoginWithoutChallengeRequestItem = z.infer< + typeof AuthLoginWithoutChallengeRequestItemSchema +> const AuthLoginWithoutChallengeRequestResponseItemSchema = object({ discriminator: literal('loginWithoutChallenge'), @@ -233,12 +242,17 @@ export type AuthLoginWithoutChallengeRequestResponseItem = z.infer< typeof AuthLoginWithoutChallengeRequestResponseItemSchema > +const ProofSchema = object({ + publicKey: string(), + signature: string(), + curve: string(), +}) + const AuthLoginWithChallengeRequestResponseItemSchema = object({ discriminator: literal('loginWithChallenge'), persona: PersonaSchema, challenge: string(), - publicKey: string(), - signature: string(), + proof: ProofSchema, }) export type AuthLoginWithChallengeRequestResponseItem = z.infer< @@ -256,7 +270,8 @@ export type AuthLoginRequestResponseItem = z.infer< export const AuthRequestItemSchema = union([ AuthUsePersonaRequestItemSchema, - AuthLoginRequestItemSchema, + AuthLoginWithChallengeRequestItemSchema, + AuthLoginWithoutChallengeRequestItemSchema, ]) export type AuthRequestItem = z.infer diff --git a/lib/IO/transform-method-input.ts b/lib/IO/transform-method-input.ts index 41b4751..36d2123 100644 --- a/lib/IO/transform-method-input.ts +++ b/lib/IO/transform-method-input.ts @@ -88,21 +88,23 @@ export const transformMethodInput = (input: I) => case requestMethodRequestType.loginWithoutChallenge: return { ...acc, - auth: { ...value, discriminator: 'login' }, + auth: { discriminator: 'loginWithoutChallenge' }, discriminator: 'authorizedRequest', } - case requestMethodRequestType.usePersona: + case requestMethodRequestType.loginWithChallenge: { + const { challenge } = value return { ...acc, - auth: { ...value, discriminator: 'usePersona' }, + auth: { challenge, discriminator: 'loginWithChallenge' }, discriminator: 'authorizedRequest', } + } - case requestMethodRequestType.loginWithChallenge: + case requestMethodRequestType.usePersona: return { ...acc, - auth: value, + auth: { ...value, discriminator: 'usePersona' }, discriminator: 'authorizedRequest', } diff --git a/lib/__tests__/decode-wallet-response.spec.ts b/lib/__tests__/decode-wallet-response.spec.ts index 97a97bc..4a34037 100644 --- a/lib/__tests__/decode-wallet-response.spec.ts +++ b/lib/__tests__/decode-wallet-response.spec.ts @@ -52,8 +52,11 @@ describe('decodeWalletResponse', () => { label: 'personaLabel', }, challenge: 'challenge', - publicKey: 'publicKey', - signature: 'signature', + proof: { + publicKey: 'publicKey', + signature: 'signature', + curve: '123', + }, }, }, expected: { @@ -61,10 +64,11 @@ describe('decodeWalletResponse', () => { identityAddress: 'addr_xxx', label: 'personaLabel', }, - signedChallenge: { - challenge: 'challenge', + challenge: 'challenge', + proof: { publicKey: 'publicKey', signature: 'signature', + curve: '123', }, }, }, diff --git a/lib/__tests__/schemas.spec.ts b/lib/__tests__/schemas.spec.ts index 0f14c45..d39a36c 100644 --- a/lib/__tests__/schemas.spec.ts +++ b/lib/__tests__/schemas.spec.ts @@ -34,11 +34,11 @@ describe('schemas', () => { describe('Auth', () => { it('AuthRequestItemSchema', () => { AuthRequestItemSchema.parse({ - discriminator: 'login', - challenge: 'asd', + discriminator: 'loginWithoutChallenge', }) AuthRequestItemSchema.parse({ - discriminator: 'login', + discriminator: 'loginWithChallenge', + challenge: 'challenge', }) }) @@ -49,9 +49,8 @@ describe('schemas', () => { identityAddress: 'addr_xx', label: 'personaLabel', }, + proof: { publicKey: 'key', signature: 'signature', curve: '12' }, challenge: 'challenge', - publicKey: 'key', - signature: 'signature', }) AuthLoginRequestResponseItemSchema.parse({ discriminator: 'loginWithoutChallenge', diff --git a/lib/__tests__/transform-method-input.spec.ts b/lib/__tests__/transform-method-input.spec.ts index 3a9b77e..43ca9d2 100644 --- a/lib/__tests__/transform-method-input.spec.ts +++ b/lib/__tests__/transform-method-input.spec.ts @@ -169,7 +169,7 @@ describe('transformMethodInput', () => { expected: { discriminator: 'authorizedRequest', auth: { - discriminator: 'login', + discriminator: 'loginWithChallenge', challenge: 'abc', }, }, @@ -179,7 +179,7 @@ describe('transformMethodInput', () => { expected: { discriminator: 'authorizedRequest', auth: { - discriminator: 'login', + discriminator: 'loginWithoutChallenge', }, }, }, diff --git a/lib/create-methods.ts b/lib/create-methods.ts index b0a2032..2652de1 100644 --- a/lib/create-methods.ts +++ b/lib/create-methods.ts @@ -12,6 +12,7 @@ import { transformMethodInput } from './IO/transform-method-input' import { createMessage } from './messages/create-message' import { CallbackFns } from './messages/events/_types' import { Method, requestType } from './_types' +import { AppLogger } from './wallet-sdk' type SendWalletInteraction = ( callbackFns: Partial @@ -21,7 +22,8 @@ type SendWalletInteraction = ( export const createMethods = ( metadata: Metadata, - sendMessageToWallet: SendWalletInteraction + sendMessageToWallet: SendWalletInteraction, + logger?: AppLogger ) => { const request = < Input extends Method['request']['input'], @@ -38,7 +40,7 @@ export const createMethods = ( ) => transformMethodInput(input) .andThen(createMessage(metadata)) - .asyncAndThen(validateWalletRequest) + .asyncAndThen((message) => validateWalletRequest(message, logger)) .andThen(sendMessageToWallet(callbackFns)) .andThen(validateWalletResponse) .map((response) => response.items) diff --git a/lib/methods/request.ts b/lib/methods/request.ts index 1db0144..37ab9d8 100644 --- a/lib/methods/request.ts +++ b/lib/methods/request.ts @@ -29,7 +29,7 @@ type RequestItems = { // [requestMethodRequestType.ongoingAccountsWithProofOfOwnership]: OngoingAccounts['WithProofOfOwnership'] [requestMethodRequestType.usePersona]: UsePersona [requestMethodRequestType.loginWithoutChallenge]: Login['WithoutChallenge'] - // [requestMethodRequestType.loginWithChallenge]: Login['WithChallenge'] + [requestMethodRequestType.loginWithChallenge]: Login['WithChallenge'] [requestMethodRequestType.oneTimePersonaData]: OneTimePersonaData [requestMethodRequestType.ongoingPersonaData]: OngoingPersonaData } diff --git a/lib/wallet-sdk.ts b/lib/wallet-sdk.ts index 152270c..3d22093 100644 --- a/lib/wallet-sdk.ts +++ b/lib/wallet-sdk.ts @@ -37,7 +37,8 @@ export const WalletSdk = ({ const methods = createMethods( { networkId, dAppDefinitionAddress }, - createSendMessage(messageClient.subjects, logger) + createSendMessage(messageClient.subjects, logger), + logger ) return {