1
1
import { Signers as CoreSigners , Envelope , Wallet } from '@0xsequence/wallet-core'
2
- import { Attestation , Payload , SessionConfig , Signature as SequenceSignature } from '@0xsequence/wallet-primitives'
2
+ import {
3
+ Attestation ,
4
+ Payload ,
5
+ SessionConfig ,
6
+ Signature as SequenceSignature ,
7
+ Config ,
8
+ } from '@0xsequence/wallet-primitives'
3
9
import { Address , Bytes , Hex , Provider , RpcTransport } from 'ox'
4
10
import { SessionController } from '../session/index.js'
5
11
import { IdentityHandler , identityTypeToHex } from './handlers/identity.js'
6
12
import { Shared } from './manager.js'
7
13
import { AuthCodePkceHandler } from './handlers/authcode-pkce.js'
8
14
import { IdentityType } from '../identity/index.js'
9
- import { isSignature } from '../../../core/dist/envelope.js'
10
15
11
16
export type AuthorizeImplicitSessionArgs = {
12
17
target : string
@@ -58,14 +63,11 @@ export class Sessions {
58
63
return controller . getTopology ( )
59
64
}
60
65
61
- async authorizeImplicitSession (
66
+ async prepareAuthorizeImplicitSession (
62
67
walletAddress : Address . Address ,
63
68
sessionAddress : Address . Address ,
64
69
args : AuthorizeImplicitSessionArgs ,
65
- ) : Promise < {
66
- attestation : Attestation . Attestation
67
- signature : SequenceSignature . RSY
68
- } > {
70
+ ) : Promise < string > {
69
71
const topology = await this . getSessionTopology ( walletAddress )
70
72
const identitySignerAddress = SessionConfig . getIdentitySigner ( topology )
71
73
if ( ! identitySignerAddress ) {
@@ -80,7 +82,7 @@ export class Sessions {
80
82
throw new Error ( 'No identity handler found' )
81
83
}
82
84
83
- // Create the digest to sign
85
+ // Create the attestation to sign
84
86
let identityType : IdentityType | undefined
85
87
let issuerHash : Hex . Hex = '0x'
86
88
let audienceHash : Hex . Hex = '0x'
@@ -103,52 +105,62 @@ export class Sessions {
103
105
redirectUrl : args . target ,
104
106
} ,
105
107
}
106
- const attestationHash = Attestation . hash ( attestation )
107
- const walletStatus = await this . getCoreWallet ( walletAddress ) . getStatus ( )
108
- const envelope : Envelope . Envelope < Payload . Digest > = {
108
+ // Fake the configuration with the single required signer
109
+ const configuration : Config . Config = {
110
+ threshold : 1n ,
111
+ checkpoint : 0n ,
112
+ topology : {
113
+ type : 'signer' ,
114
+ address : identitySignerAddress ,
115
+ weight : 1n ,
116
+ } ,
117
+ }
118
+ const envelope : Envelope . Envelope < Payload . SessionImplicitAuthorize > = {
109
119
payload : {
110
- type : 'digest' ,
111
- digest : Hex . fromBytes ( attestationHash ) ,
120
+ type : 'session-implicit-authorize' ,
121
+ sessionAddress,
122
+ attestation,
112
123
} ,
113
124
wallet : walletAddress ,
114
125
chainId : 0n ,
115
- configuration : walletStatus . configuration ,
126
+ configuration,
116
127
}
117
128
118
129
// Request the signature from the identity handler
119
- const requestId = await this . shared . modules . signatures . request ( envelope , 'sign-digest ' , {
130
+ return this . shared . modules . signatures . request ( envelope , 'session-implicit-authorize ' , {
120
131
origin : args . target ,
121
132
} )
122
- let signatureRequest = await this . shared . modules . signatures . get ( requestId )
123
- const identitySigner = signatureRequest . signers . find ( ( s ) => s . address === identitySignerAddress )
124
- if ( ! identitySigner || ( identitySigner . status !== 'actionable' && identitySigner . status !== 'ready' ) ) {
125
- throw new Error ( `Identity signer not found or not ready: ${ identitySigner ?. status } ` )
126
- }
127
- const handled = await identitySigner . handle ( )
128
- if ( ! handled ) {
129
- throw new Error ( 'Failed to handle identity handler' )
133
+ }
134
+
135
+ async completeAuthorizeImplicitSession ( requestId : string ) : Promise < {
136
+ attestation : Attestation . Attestation
137
+ signature : SequenceSignature . RSY
138
+ } > {
139
+ // Get the updated signature request
140
+ const signatureRequest = await this . shared . modules . signatures . get ( requestId )
141
+ if (
142
+ signatureRequest . action !== 'session-implicit-authorize' ||
143
+ ! Payload . isSessionImplicitAuthorize ( signatureRequest . envelope . payload )
144
+ ) {
145
+ throw new Error ( 'Invalid action' )
130
146
}
131
- // Get the updated signature request. Then delete it, we don't need it anymore
132
- signatureRequest = await this . shared . modules . signatures . get ( requestId )
133
- await this . shared . modules . signatures . cancel ( requestId )
134
- // Find the handler signature
135
- const signatures = signatureRequest . envelope . signatures . filter (
136
- ( sig ) => isSignature ( sig ) && sig . address === identitySignerAddress ,
137
- )
138
- if ( signatures . length === 0 ) {
139
- throw new Error ( 'No signatures found' )
147
+
148
+ if ( ! Envelope . isSigned ( signatureRequest . envelope ) || ! Envelope . reachedThreshold ( signatureRequest . envelope ) ) {
149
+ throw new Error ( 'Envelope not signed or threshold not reached' )
140
150
}
141
- const signature = signatures [ 0 ]
142
- if ( ! signature ) {
143
- throw new Error ( 'No signature found' )
151
+
152
+ // Find any valid signature
153
+ const signature = signatureRequest . envelope . signatures [ 0 ]
154
+ if ( ! signature || ! Envelope . isSignature ( signature ) ) {
155
+ throw new Error ( 'No valid signature found' )
144
156
}
145
157
if ( signature . signature . type !== 'hash' ) {
146
158
// Should never happen
147
159
throw new Error ( 'Unsupported signature type' )
148
160
}
149
161
150
162
return {
151
- attestation,
163
+ attestation : signatureRequest . envelope . payload . attestation ,
152
164
signature : signature . signature ,
153
165
}
154
166
}
0 commit comments