Skip to content
This repository has been archived by the owner on Mar 28, 2023. It is now read-only.

Fetch deposit BTC address (contract code) #25

Merged
merged 37 commits into from
Aug 7, 2019
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
3c6aa8f
Export initialiseDeposit, empty test
liamzebedee Aug 1, 2019
7069859
Implement getDepositBTCPublicKey and test
liamzebedee Aug 1, 2019
bbcf7d7
Now converting key to BTC address
liamzebedee Aug 2, 2019
2d82278
Merge artifact addresses
liamzebedee Aug 5, 2019
10a57fa
Simpler try/catch
liamzebedee Aug 5, 2019
9ce980f
Doc getDepositBTCPublicKey
liamzebedee Aug 5, 2019
328bc85
Doc watchForDepositBTCPublicKey
liamzebedee Aug 5, 2019
3982aad
Add type to param doc
liamzebedee Aug 5, 2019
cda8d02
Merge master
liamzebedee Aug 5, 2019
f9a1350
Manual linting
liamzebedee Aug 6, 2019
511bcd2
Refactor getDepositBtcAddress
liamzebedee Aug 6, 2019
cdb5023
Better self-doc'ing code for hex values in events
liamzebedee Aug 6, 2019
e6e238e
Refactor test to match new names
liamzebedee Aug 6, 2019
51b586f
Clearer comments
liamzebedee Aug 6, 2019
339f449
Comments on error handling
liamzebedee Aug 6, 2019
5d07f3d
Merge from master
liamzebedee Aug 6, 2019
801c02c
Organise imports
liamzebedee Aug 6, 2019
62beb66
Remove redundant code
liamzebedee Aug 6, 2019
d021252
Fix doc format
liamzebedee Aug 6, 2019
567c7d2
Remove artifacts
liamzebedee Aug 6, 2019
009551e
Merge from master
liamzebedee Aug 6, 2019
013eca2
Fix import path
liamzebedee Aug 6, 2019
22babe9
Make index.js the main import
liamzebedee Aug 6, 2019
bfda437
Test path issues with CI build
liamzebedee Aug 6, 2019
d5819d3
Move back index.js to avoid test refactoring
liamzebedee Aug 6, 2019
030b620
Add Address back to imports
nkuba Aug 6, 2019
4e327d0
Consistent export syntax
liamzebedee Aug 6, 2019
729aa1e
Remove tbtc-helpers, use standard path for index.js
liamzebedee Aug 6, 2019
d6485e5
Revert export changes
liamzebedee Aug 6, 2019
3166d45
Remove index.js, import with simple paths
liamzebedee Aug 6, 2019
37a4c6f
Fix bitcoinspv import
liamzebedee Aug 6, 2019
fc5d82e
Add index.js, again
liamzebedee Aug 6, 2019
cb2b709
Fix import syntax
liamzebedee Aug 6, 2019
d1d5288
Another permutation of "main" and import path
liamzebedee Aug 6, 2019
93230da
Add max-old-space-size to build command
nkuba Aug 7, 2019
b9df389
Merge remote-tracking branch 'origin/master' into put-down-small-bond
nkuba Aug 7, 2019
a680bec
Fix exports in FundingTransaction
nkuba Aug 7, 2019
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
8 changes: 1 addition & 7 deletions client/src/Deposit.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
TBTCSystem,
TBTCToken,
KeepBridge,
DepositFactory
} from './eth/contracts'
import { DepositFactory, KeepBridge, TBTCSystem, TBTCToken } from './eth/contracts';

/**
* Creates a new deposit and returns its address
Expand Down Expand Up @@ -32,6 +27,5 @@ export async function createDeposit() {
})

const depositAddress = logs[0].args.depositCloneAddress

return depositAddress
}
73 changes: 59 additions & 14 deletions client/src/FundingTransaction.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,61 @@
const Address = require('tbtc-helpers').Address
Copy link
Member

Choose a reason for hiding this comment

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

CI is failing because of this removal.

import { Network, publicKeyToP2WPKHaddress } from 'tbtc-helpers'
import { Deposit, TBTCSystem } from './eth/contracts'

// PAGE 3: Pay BTC
async function getAddress(depositAddress) {
// TODO: Implement:
// 1. Get keep public key from the deposit
// 2. Calculate P2WPKH address from the key

Copy link
Member

Choose a reason for hiding this comment

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

So many blank lines, let's remove them in one of the upcoming PRs.



/**
* Requests a Bitcoin public key for a Deposit and returns it as a Bitcoin address
* @param {string} depositAddress the address of a Deposit contract
* @return {string} a bech32-encoded Bitcoin address, generated from a SegWit P2WPKH script
*/
export async function getDepositBtcAddress(depositAddress) {
const tbtcSystem = await TBTCSystem.deployed()

// 1. Request public key from the deposit
const deposit = await Deposit.at(depositAddress)

console.log(`Call getPublicKey for deposit [${deposit.address}]`)

await deposit.retrieveSignerPubkey()
.catch((err) => {
// This can happen when the public key was already retrieved before
// and we may succeed to get it with tbtcSystem.getPastEvents in the following lines
// TODO: there may be other errors that this allows to pass, refactor in future
console.error(`retrieveSignerPubkey failed: ${err}`)
})

// 2. Parse the logs to get the public key
// since the public key event is emitted in another contract, we
// can't get this from result.logs
const eventList = await tbtcSystem.getPastEvents(
'RegisteredPubkey',
{
fromBlock: '0',
toBlock: 'latest',
filter: { _depositContractAddress: depositAddress }
}
)

if (eventList.length == 0) {
throw new Error(`couldn't find RegisteredPubkey event for deposit address: ${depositAddress}`)
}

let publicKeyX = eventList[0].args._signingGroupPubkeyX
let publicKeyY = eventList[0].args._signingGroupPubkeyY
publicKeyX = publicKeyX.replace('0x', '')
publicKeyY = publicKeyY.replace('0x', '')

console.log(`Registered public key for deposit ${depositAddress}:\nX: ${publicKeyX}\nY: ${publicKeyY}`)

const btcAddress = publicKeyToP2WPKHaddress(
`${publicKeyX}${publicKeyY}`,
Network.testnet
)
return btcAddress
}


Copy link
Member

Choose a reason for hiding this comment

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

Same here about blank lines.

/**
* Funding transaction details.
* @typedef FundingTransaction
Expand All @@ -23,12 +72,12 @@ async function getAddress(depositAddress) {
* @param {number} expectedValue Expected transaction output value (satoshis).
* @return {FundingTransaction} Transaction details.
*/
async function watchForFundingTransaction(electrumClient, bitcoinAddress, expectedValue) {
export async function watchForFundingTransaction(electrumClient, bitcoinAddress, expectedValue) {
const script = Address.addressToScript(bitcoinAddress)

// This function is used as a callback to electrum client. It is invoked when
// am existing or a new transaction is found.
const findFundingTransaction = async function(status) {
const findFundingTransaction = async function (status) {
// Check if status is null which means there are not transactions for the
// script.
if (status == null) {
Expand Down Expand Up @@ -62,12 +111,8 @@ async function watchForFundingTransaction(electrumClient, bitcoinAddress, expect
}

// PAGE 4. WAITING FOR CONFIRMATIONS
async function waitForConfirmations(transactionID) {
export async function waitForConfirmations(transactionID) {
// TODO: Implement:
// 1. Wait for required number of confirmations for the transaction
// 2. Monitor confirmations on the chain and return when ready
}

module.exports = {
getAddress, watchForFundingTransaction, waitForConfirmations,
}
}
1 change: 1 addition & 0 deletions client/src/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './Deposit'
export * from './FundingTransaction'
export * from './eth/contracts'
17 changes: 10 additions & 7 deletions client/test/DepositTest.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { createDeposit, setDefaults } from '../src'
import { createDeposit, setDefaults, getDepositBtcAddress } from '../src'

import {
TBTCSystem,
TBTCToken,
KeepBridge,
Deposit
Deposit
} from '../src/eth/contracts'

const Web3 = require('web3')
Expand All @@ -29,7 +26,7 @@ describe.skip('Ethereum helpers', async () => {
Web3.providers.HttpProvider.prototype.sendAsync = Web3.providers.HttpProvider.prototype.send

web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'))
await setDefaults(web3)
await setDefaults(web3)
})

it('#createDeposit', async () => {
Expand All @@ -38,4 +35,10 @@ describe.skip('Ethereum helpers', async () => {

expect(await deposit.getCurrentState()).to.eq.BN('1')
})
})

it('#getDepositBtcAddress', async () => {
const depositAddress = await createDeposit()
const btcAddress = await getDepositBtcAddress(depositAddress)
expect(btcAddress.substring(0, 2)).to.equal('tb')
})
})
1 change: 1 addition & 0 deletions lib/tbtc-helpers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "tbtc-helpers",
"version": "0.0.1",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha --timeout 5000",
"js:lint": "eslint .",
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"redux": "^4.0.4",
"redux-saga": "^1.0.5",
"tbtc-client": "file:client",
"tbtc-helpers": "file:lib/tbtc-helpers",
Copy link
Member

Choose a reason for hiding this comment

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

Do we need this dependency in dapp's package.json? We should be using only tbtc-client.

"truffle": "^5.0.29",
"truffle-contract": "^4.0.26",
"web3": "^1.2.0"
Expand Down