From 0b224a52c8df625a54d329249a987ce8b1d71095 Mon Sep 17 00:00:00 2001 From: Jordan Ribbink Date: Tue, 17 May 2022 02:18:31 -0700 Subject: [PATCH] PKG -- [sdk] Allow for integer string acct.keyId in authorization function --- .changeset/grumpy-cats-flash.md | 5 ++ packages/sdk/src/interaction/interaction.js | 23 +++++++++- .../sdk/src/interaction/interaction.test.js | 46 +++++++++++++++++-- 3 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 .changeset/grumpy-cats-flash.md diff --git a/.changeset/grumpy-cats-flash.md b/.changeset/grumpy-cats-flash.md new file mode 100644 index 000000000..321c9dd71 --- /dev/null +++ b/.changeset/grumpy-cats-flash.md @@ -0,0 +1,5 @@ +--- +"@onflow/sdk": patch +--- + +Allow for integer string account.keyId in authorization function diff --git a/packages/sdk/src/interaction/interaction.js b/packages/sdk/src/interaction/interaction.js index f8676e820..99b3dc15c 100644 --- a/packages/sdk/src/interaction/interaction.js +++ b/packages/sdk/src/interaction/interaction.js @@ -138,6 +138,16 @@ const makeIx = wat => ix => { return Ok(ix) } +const prepAccountKeyId = acct => { + if (!acct.keyId) return acct + + invariant(Number.isInteger(+acct.keyId), "account.keyId must be an integer") + return { + ...acct, + keyId: +acct.keyId, + } +} + export const prepAccount = (acct, opts = {}) => ix => { invariant( typeof acct === "function" || typeof acct === "object", @@ -149,9 +159,16 @@ export const prepAccount = (acct, opts = {}) => ix => { const role = opts.role const tempId = uuid() - if (acct.authorization && isFn(acct.authorization)) acct = {resolve: acct.authorization} + if (acct.authorization && isFn(acct.authorization)) + acct = {resolve: acct.authorization} if (!acct.authorization && isFn(acct)) acct = {resolve: acct} + const resolve = acct.resolve + if (resolve) + acct.resolve = acct => + [resolve, prepAccountKeyId].reduce((d, fn) => fn(d), acct) + acct = prepAccountKeyId(acct) + ix.accounts[tempId] = { ...ACCOUNT, tempId, @@ -184,7 +201,9 @@ export const makeArgument = arg => ix => { ix.arguments[tempId].asArgument = arg.asArgument ix.arguments[tempId].xform = arg.xform ix.arguments[tempId].resolve = arg.resolve - ix.arguments[tempId].resolveArgument = isFn(arg.resolveArgument) ? arg.resolveArgument.bind(arg) : arg.resolveArgument + ix.arguments[tempId].resolveArgument = isFn(arg.resolveArgument) + ? arg.resolveArgument.bind(arg) + : arg.resolveArgument return Ok(ix) } diff --git a/packages/sdk/src/interaction/interaction.test.js b/packages/sdk/src/interaction/interaction.test.js index 5bb404e8a..50026c77f 100644 --- a/packages/sdk/src/interaction/interaction.test.js +++ b/packages/sdk/src/interaction/interaction.test.js @@ -1,3 +1,43 @@ -test("placeholder", () => { - expect(1).toBe(1) -}) \ No newline at end of file +import "jest" +import {resolveAccounts} from "../sdk" +import {interaction, pipe, prepAccount} from "./interaction" + +describe("prepAccount", () => { + test("prepAccount converts account object keyId to integer", async () => { + const keyId = "1" + const acct = { + addr: "f8d6e0586b0a20c7", + keyId, + signingFunction: () => ({ + addr: "f8d6e0586b0a20c7", + signature: "abc123", + }), + } + + const ix = prepAccount(acct, {role: "proposer"})({accounts: {}}) + expect(ix.accounts[ix.proposer].keyId).toBe(parseInt(keyId)) + }) + + test("prepAccount converts authorization function keyId to integer", async () => { + const keyId = "1" + const authz = acct => { + return { + ...acct, + addr: "f8d6e0586b0a20c7", + keyId, + signingFunction: () => ({ + addr: "f8d6e0586b0a20c7", + signature: "abc123", + }), + } + } + + const ix = await resolveAccounts( + prepAccount(authz, {role: "proposer"})({ + accounts: {}, + }) + ) + ix.accounts[ix.proposer] = ix.accounts[ix.proposer].resolve() + expect(ix.accounts[ix.proposer].keyId).toBe(parseInt(keyId)) + }) +})