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

Bitcoin native experience #371

Merged
merged 53 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
2d624f4
Add `@orangekit/sdk` dependency
r-czajkowski Apr 18, 2024
7f7cbff
Init the OrangeKit SDK in Acre SDK
r-czajkowski Apr 18, 2024
37a58f8
Update the staking module
r-czajkowski Apr 18, 2024
c7733c9
Add unit test for integration with OrangeKit
r-czajkowski Apr 29, 2024
5b1d724
Merge branch 'main' into bitcoin-native-experience
r-czajkowski May 7, 2024
55ea425
Add `@swan-bitcoin/xpub-lib`
r-czajkowski May 7, 2024
d2027a8
Add `BitcoinProvider` interface
r-czajkowski May 7, 2024
0a80e29
Implement `LedgerLiveWalletApiBitcoinProvider`
r-czajkowski May 7, 2024
260877b
Update the `ethereum-signer` utils in SDK
r-czajkowski May 8, 2024
df7380c
Use `BitcoinProvider` to initialize the Acre SDK
r-czajkowski May 8, 2024
0965b4c
Update staking unit tests
r-czajkowski May 9, 2024
da302d8
Update docs
r-czajkowski May 9, 2024
aac6f6f
Rename variable
r-czajkowski May 9, 2024
d2f3a51
Remove @babel/preset-env
nkuba May 10, 2024
ddfaae4
Use JestConfigWithTsJest type in jest config file
nkuba May 10, 2024
553b5be
Fix Jest and @orangekit/sdk support
nkuba May 10, 2024
a3b16d1
Fix Jest and @orangekit/sdk support (#418)
r-czajkowski May 12, 2024
0614e45
Lock the version of `@swan-bitcoin/xpub-lib` lib
r-czajkowski May 14, 2024
6c2971c
Update constructo parameters names
r-czajkowski May 14, 2024
a48e63b
Update Acre SDK initialization fn
r-czajkowski May 14, 2024
769b1ba
Simplify custom ethereum signer utils
r-czajkowski May 14, 2024
c6dfc24
Avoid skipping `import/prefer-default-export` rule
r-czajkowski May 14, 2024
ce33d42
Add docs for bitcoin provider interfaces
r-czajkowski May 14, 2024
1a84644
Update the `Network` type in Ledger Live Provider
r-czajkowski May 14, 2024
14f572d
Update `@swan-bitcoin/xpub-lib` declaration types
r-czajkowski May 14, 2024
32ebcef
Rename variable
r-czajkowski May 14, 2024
4f0a13a
Merge branch 'main' into bitcoin-native-experience
r-czajkowski May 14, 2024
8bdb895
Fix linting errors
r-czajkowski May 14, 2024
855dc38
Update the docs in Ledger Live Bitcoin Provider
r-czajkowski May 14, 2024
3df3cf7
Update `getAddress` fn
r-czajkowski May 14, 2024
097cac2
Update `getAddress` fn in Ledger Live provider
r-czajkowski May 15, 2024
196b94f
Leave TODO in `LedgerLiveWalletApiBitcoinProvider`
r-czajkowski May 15, 2024
46ed33f
Remove Ethereum account from the wallet context
r-czajkowski Apr 24, 2024
315b545
Initialize the Acre SDK w/o Ethereum address
r-czajkowski Apr 24, 2024
bb91477
Init the Acre SDK in dapp w/o ethereum address
r-czajkowski May 8, 2024
9fb193c
Update Acre SDK initialization in dapp
r-czajkowski May 15, 2024
3cdb91e
Remove unused ethers signer for Ledger Live
r-czajkowski May 15, 2024
6499c9a
Mock `@orangekit/sdk` to run jest test
r-czajkowski May 15, 2024
4f05fe8
Fix formatting error
r-czajkowski May 15, 2024
c90a92d
Remove Ethereum account from wallet context (#379)
nkuba May 15, 2024
18170eb
Merge branch 'main' into bitcoin-native-experience
r-czajkowski May 15, 2024
30526c7
Fix `useInitializeAcreSdk` hook
r-czajkowski May 15, 2024
f351fa4
Rename variable
r-czajkowski May 16, 2024
a51688e
Update docs
r-czajkowski May 16, 2024
56aa212
Update `IEthereumSignerCompatibleWithEthersV5`
r-czajkowski May 16, 2024
c6228a7
Add unit tests for Ledger Live Wallet Api Provider
r-czajkowski May 21, 2024
34d8350
Merge branch 'main' into bitcoin-native-experience
r-czajkowski May 21, 2024
9c1c45b
Merge branch 'main' into bitcoin-native-experience
r-czajkowski May 29, 2024
37db599
Simplify the bitcoin network type in SDK
r-czajkowski May 29, 2024
60a2bf5
Simplify the the Acre initialization function
r-czajkowski May 29, 2024
219127e
Rename variables
r-czajkowski May 29, 2024
f0fecf0
Leave a `TODO` in the `BitcoinProvider` interface
r-czajkowski May 29, 2024
60c1231
Merge branch 'main' into bitcoin-native-experience
nkuba May 29, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions dapp/src/acre-react/hooks/useStakeFlow.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { useCallback, useState } from "react"
import {
StakeInitialization,
EthereumAddress,
DepositorProxy,
DepositReceipt,
} from "@acre-btc/sdk"
import { useAcreContext } from "./useAcreContext"

export type UseStakeFlowReturn = {
initStake: (
bitcoinRecoveryAddress: string,
ethereumAddress: string,
bitcoinAddress: string,
referral: number,
depositor?: DepositorProxy,
) => Promise<void>
Expand All @@ -33,16 +31,14 @@ export function useStakeFlow(): UseStakeFlowReturn {

const initStake = useCallback(
async (
bitcoinRecoveryAddress: string,
ethereumAddress: string,
bitcoinAddress: string,
referral: number,
depositor?: DepositorProxy,
) => {
if (!acre || !isInitialized) throw new Error("Acre SDK not defined")

const initializedStakeFlow = await acre.staking.initializeStake(
bitcoinRecoveryAddress,
EthereumAddress.from(ethereumAddress),
bitcoinAddress,
referral,
depositor,
)
Expand Down
55 changes: 55 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
"typescript": "^5.3.2"
},
"dependencies": {
"@keep-network/tbtc-v2.ts": "2.4.0-dev.3",
"@acre-btc/contracts": "workspace:*",
"@keep-network/tbtc-v2.ts": "2.4.0-dev.3",
"@orangekit/sdk": "1.0.0-beta.8",
"ethers": "^6.10.0"
}
}
19 changes: 18 additions & 1 deletion sdk/src/acre.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { TBTC } from "@keep-network/tbtc-v2.ts"
import { OrangeKitSdk } from "@orangekit/sdk"
import { AcreContracts } from "./lib/contracts"
import { ChainEIP712Signer } from "./lib/eip712-signer"
import {
Expand All @@ -12,6 +13,8 @@ import { EthereumSignerCompatibleWithEthersV5 } from "./lib/utils"
class Acre {
readonly #tbtc: TBTC

readonly #orangeKit: OrangeKitSdk

readonly #messageSigner: ChainEIP712Signer

public readonly contracts: AcreContracts
Expand All @@ -22,26 +25,32 @@ class Acre {
_contracts: AcreContracts,
_messageSigner: ChainEIP712Signer,
_tbtc: TBTC,
_orangeKit: OrangeKitSdk,
) {
this.contracts = _contracts
this.#tbtc = _tbtc
this.#orangeKit = _orangeKit
this.#messageSigner = _messageSigner
this.staking = new StakingModule(
this.contracts,
this.#messageSigner,
this.#tbtc,
this.#orangeKit,
)
}

static async initializeEthereum(
nkuba marked this conversation as resolved.
Show resolved Hide resolved
signer: EthereumSignerCompatibleWithEthersV5,
network: EthereumNetwork,
): Promise<Acre> {
const chainId = await signer.getChainId()

const tbtc = await Acre.#getTBTCEthereumSDK(signer, network)
const orangeKit = await Acre.#getOrangeKitSDK(chainId)
const contracts = getEthereumContracts(signer, network)
const messages = new EthereumEIP712Signer(signer)

return new Acre(contracts, messages, tbtc)
return new Acre(contracts, messages, tbtc, orangeKit)
}

static #getTBTCEthereumSDK(
Expand All @@ -62,6 +71,14 @@ class Acre {
return TBTC.initializeMainnet(signer)
}
}

static #getOrangeKitSDK(chainId: number): Promise<OrangeKitSdk> {
nkuba marked this conversation as resolved.
Show resolved Hide resolved
return OrangeKitSdk.init(
chainId,
// TODO: pass rpc url as config.
"https://eth-mainnet.g.alchemy.com/v2/<YOUR_API_KEY>",
)
}
}

// eslint-disable-next-line import/prefer-default-export
Expand Down
56 changes: 43 additions & 13 deletions sdk/src/modules/staking/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { ChainIdentifier, TBTC } from "@keep-network/tbtc-v2.ts"
import {
ChainIdentifier,
EthereumAddress,
TBTC,
} from "@keep-network/tbtc-v2.ts"
import { OrangeKitSdk } from "@orangekit/sdk"
import { AcreContracts, DepositorProxy, DepositFees } from "../../lib/contracts"
import { ChainEIP712Signer } from "../../lib/eip712-signer"
import { StakeInitialization } from "./stake-initialization"
Expand Down Expand Up @@ -32,43 +37,68 @@ class StakingModule {
*/
readonly #tbtc: TBTC

/**
* OrangeKit SDK.
*/
readonly #orangeKit: OrangeKitSdk

constructor(
_contracts: AcreContracts,
_messageSigner: ChainEIP712Signer,
_tbtc: TBTC,
_orangeKit: OrangeKitSdk,
) {
this.#contracts = _contracts
this.#messageSigner = _messageSigner
this.#tbtc = _tbtc
this.#orangeKit = _orangeKit
}

/**
* Initializes the Acre staking process.
* @param bitcoinRecoveryAddress P2PKH or P2WPKH Bitcoin address that can be
* used for emergency recovery of the deposited
* funds.
* @param staker The address to which the stBTC shares will be minted.
* Initializes the Acre deposit process.
* @param depositor The Bitcoin depositor address. Supported addresses:
* `P2WPKH`, `P2PKH`, `P2SH-P2WPKH`.
* @param referral Data used for referral program.
* @param depositorProxy Depositor proxy used to initiate the deposit.
* @returns Object represents the staking process.
* @param bitcoinRecoveryAddress `P2PKH` or `P2WPKH` Bitcoin address that can
* be used for emergency recovery of the deposited funds. If
nkuba marked this conversation as resolved.
Show resolved Hide resolved
* `undefined` the `depositor` address is used as bitcoin recovery
* address then the `depositor` must be `P2WPKH` or `P2PKH`.
* @returns Object represents the deposit process.
*/
async initializeStake(
bitcoinRecoveryAddress: string,
staker: ChainIdentifier,
depositor: string,
referral: number,
nkuba marked this conversation as resolved.
Show resolved Hide resolved
depositorProxy?: DepositorProxy,
bitcoinRecoveryAddress?: string,
) {
// TODO: If we want to handle other chains we should create the wrapper for
// OrangeKit SDK to return `ChainIdentifier` from `predictAddress` fn. Or we
// can create `EVMChainIdentifier` class and use it as a type in `modules`
// and `lib`. Currently we support only `Ethereum` so here we force to
// `EthereumAddress`.
const depositOwnerChainAddress = EthereumAddress.from(
nkuba marked this conversation as resolved.
Show resolved Hide resolved
await this.#orangeKit.predictAddress(depositor),
nkuba marked this conversation as resolved.
Show resolved Hide resolved
)

// tBTC-v2 SDK will handle Bitcoin address validation and throw an error if
// address is not supported.
const finalBitcoinRecoveryAddress = bitcoinRecoveryAddress ?? depositor

const deposit = await this.#tbtc.deposits.initiateDepositWithProxy(
bitcoinRecoveryAddress,
finalBitcoinRecoveryAddress,
depositorProxy ?? this.#contracts.bitcoinDepositor,
this.#contracts.bitcoinDepositor.encodeExtraData(staker, referral),
this.#contracts.bitcoinDepositor.encodeExtraData(
depositOwnerChainAddress,
referral,
),
)

return new StakeInitialization(
this.#contracts,
this.#messageSigner,
bitcoinRecoveryAddress,
staker,
finalBitcoinRecoveryAddress,
depositOwnerChainAddress,
deposit,
)
}
Expand Down
10 changes: 4 additions & 6 deletions sdk/test/modules/staking.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BitcoinTxHash } from "@keep-network/tbtc-v2.ts"
import { ethers } from "ethers"
import { OrangeKitSdk } from "@orangekit/sdk"
import {
AcreContracts,
StakingModule,
Expand Down Expand Up @@ -100,11 +101,13 @@ describe("Staking", () => {
const contracts: AcreContracts = new MockAcreContracts()
const messageSigner = new MockMessageSigner()
const tbtc = new MockTBTC()
const orangeKit = {} as OrangeKitSdk

const staking: StakingModule = new StakingModule(
contracts,
messageSigner,
tbtc,
orangeKit,
)

describe("initializeStake", () => {
Expand Down Expand Up @@ -142,11 +145,7 @@ describe("Staking", () => {

messageSigner.sign = jest.fn().mockResolvedValue(mockedSignedMessage)

result = await staking.initializeStake(
bitcoinRecoveryAddress,
staker,
referral,
)
result = await staking.initializeStake(bitcoinRecoveryAddress, referral)
})

it("should encode extra data", () => {
Expand Down Expand Up @@ -370,7 +369,6 @@ describe("Staking", () => {

result = await staking.initializeStake(
bitcoinRecoveryAddress,
staker,
referral,
customDepositorProxy,
)
Expand Down
Loading