@farcaster/frame-host 0.0.21 → 0.0.23
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/dist/comlink/comlink.d.ts +1 -1
- package/dist/comlink/comlink.js +43 -42
- package/dist/comlink/index.d.ts +1 -1
- package/dist/comlink/index.js +1 -1
- package/dist/comlink/node-adapter.d.ts +1 -1
- package/dist/comlink/node-adapter.js +4 -4
- package/dist/comlink/protocol.d.ts +2 -2
- package/dist/comlink/protocol.js +16 -1
- package/dist/helpers/endpoint.d.ts +5 -5
- package/dist/helpers/endpoint.js +4 -4
- package/dist/helpers/provider.d.ts +2 -2
- package/dist/helpers/provider.js +31 -31
- package/dist/helpers/sdk.d.ts +1 -1
- package/dist/helpers/sdk.js +27 -4
- package/dist/iframe.d.ts +4 -4
- package/dist/iframe.js +6 -6
- package/dist/index.d.ts +4 -4
- package/dist/index.js +4 -4
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types.d.ts +2 -2
- package/package.json +9 -5
- package/src/comlink/comlink.ts +184 -187
- package/src/comlink/index.ts +1 -1
- package/src/comlink/node-adapter.ts +18 -18
- package/src/comlink/protocol.ts +46 -46
- package/src/helpers/endpoint.ts +29 -29
- package/src/helpers/provider.ts +49 -49
- package/src/helpers/sdk.ts +32 -8
- package/src/iframe.ts +25 -25
- package/src/index.ts +4 -4
- package/src/types.ts +5 -5
package/src/comlink/index.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from
|
|
1
|
+
export * from './comlink'
|
|
@@ -4,46 +4,46 @@
|
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import { Endpoint } from
|
|
7
|
+
import type { Endpoint } from './protocol'
|
|
8
8
|
|
|
9
9
|
export interface NodeEndpoint {
|
|
10
|
-
postMessage(message: any, transfer?: any[]): void
|
|
10
|
+
postMessage(message: any, transfer?: any[]): void
|
|
11
11
|
on(
|
|
12
12
|
type: string,
|
|
13
13
|
listener: EventListenerOrEventListenerObject,
|
|
14
14
|
options?: {},
|
|
15
|
-
): void
|
|
15
|
+
): void
|
|
16
16
|
off(
|
|
17
17
|
type: string,
|
|
18
18
|
listener: EventListenerOrEventListenerObject,
|
|
19
19
|
options?: {},
|
|
20
|
-
): void
|
|
21
|
-
start?: () => void
|
|
20
|
+
): void
|
|
21
|
+
start?: () => void
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
export default function nodeEndpoint(nep: NodeEndpoint): Endpoint {
|
|
25
|
-
const listeners = new WeakMap()
|
|
25
|
+
const listeners = new WeakMap()
|
|
26
26
|
return {
|
|
27
27
|
postMessage: nep.postMessage.bind(nep),
|
|
28
28
|
addEventListener: (_, eh) => {
|
|
29
29
|
const l = (data: any) => {
|
|
30
|
-
if (
|
|
31
|
-
eh.handleEvent({ data } as MessageEvent)
|
|
30
|
+
if ('handleEvent' in eh) {
|
|
31
|
+
eh.handleEvent({ data } as MessageEvent)
|
|
32
32
|
} else {
|
|
33
|
-
eh({ data } as MessageEvent)
|
|
33
|
+
eh({ data } as MessageEvent)
|
|
34
34
|
}
|
|
35
|
-
}
|
|
36
|
-
nep.on(
|
|
37
|
-
listeners.set(eh, l)
|
|
35
|
+
}
|
|
36
|
+
nep.on('message', l)
|
|
37
|
+
listeners.set(eh, l)
|
|
38
38
|
},
|
|
39
39
|
removeEventListener: (_, eh) => {
|
|
40
|
-
const l = listeners.get(eh)
|
|
40
|
+
const l = listeners.get(eh)
|
|
41
41
|
if (!l) {
|
|
42
|
-
return
|
|
42
|
+
return
|
|
43
43
|
}
|
|
44
|
-
nep.off(
|
|
45
|
-
listeners.delete(eh)
|
|
44
|
+
nep.off('message', l)
|
|
45
|
+
listeners.delete(eh)
|
|
46
46
|
},
|
|
47
|
-
start: nep.start
|
|
48
|
-
}
|
|
47
|
+
start: nep.start?.bind(nep),
|
|
48
|
+
}
|
|
49
49
|
}
|
package/src/comlink/protocol.ts
CHANGED
|
@@ -9,13 +9,13 @@ export interface EventSource {
|
|
|
9
9
|
type: string,
|
|
10
10
|
listener: EventListenerOrEventListenerObject,
|
|
11
11
|
options?: {},
|
|
12
|
-
): void
|
|
12
|
+
): void
|
|
13
13
|
|
|
14
14
|
removeEventListener(
|
|
15
15
|
type: string,
|
|
16
16
|
listener: EventListenerOrEventListenerObject,
|
|
17
17
|
options?: {},
|
|
18
|
-
): void
|
|
18
|
+
): void
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
export interface PostMessageWithOrigin {
|
|
@@ -23,83 +23,83 @@ export interface PostMessageWithOrigin {
|
|
|
23
23
|
message: any,
|
|
24
24
|
targetOrigin: string,
|
|
25
25
|
transfer?: Transferable[],
|
|
26
|
-
): void
|
|
26
|
+
): void
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
export interface Endpoint extends EventSource {
|
|
30
|
-
postMessage(message: any, transfer?: Transferable[]): void
|
|
30
|
+
postMessage(message: any, transfer?: Transferable[]): void
|
|
31
31
|
|
|
32
|
-
start?: () => void
|
|
32
|
+
start?: () => void
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
export
|
|
36
|
-
RAW =
|
|
37
|
-
PROXY =
|
|
38
|
-
THROW =
|
|
39
|
-
HANDLER =
|
|
35
|
+
export enum WireValueType {
|
|
36
|
+
RAW = 'RAW',
|
|
37
|
+
PROXY = 'PROXY',
|
|
38
|
+
THROW = 'THROW',
|
|
39
|
+
HANDLER = 'HANDLER',
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
export interface RawWireValue {
|
|
43
|
-
id?: string
|
|
44
|
-
type: WireValueType.RAW
|
|
45
|
-
value: {}
|
|
43
|
+
id?: string
|
|
44
|
+
type: WireValueType.RAW
|
|
45
|
+
value: {}
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
export interface HandlerWireValue {
|
|
49
|
-
id?: string
|
|
50
|
-
type: WireValueType.HANDLER
|
|
51
|
-
name: string
|
|
52
|
-
value: unknown
|
|
49
|
+
id?: string
|
|
50
|
+
type: WireValueType.HANDLER
|
|
51
|
+
name: string
|
|
52
|
+
value: unknown
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
-
export type WireValue = RawWireValue | HandlerWireValue
|
|
55
|
+
export type WireValue = RawWireValue | HandlerWireValue
|
|
56
56
|
|
|
57
|
-
export type MessageID = string
|
|
57
|
+
export type MessageID = string
|
|
58
58
|
|
|
59
|
-
export
|
|
60
|
-
GET =
|
|
61
|
-
SET =
|
|
62
|
-
APPLY =
|
|
63
|
-
CONSTRUCT =
|
|
64
|
-
ENDPOINT =
|
|
65
|
-
RELEASE =
|
|
59
|
+
export enum MessageType {
|
|
60
|
+
GET = 'GET',
|
|
61
|
+
SET = 'SET',
|
|
62
|
+
APPLY = 'APPLY',
|
|
63
|
+
CONSTRUCT = 'CONSTRUCT',
|
|
64
|
+
ENDPOINT = 'ENDPOINT',
|
|
65
|
+
RELEASE = 'RELEASE',
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
export interface GetMessage {
|
|
69
|
-
id?: MessageID
|
|
70
|
-
type: MessageType.GET
|
|
71
|
-
path: string[]
|
|
69
|
+
id?: MessageID
|
|
70
|
+
type: MessageType.GET
|
|
71
|
+
path: string[]
|
|
72
72
|
}
|
|
73
73
|
|
|
74
74
|
export interface SetMessage {
|
|
75
|
-
id?: MessageID
|
|
76
|
-
type: MessageType.SET
|
|
77
|
-
path: string[]
|
|
78
|
-
value: WireValue
|
|
75
|
+
id?: MessageID
|
|
76
|
+
type: MessageType.SET
|
|
77
|
+
path: string[]
|
|
78
|
+
value: WireValue
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
export interface ApplyMessage {
|
|
82
|
-
id?: MessageID
|
|
83
|
-
type: MessageType.APPLY
|
|
84
|
-
path: string[]
|
|
85
|
-
argumentList: WireValue[]
|
|
82
|
+
id?: MessageID
|
|
83
|
+
type: MessageType.APPLY
|
|
84
|
+
path: string[]
|
|
85
|
+
argumentList: WireValue[]
|
|
86
86
|
}
|
|
87
87
|
|
|
88
88
|
export interface ConstructMessage {
|
|
89
|
-
id?: MessageID
|
|
90
|
-
type: MessageType.CONSTRUCT
|
|
91
|
-
path: string[]
|
|
92
|
-
argumentList: WireValue[]
|
|
89
|
+
id?: MessageID
|
|
90
|
+
type: MessageType.CONSTRUCT
|
|
91
|
+
path: string[]
|
|
92
|
+
argumentList: WireValue[]
|
|
93
93
|
}
|
|
94
94
|
|
|
95
95
|
export interface EndpointMessage {
|
|
96
|
-
id?: MessageID
|
|
97
|
-
type: MessageType.ENDPOINT
|
|
96
|
+
id?: MessageID
|
|
97
|
+
type: MessageType.ENDPOINT
|
|
98
98
|
}
|
|
99
99
|
|
|
100
100
|
export interface ReleaseMessage {
|
|
101
|
-
id?: MessageID
|
|
102
|
-
type: MessageType.RELEASE
|
|
101
|
+
id?: MessageID
|
|
102
|
+
type: MessageType.RELEASE
|
|
103
103
|
}
|
|
104
104
|
|
|
105
105
|
export type Message =
|
|
@@ -108,4 +108,4 @@ export type Message =
|
|
|
108
108
|
| ApplyMessage
|
|
109
109
|
| ConstructMessage
|
|
110
110
|
| EndpointMessage
|
|
111
|
-
| ReleaseMessage
|
|
111
|
+
| ReleaseMessage
|
package/src/helpers/endpoint.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { FrameHost } from
|
|
2
|
-
import { Provider } from
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
1
|
+
import type { FrameHost } from '@farcaster/frame-core'
|
|
2
|
+
import type { Provider } from 'ox'
|
|
3
|
+
import { useEffect } from 'react'
|
|
4
|
+
import * as Comlink from '../comlink'
|
|
5
|
+
import type { HostEndpoint } from '../types'
|
|
6
|
+
import { forwardProviderEvents, wrapProviderRequest } from './provider'
|
|
7
|
+
import { wrapHandlers } from './sdk'
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* @returns function to cleanup provider listeners
|
|
@@ -16,29 +16,29 @@ export function exposeToEndpoint({
|
|
|
16
16
|
ethProvider,
|
|
17
17
|
debug = false,
|
|
18
18
|
}: {
|
|
19
|
-
endpoint: HostEndpoint
|
|
20
|
-
sdk: Omit<FrameHost,
|
|
21
|
-
frameOrigin: string
|
|
22
|
-
ethProvider?: Provider.Provider
|
|
23
|
-
debug?: boolean
|
|
19
|
+
endpoint: HostEndpoint
|
|
20
|
+
sdk: Omit<FrameHost, 'ethProviderRequestV2'>
|
|
21
|
+
frameOrigin: string
|
|
22
|
+
ethProvider?: Provider.Provider
|
|
23
|
+
debug?: boolean
|
|
24
24
|
}) {
|
|
25
|
-
const extendedSdk = wrapHandlers(sdk as FrameHost)
|
|
25
|
+
const extendedSdk = wrapHandlers(sdk as FrameHost)
|
|
26
26
|
|
|
27
|
-
let cleanup: () => void | undefined
|
|
27
|
+
let cleanup: (() => void) | undefined
|
|
28
28
|
if (ethProvider) {
|
|
29
29
|
extendedSdk.ethProviderRequestV2 = wrapProviderRequest({
|
|
30
30
|
provider: ethProvider,
|
|
31
31
|
debug,
|
|
32
|
-
})
|
|
33
|
-
cleanup = forwardProviderEvents({ provider: ethProvider, endpoint })
|
|
32
|
+
})
|
|
33
|
+
cleanup = forwardProviderEvents({ provider: ethProvider, endpoint })
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
const unexpose = Comlink.expose(extendedSdk, endpoint, [frameOrigin])
|
|
36
|
+
const unexpose = Comlink.expose(extendedSdk, endpoint, [frameOrigin])
|
|
37
37
|
|
|
38
38
|
return () => {
|
|
39
|
-
cleanup?.()
|
|
40
|
-
unexpose()
|
|
41
|
-
}
|
|
39
|
+
cleanup?.()
|
|
40
|
+
unexpose()
|
|
41
|
+
}
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
export function useExposeToEndpoint({
|
|
@@ -48,15 +48,15 @@ export function useExposeToEndpoint({
|
|
|
48
48
|
ethProvider,
|
|
49
49
|
debug = false,
|
|
50
50
|
}: {
|
|
51
|
-
endpoint: HostEndpoint | undefined
|
|
52
|
-
sdk: Omit<FrameHost,
|
|
53
|
-
frameOrigin: string
|
|
54
|
-
ethProvider?: Provider.Provider
|
|
55
|
-
debug?: boolean
|
|
51
|
+
endpoint: HostEndpoint | undefined
|
|
52
|
+
sdk: Omit<FrameHost, 'ethProviderRequestV2'>
|
|
53
|
+
frameOrigin: string
|
|
54
|
+
ethProvider?: Provider.Provider
|
|
55
|
+
debug?: boolean
|
|
56
56
|
}) {
|
|
57
57
|
useEffect(() => {
|
|
58
58
|
if (!endpoint) {
|
|
59
|
-
return
|
|
59
|
+
return
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
const cleanup = exposeToEndpoint({
|
|
@@ -65,8 +65,8 @@ export function useExposeToEndpoint({
|
|
|
65
65
|
frameOrigin,
|
|
66
66
|
ethProvider,
|
|
67
67
|
debug,
|
|
68
|
-
})
|
|
68
|
+
})
|
|
69
69
|
|
|
70
|
-
return cleanup
|
|
71
|
-
}, [endpoint, sdk, ethProvider, frameOrigin, debug])
|
|
70
|
+
return cleanup
|
|
71
|
+
}, [endpoint, sdk, ethProvider, frameOrigin, debug])
|
|
72
72
|
}
|
package/src/helpers/provider.ts
CHANGED
|
@@ -1,42 +1,42 @@
|
|
|
1
|
-
import { Provider, RpcRequest, RpcResponse } from
|
|
2
|
-
import { HostEndpoint } from
|
|
1
|
+
import { Provider, type RpcRequest, RpcResponse } from 'ox'
|
|
2
|
+
import type { HostEndpoint } from '../types'
|
|
3
3
|
|
|
4
4
|
export function forwardProviderEvents({
|
|
5
5
|
provider,
|
|
6
6
|
endpoint,
|
|
7
7
|
}: {
|
|
8
|
-
provider: Provider.Provider
|
|
9
|
-
endpoint: HostEndpoint
|
|
8
|
+
provider: Provider.Provider
|
|
9
|
+
endpoint: HostEndpoint
|
|
10
10
|
}) {
|
|
11
|
-
|
|
12
|
-
endpoint.emitEthProvider(
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
endpoint.emitEthProvider(
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
endpoint.emitEthProvider(
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
endpoint.emitEthProvider(
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
endpoint.emitEthProvider(
|
|
25
|
-
}
|
|
11
|
+
const accountsChanged: Provider.EventMap['accountsChanged'] = (accounts) => {
|
|
12
|
+
endpoint.emitEthProvider('accountsChanged', [accounts])
|
|
13
|
+
}
|
|
14
|
+
const chainChanged: Provider.EventMap['chainChanged'] = (chainId) => {
|
|
15
|
+
endpoint.emitEthProvider('chainChanged', [chainId])
|
|
16
|
+
}
|
|
17
|
+
const connect: Provider.EventMap['connect'] = (connectInfo) => {
|
|
18
|
+
endpoint.emitEthProvider('connect', [connectInfo])
|
|
19
|
+
}
|
|
20
|
+
const disconnect: Provider.EventMap['disconnect'] = (providerRpcError) => {
|
|
21
|
+
endpoint.emitEthProvider('disconnect', [providerRpcError])
|
|
22
|
+
}
|
|
23
|
+
const message: Provider.EventMap['message'] = (message) => {
|
|
24
|
+
endpoint.emitEthProvider('message', [message])
|
|
25
|
+
}
|
|
26
26
|
|
|
27
|
-
provider.on(
|
|
28
|
-
provider.on(
|
|
29
|
-
provider.on(
|
|
30
|
-
provider.on(
|
|
31
|
-
provider.on(
|
|
27
|
+
provider.on('accountsChanged', accountsChanged)
|
|
28
|
+
provider.on('chainChanged', chainChanged)
|
|
29
|
+
provider.on('connect', connect)
|
|
30
|
+
provider.on('disconnect', disconnect)
|
|
31
|
+
provider.on('message', message)
|
|
32
32
|
|
|
33
33
|
return () => {
|
|
34
|
-
provider.removeListener(
|
|
35
|
-
provider.removeListener(
|
|
36
|
-
provider.removeListener(
|
|
37
|
-
provider.removeListener(
|
|
38
|
-
provider.removeListener(
|
|
39
|
-
}
|
|
34
|
+
provider.removeListener('accountsChanged', accountsChanged)
|
|
35
|
+
provider.removeListener('chainChanged', chainChanged)
|
|
36
|
+
provider.removeListener('connect', connect)
|
|
37
|
+
provider.removeListener('disconnect', disconnect)
|
|
38
|
+
provider.removeListener('message', message)
|
|
39
|
+
}
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
// export type FrameTransport<
|
|
@@ -53,25 +53,25 @@ export const wrapProviderRequest =
|
|
|
53
53
|
provider,
|
|
54
54
|
debug = false,
|
|
55
55
|
}: {
|
|
56
|
-
provider: Provider.Provider
|
|
57
|
-
debug?: boolean
|
|
56
|
+
provider: Provider.Provider
|
|
57
|
+
debug?: boolean
|
|
58
58
|
}) =>
|
|
59
59
|
async (request: RpcRequest.RpcRequest) => {
|
|
60
60
|
try {
|
|
61
61
|
if (debug) {
|
|
62
|
-
console.debug(
|
|
62
|
+
console.debug('[frame-host] eth provider req: ', request)
|
|
63
63
|
}
|
|
64
|
-
const result = await provider.request(request)
|
|
65
|
-
const response = RpcResponse.from({ result }, { request })
|
|
64
|
+
const result = await provider.request(request)
|
|
65
|
+
const response = RpcResponse.from({ result }, { request })
|
|
66
66
|
|
|
67
67
|
if (debug) {
|
|
68
|
-
console.debug(
|
|
68
|
+
console.debug('[frame-host] eth provider res: ', response)
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
-
return response
|
|
71
|
+
return response
|
|
72
72
|
} catch (e) {
|
|
73
73
|
if (debug) {
|
|
74
|
-
console.error(
|
|
74
|
+
console.error('provider request error', e)
|
|
75
75
|
}
|
|
76
76
|
if (e instanceof Provider.ProviderRpcError) {
|
|
77
77
|
return RpcResponse.from(
|
|
@@ -83,16 +83,16 @@ export const wrapProviderRequest =
|
|
|
83
83
|
},
|
|
84
84
|
},
|
|
85
85
|
{ request },
|
|
86
|
-
)
|
|
86
|
+
)
|
|
87
87
|
}
|
|
88
88
|
|
|
89
89
|
if (
|
|
90
90
|
e !== null &&
|
|
91
|
-
typeof e ===
|
|
92
|
-
|
|
93
|
-
typeof e.message ===
|
|
94
|
-
|
|
95
|
-
typeof e.code ===
|
|
91
|
+
typeof e === 'object' &&
|
|
92
|
+
'message' in e &&
|
|
93
|
+
typeof e.message === 'string' &&
|
|
94
|
+
'code' in e &&
|
|
95
|
+
typeof e.code === 'number'
|
|
96
96
|
) {
|
|
97
97
|
return RpcResponse.from(
|
|
98
98
|
{
|
|
@@ -100,16 +100,16 @@ export const wrapProviderRequest =
|
|
|
100
100
|
message: e.message,
|
|
101
101
|
code: e.code,
|
|
102
102
|
details:
|
|
103
|
-
|
|
103
|
+
'details' in e && typeof e.details === 'string'
|
|
104
104
|
? e.details
|
|
105
105
|
: undefined,
|
|
106
106
|
},
|
|
107
107
|
},
|
|
108
108
|
{ request },
|
|
109
|
-
)
|
|
109
|
+
)
|
|
110
110
|
}
|
|
111
111
|
|
|
112
|
-
const errorMessage = e instanceof Error ? e.message :
|
|
112
|
+
const errorMessage = e instanceof Error ? e.message : 'Unknown'
|
|
113
113
|
return RpcResponse.from(
|
|
114
114
|
{
|
|
115
115
|
error: {
|
|
@@ -118,6 +118,6 @@ export const wrapProviderRequest =
|
|
|
118
118
|
},
|
|
119
119
|
},
|
|
120
120
|
{ request },
|
|
121
|
-
)
|
|
121
|
+
)
|
|
122
122
|
}
|
|
123
|
-
}
|
|
123
|
+
}
|
package/src/helpers/sdk.ts
CHANGED
|
@@ -1,24 +1,48 @@
|
|
|
1
|
-
import type { FrameHost, WireFrameHost } from
|
|
2
|
-
import { SignIn } from
|
|
1
|
+
import type { FrameHost, WireFrameHost } from '@farcaster/frame-core'
|
|
2
|
+
import { AddFrame, SignIn } from '@farcaster/frame-core'
|
|
3
3
|
|
|
4
4
|
export function wrapHandlers(host: FrameHost): WireFrameHost {
|
|
5
5
|
return {
|
|
6
6
|
...host,
|
|
7
|
+
addFrame: async () => {
|
|
8
|
+
try {
|
|
9
|
+
const result = await host.addFrame()
|
|
10
|
+
return { result }
|
|
11
|
+
} catch (e) {
|
|
12
|
+
if (e instanceof AddFrame.RejectedByUser) {
|
|
13
|
+
return {
|
|
14
|
+
error: {
|
|
15
|
+
type: 'rejected_by_user',
|
|
16
|
+
},
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (e instanceof AddFrame.InvalidDomainManifest) {
|
|
21
|
+
return {
|
|
22
|
+
error: {
|
|
23
|
+
type: 'invalid_domain_manifest',
|
|
24
|
+
},
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
throw e
|
|
29
|
+
}
|
|
30
|
+
},
|
|
7
31
|
signIn: async (options) => {
|
|
8
32
|
try {
|
|
9
|
-
const result = await host.signIn(options)
|
|
10
|
-
return { result }
|
|
33
|
+
const result = await host.signIn(options)
|
|
34
|
+
return { result }
|
|
11
35
|
} catch (e) {
|
|
12
36
|
if (e instanceof SignIn.RejectedByUser) {
|
|
13
37
|
return {
|
|
14
38
|
error: {
|
|
15
|
-
type: 'rejected_by_user'
|
|
16
|
-
}
|
|
39
|
+
type: 'rejected_by_user',
|
|
40
|
+
},
|
|
17
41
|
}
|
|
18
42
|
}
|
|
19
43
|
|
|
20
|
-
throw e
|
|
44
|
+
throw e
|
|
21
45
|
}
|
|
22
|
-
}
|
|
46
|
+
},
|
|
23
47
|
}
|
|
24
48
|
}
|
package/src/iframe.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
import type { Provider } from
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
1
|
+
import type { FrameHost } from '@farcaster/frame-core'
|
|
2
|
+
import type { Provider } from 'ox'
|
|
3
|
+
import * as Comlink from './comlink'
|
|
4
|
+
import { exposeToEndpoint } from './helpers/endpoint'
|
|
5
|
+
import type { HostEndpoint } from './types'
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* An endpoint of communicating with an iFrame
|
|
@@ -12,39 +12,39 @@ export function createIframeEndpoint({
|
|
|
12
12
|
targetOrigin,
|
|
13
13
|
debug = true,
|
|
14
14
|
}: {
|
|
15
|
-
iframe: HTMLIFrameElement
|
|
16
|
-
targetOrigin: string
|
|
17
|
-
debug?: boolean
|
|
15
|
+
iframe: HTMLIFrameElement
|
|
16
|
+
targetOrigin: string
|
|
17
|
+
debug?: boolean
|
|
18
18
|
}): HostEndpoint {
|
|
19
19
|
return {
|
|
20
20
|
// when is contentWindow null
|
|
21
21
|
...Comlink.windowEndpoint(iframe.contentWindow!),
|
|
22
22
|
emit: (event) => {
|
|
23
23
|
if (debug) {
|
|
24
|
-
console.debug(
|
|
24
|
+
console.debug('frameEvent', event)
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
const wireEvent = {
|
|
28
|
-
type:
|
|
28
|
+
type: 'frameEvent',
|
|
29
29
|
event,
|
|
30
|
-
}
|
|
30
|
+
}
|
|
31
31
|
|
|
32
|
-
iframe.contentWindow?.postMessage(wireEvent, targetOrigin)
|
|
32
|
+
iframe.contentWindow?.postMessage(wireEvent, targetOrigin)
|
|
33
33
|
},
|
|
34
34
|
emitEthProvider: (event, params) => {
|
|
35
35
|
if (debug) {
|
|
36
|
-
console.debug(
|
|
36
|
+
console.debug('fc:emitEthProvider', event, params)
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
const wireEvent = {
|
|
40
|
-
type:
|
|
40
|
+
type: 'frameEthProviderEvent',
|
|
41
41
|
event,
|
|
42
42
|
params,
|
|
43
|
-
}
|
|
43
|
+
}
|
|
44
44
|
|
|
45
|
-
iframe.contentWindow?.postMessage(wireEvent, targetOrigin)
|
|
45
|
+
iframe.contentWindow?.postMessage(wireEvent, targetOrigin)
|
|
46
46
|
},
|
|
47
|
-
}
|
|
47
|
+
}
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
export function exposeToIframe({
|
|
@@ -54,27 +54,27 @@ export function exposeToIframe({
|
|
|
54
54
|
frameOrigin,
|
|
55
55
|
debug = false,
|
|
56
56
|
}: {
|
|
57
|
-
iframe: HTMLIFrameElement
|
|
58
|
-
sdk: Omit<FrameHost,
|
|
59
|
-
frameOrigin: string
|
|
60
|
-
ethProvider?: Provider.Provider
|
|
61
|
-
debug?: boolean
|
|
57
|
+
iframe: HTMLIFrameElement
|
|
58
|
+
sdk: Omit<FrameHost, 'ethProviderRequestV2'>
|
|
59
|
+
frameOrigin: string
|
|
60
|
+
ethProvider?: Provider.Provider
|
|
61
|
+
debug?: boolean
|
|
62
62
|
}) {
|
|
63
63
|
const endpoint = createIframeEndpoint({
|
|
64
64
|
iframe,
|
|
65
65
|
targetOrigin: frameOrigin,
|
|
66
66
|
debug,
|
|
67
|
-
})
|
|
67
|
+
})
|
|
68
68
|
const cleanup = exposeToEndpoint({
|
|
69
69
|
endpoint,
|
|
70
70
|
sdk,
|
|
71
71
|
ethProvider,
|
|
72
72
|
frameOrigin,
|
|
73
73
|
debug,
|
|
74
|
-
})
|
|
74
|
+
})
|
|
75
75
|
|
|
76
76
|
return {
|
|
77
77
|
endpoint,
|
|
78
78
|
cleanup,
|
|
79
|
-
}
|
|
79
|
+
}
|
|
80
80
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export * from
|
|
1
|
+
export * from '@farcaster/frame-core'
|
|
2
|
+
export * from './types'
|
|
3
|
+
export * from './iframe'
|
|
4
|
+
export * from './helpers/endpoint'
|
package/src/types.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
1
|
+
import type { EmitEthProvider, FrameClientEvent } from '@farcaster/frame-core'
|
|
2
|
+
import type { Endpoint } from './comlink'
|
|
3
3
|
|
|
4
4
|
export type HostEndpoint = Endpoint & {
|
|
5
|
-
emit: (event: FrameClientEvent) => void
|
|
6
|
-
emitEthProvider: EmitEthProvider
|
|
7
|
-
}
|
|
5
|
+
emit: (event: FrameClientEvent) => void
|
|
6
|
+
emitEthProvider: EmitEthProvider
|
|
7
|
+
}
|