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