Skip to content

Commit

Permalink
PKG -- [sdk] Allow for integer string acct.keyId in authorization fun…
Browse files Browse the repository at this point in the history
…ction
  • Loading branch information
jribbink committed May 17, 2022
1 parent b873a0f commit 0b224a5
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/grumpy-cats-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@onflow/sdk": patch
---

Allow for integer string account.keyId in authorization function
23 changes: 21 additions & 2 deletions packages/sdk/src/interaction/interaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down
46 changes: 43 additions & 3 deletions packages/sdk/src/interaction/interaction.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,43 @@
test("placeholder", () => {
expect(1).toBe(1)
})
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))
})
})

0 comments on commit 0b224a5

Please sign in to comment.