Skip to content

Refactored fcl-core app utils folder files to TypeScript #2442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe("verifyUserSignatures", () => {

it("should reject if missing array of composite signatures", async () => {
expect.assertions(1)
await expect(verifyUserSignatures(message, null)).rejects.toThrow(Error)
await expect(verifyUserSignatures(message, [])).rejects.toThrow(Error)
})

it("should reject if compSigs are from different account addresses", async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {verifyAccountProof, verifyUserSignatures} from "./verify-signatures.js"
export {verifyAccountProof, verifyUserSignatures} from "./verify-signatures"
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,42 @@ import {query} from "../exec/query"
import {encodeAccountProof} from "../wallet-utils"
import {isString} from "../utils/is"
import {getChainId} from "../utils"
import {CompositeSignature} from "@onflow/typedefs"

export interface AccountProofData {
address: string
nonce: string
signatures: CompositeSignature[]
}

export interface VerifySignaturesScriptOptions {
fclCryptoContract?: string
}

export interface ValidateArgsInput {
appIdentifier?: string
address?: string
nonce?: string
signatures?: CompositeSignature[]
message?: string
compSigs?: CompositeSignature[]
}

const ACCOUNT_PROOF = "ACCOUNT_PROOF"
const USER_SIGNATURE = "USER_SIGNATURE"

export const validateArgs = args => {
export const validateArgs = (args: ValidateArgsInput): boolean => {
if (args.appIdentifier) {
const {appIdentifier, address, nonce, signatures} = args
invariant(
isString(appIdentifier),
"verifyAccountProof({ appIdentifier }) -- appIdentifier must be a string"
)
invariant(
isString(address) && sansPrefix(address).length === 16,
isString(address) && sansPrefix(address!).length === 16,
"verifyAccountProof({ address }) -- address must be a valid address"
)
invariant(/^[0-9a-f]+$/i.test(nonce), "nonce must be a hex string")
invariant(/^[0-9a-f]+$/i.test(nonce!), "nonce must be a hex string")
invariant(
Array.isArray(signatures) &&
signatures.every((sig, i, arr) => sig.f_type === "CompositeSignature"),
Expand All @@ -33,11 +53,11 @@ export const validateArgs = args => {
} else {
const {message, address, compSigs} = args
invariant(
/^[0-9a-f]+$/i.test(message),
/^[0-9a-f]+$/i.test(message!),
"Signed message must be a hex string"
)
invariant(
isString(address) && sansPrefix(address).length === 16,
isString(address) && sansPrefix(address!).length === 16,
"verifyUserSignatures({ address }) -- address must be a valid address"
)
invariant(
Expand All @@ -55,23 +75,26 @@ export const validateArgs = args => {

// TODO: pass in option for contract but we're connected to testnet
// log address + network -> in sync?
const getVerifySignaturesScript = async (sig, opts) => {
const getVerifySignaturesScript = async (
sig: string,
opts: VerifySignaturesScriptOptions
): Promise<string> => {
const verifyFunction =
sig === "ACCOUNT_PROOF"
? "verifyAccountProofSignatures"
: "verifyUserSignatures"

let network = await getChainId(opts)
const network = await getChainId(opts)

const contractAddresses = {
const contractAddresses: any = {
testnet: "0x74daa6f9c7ef24b1",
mainnet: "0xb4b82a1c9d21d284",
previewnet: "0x40b5b8b2ce81ea4a",
}
const fclCryptoContract = opts.fclCryptoContract || contractAddresses[network]

invariant(
fclCryptoContract,
fclCryptoContract as any,
`${verifyFunction}({ fclCryptoContract }) -- FCLCrypto contract address is unknown for network: ${network}. Please manually specify the FCLCrypto contract address.`
)

Expand Down Expand Up @@ -117,15 +140,15 @@ const getVerifySignaturesScript = async (sig, opts) => {
* )
*/
export async function verifyAccountProof(
appIdentifier,
{address, nonce, signatures},
opts = {}
) {
appIdentifier: string,
{address, nonce, signatures}: AccountProofData,
opts: VerifySignaturesScriptOptions = {}
): Promise<boolean> {
validateArgs({appIdentifier, address, nonce, signatures})
const message = encodeAccountProof({address, nonce, appIdentifier}, false)

let signaturesArr = []
let keyIndices = []
const signaturesArr: string[] = []
const keyIndices: string[] = []

for (const el of signatures) {
signaturesArr.push(el.signature)
Expand All @@ -134,7 +157,7 @@ export async function verifyAccountProof(

return query({
cadence: await getVerifySignaturesScript(ACCOUNT_PROOF, opts),
args: (arg, t) => [
args: (arg: any, t: any) => [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary? If so, it seems like there might be another issue going on here. fcl.query args should be typed.

Copy link
Contributor Author

@mfbz mfbz May 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, i didn't refactor yet the exec folder that is where there is the query function!
When refactoring that, these params will be inferred from query params interface

Copy link
Contributor

@jribbink jribbink May 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, gotcha. Should have clarified - it's already typed through the JSDoc.

I just checked myself and you're able to remove the any annotations with no type errors here.

Screenshot 2025-05-23 at 12 01 00 AM

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did it! Thanks

arg(withPrefix(address), t.Address),
arg(message, t.String),
arg(keyIndices, t.Array(t.Int)),
Expand Down Expand Up @@ -164,12 +187,16 @@ export async function verifyAccountProof(
* {fclCryptoContract}
* )
*/
export async function verifyUserSignatures(message, compSigs, opts = {}) {
export async function verifyUserSignatures(
message: string,
compSigs: CompositeSignature[],
opts: VerifySignaturesScriptOptions = {}
): Promise<boolean> {
const address = withPrefix(compSigs[0].addr)
validateArgs({message, address, compSigs})

let signaturesArr = []
let keyIndices = []
const signaturesArr: string[] = []
const keyIndices: string[] = []

for (const el of compSigs) {
signaturesArr.push(el.signature)
Expand Down
5 changes: 5 additions & 0 deletions packages/fcl-core/src/fcl-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,8 @@ export {
export {execStrategy} from "./current-user/exec-service"

export type {StorageProvider} from "./utils/storage"

export type {
AccountProofData,
VerifySignaturesScriptOptions,
} from "./app-utils/verify-signatures"