Skip to content
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

PKG -- [sdk] Allow for integer string acct.keyId in authorization function #1191

Merged
merged 9 commits into from May 27, 2022
5 changes: 5 additions & 0 deletions .changeset/grumpy-cats-flash.md
@@ -0,0 +1,5 @@
---
"@onflow/sdk": patch
---

Allow for integer string account.keyId in authorization function
26 changes: 24 additions & 2 deletions packages/sdk/src/interaction/interaction.js
Expand Up @@ -138,6 +138,16 @@ const makeIx = wat => ix => {
return Ok(ix)
}

const prepAccountKeyId = acct => {
if (acct.keyId == null || acct.keyId == undefined) return acct
jribbink marked this conversation as resolved.
Show resolved Hide resolved

jribbink marked this conversation as resolved.
Show resolved Hide resolved
invariant(!isNaN(parseInt(acct.keyId)), "account.keyId must be an integer")
return {
...acct,
keyId: parseInt(acct.keyId),
}
}

export const prepAccount = (acct, opts = {}) => ix => {
invariant(
typeof acct === "function" || typeof acct === "object",
Expand All @@ -149,9 +159,19 @@ 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)
jribbink marked this conversation as resolved.
Show resolved Hide resolved
acct.resolve = async acct =>
await [resolve, prepAccountKeyId].reduce(
jribbink marked this conversation as resolved.
Show resolved Hide resolved
async (d, fn) => fn(await d),
acct
)
acct = prepAccountKeyId(acct)

ix.accounts[tempId] = {
...ACCOUNT,
tempId,
Expand Down Expand Up @@ -184,7 +204,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
jribbink marked this conversation as resolved.
Show resolved Hide resolved

return Ok(ix)
}
Expand Down
45 changes: 42 additions & 3 deletions packages/sdk/src/interaction/interaction.test.js
@@ -1,3 +1,42 @@
test("placeholder", () => {
expect(1).toBe(1)
})
import {resolveAccounts} from "../sdk"
import {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] = await ix.accounts[ix.proposer].resolve()
expect(ix.accounts[ix.proposer].keyId).toBe(parseInt(keyId))
})
jribbink marked this conversation as resolved.
Show resolved Hide resolved
})