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

Commit

Permalink
Merge pull request #25 from keep-network/put-down-small-bond
Browse files Browse the repository at this point in the history
Fetch deposit BTC address (contract code)
  • Loading branch information
nkuba authored Aug 7, 2019
2 parents e722bf7 + a680bec commit 8690899
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 38 deletions.
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
}
10 changes: 3 additions & 7 deletions client/src/FundingProof.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const BitcoinTxParser = require('tbtc-helpers').BitcoinTxParser
import { BitcoinTxParser } from 'tbtc-helpers'
const bitcoinspv = require('tbtc-helpers').BitcoinSPV

/**
Expand Down Expand Up @@ -32,7 +32,7 @@ async function getTransactionProof(electrumClient, txID, confirmations) {
* @param {string} txID Funding transaction ID.
* @param {number} fundingOutputIndex Position of a funding output in the transaction.
*/
async function calculateAndSubmitFundingProof(electrumClient, txID, fundingOutputIndex) {
export async function calculateAndSubmitFundingProof(electrumClient, txID, fundingOutputIndex) {
if (txID.length != 64) {
throw new Error(`invalid transaction id length [${txID.length}], required: [64]`)
}
Expand Down Expand Up @@ -61,8 +61,4 @@ async function calculateAndSubmitFundingProof(electrumClient, txID, fundingOutpu


// return eth transaction id to later convert it to etherscan link
}

module.exports = {
calculateAndSubmitFundingProof,
}
}
70 changes: 55 additions & 15 deletions client/src/FundingTransaction.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,56 @@
const Address = require('tbtc-helpers').Address
import { Address } from 'tbtc-helpers'
import { Deposit, TBTCSystem } from './eth/contracts'
const { Network, publicKeyToP2WPKHaddress, addressToScript } = Address

// 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
/**
* 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
}

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

// This function is used as a callback to electrum client. It is invoked when
// am existing or a new transaction is found.
Expand Down Expand Up @@ -69,7 +115,7 @@ async function watchForFundingTransaction(electrumClient, bitcoinAddress, expect
* TODO: When we increase required confirmations number above 1 we should probably
* emit an event for each new confirmation to update state in the web app.
*/
async function waitForConfirmations(electrumClient, transactionID) {
export async function waitForConfirmations(electrumClient, transactionID) {
const requiredConfirmations = 1 // TODO: This is simplification for demo

const checkConfirmations = async function() {
Expand All @@ -89,9 +135,3 @@ async function waitForConfirmations(electrumClient, transactionID) {

return confirmations
}

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
2 changes: 1 addition & 1 deletion lib/tbtc-helpers/src/ElectrumClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,4 +380,4 @@ function scriptToHash(script) {

module.exports = {
Config, Client, scriptToHash,
}
}
3 changes: 2 additions & 1 deletion 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",
"truffle": "^5.0.29",
"truffle-contract": "^4.0.26",
"web3": "^1.2.0"
Expand All @@ -26,7 +27,7 @@
"watch-css": "npm run build-css && less-watch-compiler src/css src app.less",
"start-js": "sleep 2 && react-scripts start",
"start": "npm-run-all -p watch-css start-js",
"build": "npm run build-css && react-scripts build",
"build": "npm run build-css && react-scripts --max_old_space_size=8192 build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
Expand Down

0 comments on commit 8690899

Please sign in to comment.