Skip to content

Commit

Permalink
Merge branch 'dev-alessandro'
Browse files Browse the repository at this point in the history
  • Loading branch information
allemanfredi committed Feb 22, 2021
2 parents 1bd2bbf + 8b770be commit 34bb47b
Show file tree
Hide file tree
Showing 90 changed files with 2,103 additions and 905 deletions.
3 changes: 2 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"tabWidth": 2,
"semi": false,
"singleQuote": true,
"bracketSpacing": true
"bracketSpacing": true,
"printWidth": 120
}
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"packages": [
"packages/*"
],
"version": "0.8.0"
"version": "0.9.0"
}
8 changes: 4 additions & 4 deletions packages/ptokens-deposit-address/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ptokens-deposit-address",
"version": "0.8.0",
"version": "0.9.0",
"description": "repo holding the code related to deposit addresses",
"repository": "https://github.com/provable-things/ptokens.js/tree/master/packages/ptokens-deposit-address",
"main": "dist/ptokens-deposit-address.cjs.js",
Expand Down Expand Up @@ -32,9 +32,9 @@
"bitcoinjs-lib": "^5.1.6",
"chai": "^4.2.0",
"eosjs": "^20.0.3",
"ptokens-node": "^0.8.0",
"ptokens-providers": "^0.8.0",
"ptokens-utils": "^0.8.0",
"ptokens-node": "^0.9.0",
"ptokens-providers": "^0.9.0",
"ptokens-utils": "^0.9.0",
"web3": "^1.2.2"
},
"devDependencies": {
Expand Down
173 changes: 67 additions & 106 deletions packages/ptokens-deposit-address/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const POLLING_TIME = 3000

const confirmations = {
btc: 1,
ltc: 4
ltc: 4,
doge: 1
}

// NOTE: will be removed in versions >= 1.0.0
Expand Down Expand Up @@ -42,19 +43,43 @@ bitcoin.networks.litecoinTestnet = {
wif: 0xb0
}

bitcoin.networks.dogecoin = {
messagePrefix: '\x19Dogecoin Signed Message:\n',
bip32: {
public: 0x02facafd,
private: 0x02fac398
},
pubKeyHash: 0x1e,
scriptHash: 0x16,
wif: 0x9e
}

const {
constants: {
networks: { BitcoinMainnet, BitcoinTestnet, LitecoinMainnet, LitecoinTestnet, DogecoinMainnet },
blockchains: { Bitcoin, Litecoin, Dogecoin }
}
} = utils
const NETWORKS = {
[Bitcoin]: {
[BitcoinMainnet]: bitcoin.networks.bitcoin,
[BitcoinTestnet]: bitcoin.networks.testnet
},
[Litecoin]: {
[LitecoinMainnet]: bitcoin.networks.litecoin,
[LitecoinTestnet]: bitcoin.networks.litecoinTestnet
},
[Dogecoin]: {
[DogecoinMainnet]: bitcoin.networks.dogecoin
}
}

export class DepositAddress {
/**
* @param {Object} _configs
*/
constructor(_configs) {
const {
nativeBlockchain,
nativeNetwork,
hostBlockchain,
hostNetwork,
hostApi,
node
} = _configs
const { nativeBlockchain, nativeNetwork, hostBlockchain, hostNetwork, hostApi, node } = _configs

this.hostBlockchain = hostBlockchain
this.hostNetwork = hostNetwork
Expand All @@ -68,31 +93,21 @@ export class DepositAddress {
* @param {String} _hostAddress
*/
async generate(_hostAddress) {
if (
this.hostBlockchain === utils.constants.blockchains.Ethereum &&
!Web3Utils.isAddress(_hostAddress)
)
if (this.hostBlockchain === utils.constants.blockchains.Ethereum && !Web3Utils.isAddress(_hostAddress))
throw new Error('Invalid Ethereum Address')

if (
this.hostBlockchain === utils.constants.blockchains.Eosio &&
!utils.eos.isValidAccountName(_hostAddress)
)
if (this.hostBlockchain === utils.constants.blockchains.Eosio && !utils.eos.isValidAccountName(_hostAddress))
throw new Error('Invalid EOS Account')

try {
const {
nonce,
enclavePublicKey,
nativeDepositAddress
} = await this.node.getNativeDepositAddress(_hostAddress)
const { nonce, enclavePublicKey, nativeDepositAddress } = await this.node.getNativeDepositAddress(_hostAddress)

this.nonce = nonce
this.enclavePublicKey = enclavePublicKey
this.value = nativeDepositAddress
this.hostAddress = _hostAddress
return this.value
} catch (err) {
} catch (_err) {
throw new Error('Error during deposit address generation')
}
}
Expand All @@ -102,59 +117,25 @@ export class DepositAddress {
}

verify() {
const { constants } = utils

let network
if (
this.nativeNetwork === constants.networks.BitcoinMainnet &&
this.nativeBlockchain === constants.blockchains.Bitcoin
) {
network = bitcoin.networks.bitcoin
} else if (
this.nativeNetwork === constants.networks.BitcoinTestnet &&
this.nativeBlockchain === constants.blockchains.Bitcoin
) {
network = bitcoin.networks.testnet
} else if (
this.nativeNetwork === constants.networks.LitecoinMainnet &&
this.nativeBlockchain === constants.blockchains.Litecoin
) {
network = bitcoin.networks.litecoin
} else if (
this.nativeNetwork === constants.networks.LitecoinTestnet &&
this.nativeBlockchain === constants.blockchains.Litecoin
) {
network = bitcoin.networks.litecoinTestnet
} else {
throw new Error(
'Please use a valid combination of nativeNetwork and nativeBlockchain'
)
}
const {
constants: {
blockchains: { Eosio, Telos }
}
} = utils
const network = NETWORKS[this.nativeBlockchain][this.nativeNetwork]
if (!network) throw new Error('Please use a valid combination of nativeNetwork and nativeBlockchain')

// NOTE: eos account name are utf-8 encoded
const hostAddressBuf =
this.hostBlockchain === constants.blockchains.Eosio ||
this.hostBlockchain === constants.blockchains.Telos
this.hostBlockchain === Eosio || this.hostBlockchain === Telos
? Buffer.from(this.hostAddress, 'utf-8')
: Buffer.from(utils.eth.removeHexPrefix(this.hostAddress), 'hex')

const nonceBuf = utils.converters.encodeUint64le(this.nonce)
const enclavePublicKeyBuf = Buffer.from(
utils.eth.removeHexPrefix(this.enclavePublicKey),
'hex'
)

const hostAddressAndNonceHashBuf = bitcoin.crypto.hash256(
Buffer.concat([hostAddressBuf, nonceBuf])
)

const enclavePublicKeyBuf = Buffer.from(utils.eth.removeHexPrefix(this.enclavePublicKey), 'hex')
const hostAddressAndNonceHashBuf = bitcoin.crypto.hash256(Buffer.concat([hostAddressBuf, nonceBuf]))
const output = bitcoin.script.compile(
[].concat(
hostAddressAndNonceHashBuf,
bitcoin.opcodes.OP_DROP,
enclavePublicKeyBuf,
bitcoin.opcodes.OP_CHECKSIG
)
[].concat(hostAddressAndNonceHashBuf, bitcoin.opcodes.OP_DROP, enclavePublicKeyBuf, bitcoin.opcodes.OP_CHECKSIG)
)

const p2sh = bitcoin.payments.p2sh({
Expand All @@ -179,54 +160,34 @@ export class DepositAddress {
const start = async () => {
if (!this.value) promiEvent.reject('Please generate a deposit address')

const shortNativeBlockchain = utils.helpers.getBlockchainShortType(
this.nativeBlockchain
)
const shortHostBlockchain = utils.helpers.getBlockchainShortType(
this.hostBlockchain
const shortNativeBlockchain = utils.helpers.getBlockchainShortType(this.nativeBlockchain)
const shortHostBlockchain = utils.helpers.getBlockchainShortType(this.hostBlockchain)

const nativeTxId = await utils[shortNativeBlockchain].monitorUtxoByAddress(
this.nativeNetwork,
this.value,
promiEvent.eventEmitter,
POLLING_TIME,
'nativeTxBroadcasted',
'nativeTxConfirmed',
confirmations[shortNativeBlockchain]
)

// prettier-ignore
const nativeTxId = await utils[shortNativeBlockchain]
.monitorUtxoByAddress(
this.nativeNetwork,
this.value,
promiEvent.eventEmitter,
POLLING_TIME,
'nativeTxBroadcasted',
'nativeTxConfirmed',
confirmations[shortNativeBlockchain]
)

const broadcastedHostTxReport = await this.node.monitorIncomingTransaction(
nativeTxId,
promiEvent.eventEmitter
const broadcastedHostTxReport = await this.node.monitorIncomingTransaction(nativeTxId, promiEvent.eventEmitter)
const hostTxReceipt = await utils[shortHostBlockchain].waitForTransactionConfirmation(
this.hostApi,
broadcastedHostTxReport.broadcast_tx_hash,
HOST_NODE_POLLING_TIME_INTERVAL
)

// prettier-ignore
const hostTxReceipt = await utils[shortHostBlockchain]
.waitForTransactionConfirmation(
this.hostApi,
broadcastedHostTxReport.broadcast_tx_hash,
HOST_NODE_POLLING_TIME_INTERVAL
)

if (this.hostBlockchain !== utils.constants.blockchains.Telos) {
// NOTE: 'onEosTxConfirmed & onEthTxConfirmed' will be removed in version >= 1.0.0
promiEvent.eventEmitter.emit(
hostBlockchainEvents[this.hostBlockchain],
hostTxReceipt
)
promiEvent.eventEmitter.emit(hostBlockchainEvents[this.hostBlockchain], hostTxReceipt)
}
promiEvent.eventEmitter.emit('hostTxConfirmed', hostTxReceipt)

promiEvent.resolve({
amount: utils.eth
.offChainFormat(
new BigNumber(broadcastedHostTxReport.host_tx_amount),
8
)
.toFixed(),
amount: utils.eth.offChainFormat(new BigNumber(broadcastedHostTxReport.host_tx_amount), 8).toFixed(),
nativeTx: nativeTxId,
hostTx: broadcastedHostTxReport.broadcast_tx_hash,
to: this.hostAddress
Expand Down
Loading

0 comments on commit 34bb47b

Please sign in to comment.