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

Acre SDK - staking module #106

Merged
merged 75 commits into from
Feb 7, 2024
Merged

Acre SDK - staking module #106

merged 75 commits into from
Feb 7, 2024

Commits on Feb 7, 2024

  1. Configuration menu
    Copy the full SHA
    8609c2d View commit details
    Browse the repository at this point in the history
  2. Add necessary wrappers

    Create wrappers for some of objects from `@keep-network/tbtc-v2.ts` lib
    to use in Acre SDK.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    6cf049a View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    c84ba51 View commit details
    Browse the repository at this point in the history
  4. Create ChainMessages interface

    The main idea is to hide the signing process under the exact
    implementation for a given chain. For example, we can use `ethers` lib
    for Ethereum implementation, but different lib for other chains. The
    Acre staking flow will require a signature and the signig message
    implementation will be used in the staking module.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    7a1619d View commit details
    Browse the repository at this point in the history
  5. Create Staking module

    Create a module that is responsible for initializing a stake for a
    receiver via tBTC depositor contract from `Acre`.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    155ce60 View commit details
    Browse the repository at this point in the history
  6. Add initializeEthereum to the Acre main class

    This function is responsible for creating the `Acre` SDK for any
    `Ethereum` network. It also creates necessary modules that allow
    developers to interact with Acre system.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    936c90e View commit details
    Browse the repository at this point in the history
  7. Add ethers lib to the SDK module

    We want to build the Ethereum implementation on top of the `ethers` lib.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    d298630 View commit details
    Browse the repository at this point in the history
  8. Configuration menu
    Copy the full SHA
    8cad4de View commit details
    Browse the repository at this point in the history
  9. Update chain messages interface

    Add `ChainSignedMessage` that represents a signature. This interface
    exposes `verify` function that returns an address of the private key
    that produced the signature. Also here we update the `ChainMessages`
    interface - `sign` function should return the `ChainSignedMessage`
    object.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    181b266 View commit details
    Browse the repository at this point in the history
  10. Add Ethereum implementation of messages

    Implement signing process for Ethereum chain. Here we use the `ethers`
    lib under the hood.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    91d4e42 View commit details
    Browse the repository at this point in the history
  11. Add receiver validation to stake function

    We make sure that the `receiver` address is equal to the address that
    produced the signature.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    20d6a83 View commit details
    Browse the repository at this point in the history
  12. Add core dependency to sdk

    We need types generated by typechain tool.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    d877929 View commit details
    Browse the repository at this point in the history
  13. Create Ethereum contract wrapper

    Create Ethereum contract wrapper using ethers. We do not want to expose
    ethers contract instance outside the SDK.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    4e70b9c View commit details
    Browse the repository at this point in the history
  14. Configuration menu
    Copy the full SHA
    0aa1ce3 View commit details
    Browse the repository at this point in the history
  15. Fix imports

    We should use our wrappers because we expose them outside the SDK.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    7fc2f8b View commit details
    Browse the repository at this point in the history
  16. Rename class

    `Staking` -> `StakeInitialization`. The previous name was confusing - we
    want to name classes as steps of the staking flow to make the sdk more
    dev-friendly.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    48d3c4b View commit details
    Browse the repository at this point in the history
  17. Update EthereumSigner type

    To simplify accept `Signer` or `VoidSigner` only. Developers can use
    `VoidSigner` from `ethers` if they want to initialize the Ethereum Acre
    SDK in readonly mode instead of `Provider`.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    22e6493 View commit details
    Browse the repository at this point in the history
  18. Use jest instead of chai and mocha

    `Jest` testing framework has built-in support for mocks, stubs, and
    spies. Chai requires the installation of additional dependencies but
    most of them are no longer maintained. I also tried using the `chai`
    with `ethereum-waffle` package, that adds support for mocking contracts,
    but it only supports ethers in v5, we want to use the latest version of
    ethers - it is v6.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    e92448c View commit details
    Browse the repository at this point in the history
  19. Configuration menu
    Copy the full SHA
    ab32db3 View commit details
    Browse the repository at this point in the history
  20. Rename interface

    `ChainMessages` -> `ChainMessageSigner`
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    dd89d50 View commit details
    Browse the repository at this point in the history
  21. Update unit test

    Add unit test for `EthereumTBTCDepositor.initializeStake` function.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    5273564 View commit details
    Browse the repository at this point in the history
  22. Configuration menu
    Copy the full SHA
    15aebb1 View commit details
    Browse the repository at this point in the history
  23. Add support for eip-712 typed messages

    The staking flow in Acre will require the typed messages so here we
    rename the `ChainMessageSigner` to `ChainEIP712Signer`. The Ethereum
    implementation uses the ethers lib to handle the signing and verifying
    process.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    e97b2a5 View commit details
    Browse the repository at this point in the history
  24. Update staking module

    Add `receiver` and `referral` params to the `initializeStake` function.
    We need to pass these values as parsed hex as extra data to the tBTC
    system. Currently left only TODO because we need to wait for extra data
    feature in tbtc-v2.ts lib. Also we decided to not include the signing
    process here but the `StakeInitialization` should be responsible for
    this.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    a4acb89 View commit details
    Browse the repository at this point in the history
  25. Update StakeInitialization

    Rename `getDepositAddress` -> `getBitcoinAddress` to better understand
    what type of address it is. It should be called as first step of the
    staking to get the Bitcoin address and send a given amount of BTC to a
    received address.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    230041f View commit details
    Browse the repository at this point in the history
  26. Update StakeInitialization constructor

    Add `receiver` and `referral` those values should be stored in this
    object to not pass them again to the `stake` function. Also here we
    update the type of `messageSigner`.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    d791c5c View commit details
    Browse the repository at this point in the history
  27. Add signMessage step to StakeInitialization

    User shuold sign message before the stake. We decided to implelemnt it
    as a separate function that should be called before stake because we
    want to give developers more flexibility. It's not a part of `stake`
    function because e.g. in the dapp after signing message there may be
    some additional steps. After signing we save the signed message in the
    instance of `StakeInitialization` which then can be used inside `stake`
    function w/o passing message to this function.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    85cb2e7 View commit details
    Browse the repository at this point in the history
  28. Add unit test for staking module

    Clean up tests by extracting test data to variables outside of the tests
    brackets. Add unit test for `StakeInitialization` object which is
    created by `StakingModule.initializeStake`.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    08b0872 View commit details
    Browse the repository at this point in the history
  29. Update test script in sdk package.json

    Use `--verbose` flag to display individual test results with the test
    suite hierarchy.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    7baf424 View commit details
    Browse the repository at this point in the history
  30. Rename lib dir

    `message-signer` -> `eip712-signer`. The staking flow will require only
    eip712 typed messages so we decided to rename to a more specific name.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    961c5e2 View commit details
    Browse the repository at this point in the history
  31. Configuration menu
    Copy the full SHA
    fcb76d3 View commit details
    Browse the repository at this point in the history
  32. Update unit tests of eip712

    Add unit test for Ethereum implementation of ChainSignedMessage.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    f264667 View commit details
    Browse the repository at this point in the history
  33. Add Bitcoin refund account property to stake msg

    Also leave a `TODO` not to revisit the message structure before the
    launch.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    b4fcf06 View commit details
    Browse the repository at this point in the history
  34. Leave TODO comment in stake fn

    Currently to do not block the integration SDK <-> dApp we want to send
    Ethereum transaction as part of the staking flow. In the final version
    users won't have to sign the Ethereum transaction, they will just send
    stake details to a relayer bot which will stake BTC on user's behalf.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    1103d0e View commit details
    Browse the repository at this point in the history
  35. Add fn to initialize the Acre SDK for Ethereum

    Based on the network param this function creates Acre SDK for supported
    Ethereum networks eg. `mainnet` or `sepolia`. Here we leave few TODOs
    that we should handle later once we get contract artifacts for different
    networks. Also we need to make sure the tBTC SDK will work ok with
    ethers v6 - we will check this while working on poc for Acre SDK and
    dApp integration.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    497e44e View commit details
    Browse the repository at this point in the history
  36. Update EthereumEIP712Signer implementation

    Get the chain id from `ethers.provider` instead of passing it to a
    `sign` fn in `domain` param. The main idea is to not expose
    chain-specific API outside the lib components. Modules should not know
    about ethers lib.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    3d98e48 View commit details
    Browse the repository at this point in the history
  37. Update eip712 signer tests

    Cover a case when ethers provider can not return chain id.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    e8d7d37 View commit details
    Browse the repository at this point in the history
  38. Add docs comments

    Add docs comments to TBTCDepositor interface and to the Ethereum
    implementation of this contract.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    9317fb3 View commit details
    Browse the repository at this point in the history
  39. Configuration menu
    Copy the full SHA
    53fa13a View commit details
    Browse the repository at this point in the history
  40. Add docs comments to eip-712 signer

    Add docs comments to abstract interfaces and to Ethereum implementation.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    0ba2893 View commit details
    Browse the repository at this point in the history
  41. Configuration menu
    Copy the full SHA
    bd7d335 View commit details
    Browse the repository at this point in the history
  42. Configuration menu
    Copy the full SHA
    c9020fd View commit details
    Browse the repository at this point in the history
  43. Fix linting errors in SDK tests

    Here we also disable `@typescript-eslint/unbound-method` rule for test
    files because it reporst that we use unbonded method for `expect`
    function from jest. For example, with this rule we can't do the following
    check:
    ```
    expect(...).toBeDefined()
    ```
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    614cf75 View commit details
    Browse the repository at this point in the history
  44. Add abstract ethers signer

    The tBTC-v2.ts SDK uses the ethers v5 and signers are not compatible
    with signers from ethers v6. Here we create an abstract signer that adds
    necessary methods (only used by tBTC-v2 SDK) to be compatible with
    ethers v5 signer which is used in the tBTC-v2.ts SDK.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    bc4ec4c View commit details
    Browse the repository at this point in the history
  45. Update initializeEthereum fn

    Use `EthereumSignerCompatibleWithEthersV5` type for `signer` param to
    force developers to pass signer compatible with signer used in
    tbtc-v2.ts SDK. Thansk to this we can pass signer from ethers v6 but it
    must implements necessary functions or extends
    `EthereumSignerCompatibleWithEthersV5` abstract class.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    ee03ee9 View commit details
    Browse the repository at this point in the history
  46. Bump ethers lib to 6.10.0 in SDK

    To use the latest version.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    f4e6dc9 View commit details
    Browse the repository at this point in the history
  47. Disble rule for import in tbtc-depositor.ts file

    Disable `import/no-unresolved` and leave a `TODO` to fix this issue.
    Disabling this rule for now because probably the path will change once
    we publish npm package for `core` module.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    c7941c9 View commit details
    Browse the repository at this point in the history
  48. Fix Ethereum implementation of initializeStake

    The ethers contract expects the address in prefixed format so we must
    add `0x` prefix. I've updated unit tests in one of the previous commits.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    0cdd4c5 View commit details
    Browse the repository at this point in the history
  49. Update the SDK tsconfig

    To be able to use the sdk in our dapp.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    d732d77 View commit details
    Browse the repository at this point in the history
  50. Rename variable

    `depositor` -> `tbtcDepositor` to match the contract name.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    c12643d View commit details
    Browse the repository at this point in the history
  51. Clearly specify the return type

    Specify the return type of the private static method in Acre class.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    d0e121d View commit details
    Browse the repository at this point in the history
  52. Rename class field and constructor param

    `messages` -> `messageSigner`.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    26389b0 View commit details
    Browse the repository at this point in the history
  53. Rename receiver -> staker

    We would require this account to sign the messages, so it makes more
    sense to name it `staker` rather than `receiver`.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    977062b View commit details
    Browse the repository at this point in the history
  54. Rename import and class field

    Use `TbtcDeposit` alias for `Deposit` import and rename the field in the
    `StakeInitialization` class to `tbtcDepositor`. To indicate this is
    deposit from tbtc-v2.ts SDK.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    46fa436 View commit details
    Browse the repository at this point in the history
  55. Set correct contract name in typed message

    The contract name is `TbtcDepositor` not `TBTCDepositor`.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    239d6ec View commit details
    Browse the repository at this point in the history
  56. Update the staking message format

    The message should contain the Bitcoin recovery address instead of the
    public key hash of this address.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    05a22f6 View commit details
    Browse the repository at this point in the history
  57. Bump tbtc-v2.ts lib to the latest version

    The latest version of the `tbtc-v2.ts` lib adds support for revealing
    deposit with extra data. We need this feature to stake BTC(tBTC) on
    user's behalf.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    1c24521 View commit details
    Browse the repository at this point in the history
  58. Update the TBTCDepositor interface

    The `TBTCDepositor` should implement the `DepositorProxy` interface to
    reveal the deposit with extra data on user's behalf. Here we also add
    `encodeExtraData` and `decodeExtraData` functions to encodes and decodes
    extra data passed to the tBTC Bridge contract. The Acre extra data is
    staker address and referral number.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    1e91791 View commit details
    Browse the repository at this point in the history
  59. Adjust staking module

    Adujst staking module to the new TBTCDepositor interface and the latest
    version of the `tbtc-v2.ts` lib. We need to initiate the deposit with
    extra data using the `tbtc-v2.ts` SDK by injecting our implementation of
    the `DepositorProxy`.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    968bdf7 View commit details
    Browse the repository at this point in the history
  60. Update staking module unit test

    Adjust unit tests to the latest version of tbtc-v2.ts lib that adds
    support for revealing deposits with extra data via depositor proxy.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    bbf93ff View commit details
    Browse the repository at this point in the history
  61. Simplify class constructor

    We can get the referral and staker from tBTC deposit object.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    54dabd2 View commit details
    Browse the repository at this point in the history
  62. Improve imports in Acre SDK

    Expose shared components, feature modules and the `Acre` entry point
    class from the root `index.ts` file. The `Acre` entry point class
    currently provides static function to initialize the Acre SDK for
    Ethereum network.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    a59aa37 View commit details
    Browse the repository at this point in the history
  63. Update message signing process in stake flow

    Pass the `bitcoinRecoveryAddress` to the `StakeInitialization` class.
    Since we can't easily determine if we should use `true` or `false` in
    `BitcoinAddressConverter.publicKeyHashToAddress` to convert public key
    hash to `P2PKH` or `P2WPKH` we can use what the input was w/o doing a
    conversion.
    
    Here we also get rid of:
    - the bitcoin client from the `StakeInitailization` class because at
      least for now it is unnecessary,
    - `referral` field - it is decoded and passed to the contract function
      under the hood.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    d29e0fb View commit details
    Browse the repository at this point in the history
  64. Fix SDK GH worklow

    We need `build` and `typechain` artifacts from `core` package in `sdk`
    because we import artifacts and types generated by `typechain` in `sdk`
    package. To achieve this, we extract a reusable workflow that builds the
    `core` package and uploads necessary artifacts. Then we use this
    reusable workflow to run successfully the `sdk` jobs.
    
    Here we also update the `jest` config - we should ignore tests inside
    `dist` dir if exists.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    df85827 View commit details
    Browse the repository at this point in the history
  65. Add comment to describe why we omit field

    We want to omit the `signerOrProvider` because it points to ethers v5.
    We use ethers v6 in Acre SDK.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    565a0a1 View commit details
    Browse the repository at this point in the history
  66. Update root .prettierignore file

    Ignore `sdk` monorepo package - `sdk` package has own prettier config.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    10d4bec View commit details
    Browse the repository at this point in the history
  67. Fix variable type in encodeExtraData fn

    According to the contract source code, the referral should be `uint16`.
    Here we also add more unit tests to cover more cases eg. when a referral
    is `0` (min `uint16`) and `65535` (max `uint16`).
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    d846701 View commit details
    Browse the repository at this point in the history
  68. Remove Goerli from supported Ethereum networks

    We do not expect to support it.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    06b7617 View commit details
    Browse the repository at this point in the history
  69. Add artifacts deployed on Sepolia to SDK

    And create the ethereum contract instance based on this artifact.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    8b202b8 View commit details
    Browse the repository at this point in the history
  70. Configuration menu
    Copy the full SHA
    e037f64 View commit details
    Browse the repository at this point in the history
  71. Imporve the stake message template

    `receiver` -> `ethereumStakerAddress`. Add more context to the name of
    field for clarity when user sees it on the device, so they know what the
    value means.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    ef9d7ee View commit details
    Browse the repository at this point in the history
  72. Validate if staker is a valid ethereum address

    Check if the staker address is a valid ethereum and non-zero address.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    96101e2 View commit details
    Browse the repository at this point in the history
  73. Configuration menu
    Copy the full SHA
    4bba1bd View commit details
    Browse the repository at this point in the history
  74. Add initial implementation of TbtcDepositor

    Based on the #91. We need this
    contract to get types generated by `typechain` and import them in `sdk`.
    Currently our SDK uses only `initializeStakeRequest` function and there
    is no need to add more functions like in #91.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    faa487c View commit details
    Browse the repository at this point in the history
  75. Update the StakeInitialization constructor

    Pass the `staker` value the same as was used in this function input
    parameter and use it insied `StakeInitialization` constructor to
    initialize the value of `#staker` field, instead of recovering it from
    the ` _tbtcDeposit.getReceipt()` extra data, as this is an external
    library that can be compromised and return invalid value.
    r-czajkowski committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    07e83b0 View commit details
    Browse the repository at this point in the history