diff --git a/packages/fcl-core/src/wallet-utils/CompositeSignature.js b/packages/fcl-core/src/wallet-utils/CompositeSignature.js deleted file mode 100644 index 5450c596e..000000000 --- a/packages/fcl-core/src/wallet-utils/CompositeSignature.js +++ /dev/null @@ -1,18 +0,0 @@ -import {withPrefix} from "@onflow/util-address" -import {COMPOSITE_SIGNATURE_PRAGMA} from "../normalizers/service/__vsn" - -/** - * @description - * Constructs a new CompositeSignature instance. - * - * @param {string} addr - Flow Address - * @param {number} keyId - Key ID - * @param {string} signature - Signature as a hex string - */ -export function CompositeSignature(addr, keyId, signature) { - this.f_type = COMPOSITE_SIGNATURE_PRAGMA.f_type - this.f_vsn = COMPOSITE_SIGNATURE_PRAGMA.f_vsn - this.addr = withPrefix(addr) - this.keyId = Number(keyId) - this.signature = signature -} diff --git a/packages/fcl-core/src/wallet-utils/CompositeSignature.ts b/packages/fcl-core/src/wallet-utils/CompositeSignature.ts new file mode 100644 index 000000000..82085e1bf --- /dev/null +++ b/packages/fcl-core/src/wallet-utils/CompositeSignature.ts @@ -0,0 +1,25 @@ +import {withPrefix} from "@onflow/util-address" +import {COMPOSITE_SIGNATURE_PRAGMA} from "../normalizers/service/__vsn" + +/** + * @description Constructs a new CompositeSignature instance. + * + * @param addr Flow Address + * @param keyId Key ID + * @param signature Signature as a hex string + */ +export class CompositeSignature { + f_type: string + f_vsn: string + addr: string + keyId: number + signature: string + + constructor(addr: string, keyId: number | string, signature: string) { + this.f_type = COMPOSITE_SIGNATURE_PRAGMA.f_type + this.f_vsn = COMPOSITE_SIGNATURE_PRAGMA.f_vsn + this.addr = withPrefix(addr) + this.keyId = Number(keyId) + this.signature = signature + } +} diff --git a/packages/fcl-core/src/wallet-utils/encode-account-proof.test.js b/packages/fcl-core/src/wallet-utils/encode-account-proof.test.ts similarity index 96% rename from packages/fcl-core/src/wallet-utils/encode-account-proof.test.js rename to packages/fcl-core/src/wallet-utils/encode-account-proof.test.ts index 471adce93..60a5adb08 100644 --- a/packages/fcl-core/src/wallet-utils/encode-account-proof.test.js +++ b/packages/fcl-core/src/wallet-utils/encode-account-proof.test.ts @@ -1,4 +1,4 @@ -import {encodeAccountProof} from "./encode-account-proof.js" +import {encodeAccountProof} from "./encode-account-proof" const address = "0xABC123DEF456" const appIdentifier = "AWESOME-APP-ID" diff --git a/packages/fcl-core/src/wallet-utils/encode-account-proof.js b/packages/fcl-core/src/wallet-utils/encode-account-proof.ts similarity index 59% rename from packages/fcl-core/src/wallet-utils/encode-account-proof.js rename to packages/fcl-core/src/wallet-utils/encode-account-proof.ts index 0b01477a2..e6bbb08bf 100644 --- a/packages/fcl-core/src/wallet-utils/encode-account-proof.js +++ b/packages/fcl-core/src/wallet-utils/encode-account-proof.ts @@ -2,35 +2,41 @@ import {sansPrefix} from "@onflow/util-address" import {invariant} from "@onflow/util-invariant" import {Buffer, encode as rlpEncode} from "@onflow/rlp" -const rightPaddedHexBuffer = (value, pad) => +export interface AccountProofData { + address?: string + nonce?: string + appIdentifier?: string +} + +const rightPaddedHexBuffer = (value: string, pad: number): Buffer => Buffer.from(value.padEnd(pad * 2, "0"), "hex") -const leftPaddedHexBuffer = (value, pad) => +const leftPaddedHexBuffer = (value: string, pad: number): Buffer => Buffer.from(value.padStart(pad * 2, "0"), "hex") -const addressBuffer = addr => leftPaddedHexBuffer(addr, 8) +const addressBuffer = (addr: string): Buffer => leftPaddedHexBuffer(addr, 8) -const nonceBuffer = nonce => Buffer.from(nonce, "hex") +const nonceBuffer = (nonce: string): Buffer => Buffer.from(nonce, "hex") export const encodeAccountProof = ( - {address, nonce, appIdentifier}, - includeDomainTag = true -) => { + {address, nonce, appIdentifier}: AccountProofData, + includeDomainTag: boolean = true +): string => { invariant( - address, + !!address, "Encode Message For Provable Authn Error: address must be defined" ) invariant( - nonce, + !!nonce, "Encode Message For Provable Authn Error: nonce must be defined" ) invariant( - appIdentifier, + !!appIdentifier, "Encode Message For Provable Authn Error: appIdentifier must be defined" ) invariant( - nonce.length >= 64, + nonce!.length >= 64, "Encode Message For Provable Authn Error: nonce must be minimum of 32 bytes" ) @@ -44,15 +50,15 @@ export const encodeAccountProof = ( ACCOUNT_PROOF_DOMAIN_TAG, rlpEncode([ appIdentifier, - addressBuffer(sansPrefix(address)), - nonceBuffer(nonce), + addressBuffer(sansPrefix(address!)), + nonceBuffer(nonce!), ]), ]).toString("hex") } return rlpEncode([ appIdentifier, - addressBuffer(sansPrefix(address)), - nonceBuffer(nonce), + addressBuffer(sansPrefix(address!)), + nonceBuffer(nonce!), ]).toString("hex") } diff --git a/packages/fcl-core/src/wallet-utils/index.js b/packages/fcl-core/src/wallet-utils/index.js deleted file mode 100644 index 1757de831..000000000 --- a/packages/fcl-core/src/wallet-utils/index.js +++ /dev/null @@ -1,13 +0,0 @@ -export { - sendMsgToFCL, - ready, - close, - approve, - decline, - redirect, -} from "./send-msg-to-fcl.js" -export {onMessageFromFCL} from "./on-message-from-fcl.js" -export {encodeMessageFromSignable} from "@onflow/sdk" -export {CompositeSignature} from "./CompositeSignature.js" -export {encodeAccountProof} from "./encode-account-proof.js" -export {injectExtService} from "./inject-ext-service.js" diff --git a/packages/fcl-core/src/wallet-utils/index.ts b/packages/fcl-core/src/wallet-utils/index.ts new file mode 100644 index 000000000..a37d0051d --- /dev/null +++ b/packages/fcl-core/src/wallet-utils/index.ts @@ -0,0 +1,13 @@ +export { + sendMsgToFCL, + ready, + close, + approve, + decline, + redirect, +} from "./send-msg-to-fcl" +export {onMessageFromFCL} from "./on-message-from-fcl" +export {encodeMessageFromSignable} from "@onflow/sdk" +export {CompositeSignature} from "./CompositeSignature" +export {encodeAccountProof} from "./encode-account-proof" +export {injectExtService} from "./inject-ext-service" diff --git a/packages/fcl-core/src/wallet-utils/inject-ext-service.js b/packages/fcl-core/src/wallet-utils/inject-ext-service.js deleted file mode 100644 index b5c607707..000000000 --- a/packages/fcl-core/src/wallet-utils/inject-ext-service.js +++ /dev/null @@ -1,10 +0,0 @@ -export function injectExtService(service) { - if (service.type === "authn" && service.endpoint != null) { - if (!Array.isArray(window.fcl_extensions)) { - window.fcl_extensions = [] - } - window.fcl_extensions.push(service) - } else { - console.warn("Authn service is required") - } -} diff --git a/packages/fcl-core/src/wallet-utils/inject-ext-service.ts b/packages/fcl-core/src/wallet-utils/inject-ext-service.ts new file mode 100644 index 000000000..e3b82481c --- /dev/null +++ b/packages/fcl-core/src/wallet-utils/inject-ext-service.ts @@ -0,0 +1,12 @@ +import type {Service} from "@onflow/typedefs" + +export function injectExtService(service: Service): void { + if (service.type === "authn" && service.endpoint != null) { + if (!Array.isArray((window as any).fcl_extensions)) { + ;(window as any).fcl_extensions = [] + } + ;(window as any).fcl_extensions.push(service) + } else { + console.warn("Authn service is required") + } +} diff --git a/packages/fcl-core/src/wallet-utils/on-message-from-fcl.js b/packages/fcl-core/src/wallet-utils/on-message-from-fcl.ts similarity index 59% rename from packages/fcl-core/src/wallet-utils/on-message-from-fcl.js rename to packages/fcl-core/src/wallet-utils/on-message-from-fcl.ts index 585269fe2..18dff7b65 100644 --- a/packages/fcl-core/src/wallet-utils/on-message-from-fcl.js +++ b/packages/fcl-core/src/wallet-utils/on-message-from-fcl.ts @@ -2,12 +2,15 @@ * @description * Listens for messages from FCL * - * @param {string} messageType - Message type - * @param {Function} cb - Callback function - * @returns {Function} - Function to remove event listener + * @param messageType Message type + * @param cb Callback function + * @returns Function to remove event listener */ -export const onMessageFromFCL = (messageType, cb = () => {}) => { - const buildData = data => { +export const onMessageFromFCL = ( + messageType: string, + cb: (data: any, context: {origin: string}) => void = () => {} +): (() => void) => { + const buildData = (data: any): any => { if (data.deprecated) console.warn("DEPRECATION NOTICE", data.deprecated.message) delete data?.body?.interaction @@ -15,7 +18,7 @@ export const onMessageFromFCL = (messageType, cb = () => {}) => { return data } - const internal = e => { + const internal = (e: MessageEvent): void => { const {data, origin} = e if (typeof data !== "object") return if (typeof data == null) return diff --git a/packages/fcl-core/src/wallet-utils/send-msg-to-fcl.js b/packages/fcl-core/src/wallet-utils/send-msg-to-fcl.ts similarity index 58% rename from packages/fcl-core/src/wallet-utils/send-msg-to-fcl.js rename to packages/fcl-core/src/wallet-utils/send-msg-to-fcl.ts index 2362dc177..5b57528be 100644 --- a/packages/fcl-core/src/wallet-utils/send-msg-to-fcl.js +++ b/packages/fcl-core/src/wallet-utils/send-msg-to-fcl.ts @@ -3,15 +3,20 @@ import { FCL_RESPONSE_PARAM_NAME, } from "../utils/constants" import {onMessageFromFCL} from "./on-message-from-fcl" -import {URL} from "../utils/url" + +export interface PollingResponse { + f_type: "PollingResponse" + f_vsn: "1.0.0" + status: "APPROVED" | "DECLINED" | "REDIRECT" + reason: string | null + data: any +} /** - * @description - * Sends message to FCL window + * @description Sends message to FCL window * - * @param {string} type - Message type - * @param {object} msg - Message object - * @returns {void} + * @param type Message type + * @param msg Message object * * @example * sendMsgToFCL("FCL:VIEW:RESPONSE", { @@ -22,7 +27,7 @@ import {URL} from "../utils/url" * data: data, * }) */ -export const sendMsgToFCL = (type, msg = {}) => { +export const sendMsgToFCL = (type: string, msg?: PollingResponse): void => { const data = {...msg, type} const urlParams = new URLSearchParams(window.location.search) @@ -41,36 +46,32 @@ export const sendMsgToFCL = (type, msg = {}) => { } /** - * @description - * Listens for "FCL:VIEW:READY:RESPONSE" and sends "FCL:VIEW:READY" + * @description Listens for "FCL:VIEW:READY:RESPONSE" and sends "FCL:VIEW:READY" * - * @param {Function} cb - Callback function - * @param {object} msg - Message object - * @returns {void} + * @param cb Callback function + * @param msg Message object */ -export const ready = (cb, msg = {}) => { +export const ready = ( + cb: (data: any, context: {origin: string}) => void, + msg: PollingResponse = {} as PollingResponse +): void => { onMessageFromFCL("FCL:VIEW:READY:RESPONSE", cb) sendMsgToFCL("FCL:VIEW:READY") } /** - * @description - * Sends "FCL:VIEW:CLOSE" - * - * @returns {void} + * @description Sends "FCL:VIEW:CLOSE" */ -export const close = () => { +export const close = (): void => { sendMsgToFCL("FCL:VIEW:CLOSE") } /** - * @description - * Sends "FCL:VIEW:RESPONSE" with status "APPROVED" + * @description Sends "FCL:VIEW:RESPONSE" with status "APPROVED" * - * @param {object} data - Data object - * @returns {void} + * @param data Data object */ -export const approve = data => { +export const approve = (data: any): void => { sendMsgToFCL("FCL:VIEW:RESPONSE", { f_type: "PollingResponse", f_vsn: "1.0.0", @@ -81,13 +82,11 @@ export const approve = data => { } /** - * @description - * Sends "FCL:VIEW:RESPONSE" with status "DECLINED" + * @description Sends "FCL:VIEW:RESPONSE" with status "DECLINED" * - * @param {string} reason - Reason for declining - * @returns {void} + * @param reason Reason for declining */ -export const decline = reason => { +export const decline = (reason: string): void => { sendMsgToFCL("FCL:VIEW:RESPONSE", { f_type: "PollingResponse", f_vsn: "1.0.0", @@ -98,13 +97,11 @@ export const decline = reason => { } /** - * @description - * Sends "FCL:VIEW:RESPONSE" with status "REDIRECT" + * @description Sends "FCL:VIEW:RESPONSE" with status "REDIRECT" * - * @param {object} data - Data object - * @returns {void} + * @param data Data object */ -export const redirect = data => { +export const redirect = (data: any): void => { sendMsgToFCL("FCL:VIEW:RESPONSE", { f_type: "PollingResponse", f_vsn: "1.0.0", diff --git a/packages/fcl-core/src/wallet-utils/wallet-utils.test.js b/packages/fcl-core/src/wallet-utils/wallet-utils.test.ts similarity index 100% rename from packages/fcl-core/src/wallet-utils/wallet-utils.test.js rename to packages/fcl-core/src/wallet-utils/wallet-utils.test.ts