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

feature(sdk): Add support for signing v2 transactions #873

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

n8maninger
Copy link
Member

@n8maninger n8maninger commented Jan 9, 2025

Signing v2 transactions has a slightly different flow and structure from V1.

  1. Build the transaction
  2. Calculate the input sig hash
  3. Sign the input sighash for each siacoin and siafund input using its private key
const state, network; // should be retrieved from the API

const txn = {
    "siacoinInputs": [
      {
        "parent": {
          "id": "a2b01527c128eebd19056d5048d6b18b31df1fa9589993c55d50607dc097e69c",
          "stateElement": {
            "leafIndex": 4646,
            "merkleProof": [
              "aac548e6cdd2f05daa89ce83b1935bb1f2d4b363a1d27ed3158f8157d209eea7",
              "211e336ba260f72dbe8059e7e564414cda75386ebaf79720ee1078e6aa513e15",
              "3ad4326fae446f729679cdb80fd1ea55c184c025bf228cd4cf49471be9c517f6"
            ]
          },
          "siacoinOutput": {
            "value": "424999930000000000000000000000",
            "address": "2b8fc779876872055333fa0609f37a604be076e97bfeeab4d1c7df7d02f9cff3b8e6317047fa"
          },
          "maturityHeight": 0
        }
      }
    ],
    "siacoinOutputs": [
      {
        "value": "15000000000000000000000000000",
        "address": "2e5716a6ba598292b2c0233fcc9e21f992c5f536ae1e0634907855224d5e9216a0c1256e0085"
      },
      {
        "value": "409999918000000000000000000000",
        "address": "2b8fc779876872055333fa0609f37a604be076e97bfeeab4d1c7df7d02f9cff3b8e6317047fa"
      }
    ],
    "minerFee": "12000000000000000000000"
  },
  sigHash = wallet.v2TransactionInputSigHash(state, network, txn);

txn.siacoinInputs.forEach((input) => {
    const privateKey = getPrivateKeyForAddress(input.parent.siacoinOutput.address),
      unlockCondtions = getUnlockConditionsForAddress(input.parent.siacoinOutput.address); // I can't remember if you store this as the policy or unlock conditions on the address or in the local storage

    input.satisfiedPolicy = {
      "policy": {
        "type": "uc",
        "policy": unlockCondtions
      },
      "signatures": [
        wallet.signHash(privateKey, sigHash)
      ]
    };
});

txn.siafundInputs.forEach((input) => {
  const privateKey = getPrivateKeyForAddress(input.parent.siacoinOutput.address),
    unlockCondtions = getUnlockConditionsForAddress(input.parent.siacoinOutput.address);

  input.satisfiedPolicy = {
    "policy": {
      "type": "uc",
      "policy": unlockCondtions
    },
    "signatures": [
      wallet.signHash(privateKey, sigHash)
    ]
  };
});

/*
{
  "siacoinInputs": [
    {
      "parent": {
        "id": "a2b01527c128eebd19056d5048d6b18b31df1fa9589993c55d50607dc097e69c",
        "stateElement": {
          "leafIndex": 4646,
          "merkleProof": [
            "aac548e6cdd2f05daa89ce83b1935bb1f2d4b363a1d27ed3158f8157d209eea7",
            "211e336ba260f72dbe8059e7e564414cda75386ebaf79720ee1078e6aa513e15",
            "3ad4326fae446f729679cdb80fd1ea55c184c025bf228cd4cf49471be9c517f6"
          ]
        },
        "siacoinOutput": {
          "value": "424999930000000000000000000000",
          "address": "2b8fc779876872055333fa0609f37a604be076e97bfeeab4d1c7df7d02f9cff3b8e6317047fa"
        },
        "maturityHeight": 0
      },
      "satisfiedPolicy": {
        "policy": {
          "type": "uc",
          "policy": {
            "timelock": 0,
            "publicKeys": [
              "ed25519:9dd50ac053764d67b6bcee937a1186c7d3341daedd326f6022afca8c4021d6bb"
            ],
            "signaturesRequired": 1
          }
        },
        "signatures": [
          "c21ac65cd105460879dfdaa37cf7efabf42e7978f85c7e1fe73f95c4637b3e0b64dd43f052a9f59194abfc0ba678d4c9e9122a821c9a7a5b45c4c204e8d2470d"
        ]
      }
    }
  ],
  "siacoinOutputs": [
    {
      "value": "15000000000000000000000000000",
      "address": "2e5716a6ba598292b2c0233fcc9e21f992c5f536ae1e0634907855224d5e9216a0c1256e0085"
    },
    {
      "value": "409999918000000000000000000000",
      "address": "2b8fc779876872055333fa0609f37a604be076e97bfeeab4d1c7df7d02f9cff3b8e6317047fa"
    }
  ],
  "minerFee": "12000000000000000000000"
}
*/

I'm hoping you'll be able to use this API to construct the transaction instead of using /fund and /fundsf separately: SiaFoundation/walletd#211

The request body is the Siacoin and Siafund recipients. The response is an unsigned transaction.

Copy link

changeset-bot bot commented Jan 9, 2025

⚠️ No Changeset found

Latest commit: c6db29f

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

vercel bot commented Jan 9, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

5 Skipped Deployments
Name Status Preview Comments Updated (UTC)
explorer ⬜️ Ignored (Inspect) Visit Preview Jan 10, 2025 3:40pm
explorer-zen ⬜️ Ignored (Inspect) Visit Preview Jan 10, 2025 3:40pm
hostd ⬜️ Ignored (Inspect) Visit Preview Jan 10, 2025 3:40pm
renterd ⬜️ Ignored (Inspect) Visit Preview Jan 10, 2025 3:40pm
website ⬜️ Ignored (Inspect) Visit Preview Jan 10, 2025 3:40pm

@n8maninger n8maninger requested a review from alexfreska January 9, 2025 20:07
@n8maninger n8maninger self-assigned this Jan 9, 2025
Copy link
Member

@alexfreska alexfreska left a comment

Choose a reason for hiding this comment

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

Looks good, I'll give it a go and see if I hit any issues.

Just need to fix an issue with the commit formatting 😆:

feat(sdk): add support for signing v2 transactions

error:

⧗   input: feature(sdk): Add support for signing v2 transactions
✖   subject must not be sentence-case, start-case, pascal-case, upper-case [subject-case]
✖   type must be one of [build, chore, ci, docs, feat, fix, perf, refactor, revert, style, test] [type-enum]

✖   found 2 problems, 0 warnings

@n8maninger n8maninger force-pushed the nate/v2-signing branch 2 times, most recently from 1ed0bbe to c6db29f Compare January 10, 2025 15:44
@n8maninger n8maninger requested a review from alexfreska January 10, 2025 17:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In Review
Development

Successfully merging this pull request may close these issues.

2 participants