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

chore: improve websocket logs #412

Closed
wants to merge 30 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
fae1ef2
feat: addSend with optional utxos argument
r4mmer Aug 23, 2022
e1949a1
feat: calculate balance method
r4mmer Aug 23, 2022
547f28b
feat: calculate authority balance
r4mmer Aug 24, 2022
b41b114
fix: utxo selection
r4mmer Aug 24, 2022
a0d6423
fix: use base58 address
r4mmer Aug 24, 2022
1c180f0
tests: PartialTxProposal tests
r4mmer Aug 26, 2022
4be9aeb
tests: more partialTx proposal tests
r4mmer Aug 27, 2022
63d1b15
chore: increase code coverage
r4mmer Aug 29, 2022
34f4686
chore: change coverage
r4mmer Aug 29, 2022
a3d831b
feat: filter utxos
r4mmer Aug 30, 2022
8693816
feat: addressPath helper
r4mmer Aug 31, 2022
8c0caae
chore: use addressIndex to get addressPath
r4mmer Aug 31, 2022
ce01429
feat: expose bufferUtils
r4mmer Sep 2, 2022
88bb352
feat: track token authority with authorities property
r4mmer Sep 2, 2022
dc9530a
chore: add authorities to constructor
r4mmer Sep 5, 2022
92e2a33
tests: remove tokenData from partial tx constructor
r4mmer Sep 5, 2022
d2aa086
tests: remove tokenData from expected tests
r4mmer Sep 5, 2022
94cff9e
tests: change mock variables
r4mmer Sep 5, 2022
0c5eff3
tests: fix mock methods
r4mmer Sep 5, 2022
f821ef7
tests: integration tests
r4mmer Sep 12, 2022
0496a1c
tests: expect serialized string
r4mmer Sep 12, 2022
a85d068
tests: missing declaration
r4mmer Sep 12, 2022
99947e1
tests: remove expect
r4mmer Sep 12, 2022
b504c85
tests: setStore before signing
r4mmer Sep 12, 2022
0e2f16d
chore: use constants
r4mmer Sep 14, 2022
d3f1133
tests(integration): add balance check for atomic-swap
r4mmer Sep 14, 2022
701a06c
Merge pull request #408 from HathorNetwork/feat/partial-tx-facade
r4mmer Sep 14, 2022
b0dee84
chore: improve websocket logs
luislhl Sep 14, 2022
4dbbf57
chore: bump version v0.40.4 (#413)
r4mmer Sep 15, 2022
fd07dcd
Merge branch 'dev' into chore/improve-websocket-logs
luislhl Sep 15, 2022
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
Next Next commit
feat: addSend with optional utxos argument
  • Loading branch information
r4mmer committed Sep 10, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit fae1ef2b0e433cad9956c212e30eccad815493c5
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -37,6 +37,7 @@ const tokensUtils = require('./lib/utils/tokens');
const walletUtils = require('./lib/utils/wallet');
const helpersUtils = require('./lib/utils/helpers');
const scriptsUtils = require('./lib/utils/scripts');
const transactionUtils = require('./lib/utils/transaction');
const HathorWalletServiceWallet = require('./lib/wallet/wallet');
const SendTransactionWalletService = require('./lib/wallet/sendTransactionWalletService');
const config = require('./lib/config');
@@ -87,6 +88,7 @@ module.exports = {
walletUtils: walletUtils.default,
helpersUtils: helpersUtils.default,
scriptsUtils: scriptsUtils,
transactionUtils: transactionUtils.default,
HathorWalletServiceWallet: HathorWalletServiceWallet.default,
SendTransactionWalletService: SendTransactionWalletService.default,
config: config.default,
18 changes: 2 additions & 16 deletions src/new/wallet.js
Original file line number Diff line number Diff line change
@@ -921,22 +921,8 @@ class HathorWallet extends EventEmitter {

if (txout.spent_by === null) {
if (wallet.canUseUnspentTx(txout, tx.height)) {
const isAuthority = wallet.isAuthorityOutput(txout);
const addressIndex = this.getAddressIndex(txout.decoded.address);

const utxo = {
txId: tx_id,
index,
tokenId: txout.token,
address: txout.decoded.address,
value: txout.value,
authorities: isAuthority ? txout.value : 0,
timelock: txout.decoded.timelock,
heightlock: null, // not enough info to determine this.
locked: false,
addressPath: `m/44'/280'/0'/0/${addressIndex}`,
};
yield utxo;
const addressPath = `m/44'/280'/0'/0/${this.getAddressIndex(txout.decoded.address)}`;
yield transactionUtils.utxoFromHistoryOutput(tx_id, index, txout, { addressPath });
}
}
}
42 changes: 41 additions & 1 deletion src/utils/transaction.ts
Original file line number Diff line number Diff line change
@@ -8,6 +8,9 @@

import { Utxo } from '../wallet/types';
import { UtxoError } from '../errors';
import { HistoryTransactionOutput } from '../models/types';
import HathorWallet from '../new/wallet';
import wallet from '../wallet';

const transaction = {
/**
@@ -35,13 +38,14 @@ const transaction = {
if (utxo.value >= totalAmount) {
utxosToUse = [utxo];
filledAmount = utxo.value;
break
} else {
if (filledAmount >= totalAmount) {
break
}
filledAmount += utxo.value;
utxosToUse.push(utxo);
}
}
}
if (filledAmount < totalAmount) {
throw new UtxoError('Don\'t have enough utxos to fill total amount.');
@@ -52,6 +56,42 @@ const transaction = {
changeAmount: filledAmount - totalAmount,
}
},

/**
* Convert an output from the history of transactions to an Utxo.
*
* @param {string} txId The transaction this output belongs to.
* @param {number} index The output index on the original transaction.
* @param {HistoryTransactionOutput} txout output from the transaction history.
* @param {Object} [options]
* @param {string} [options.addressPath=''] utxo address bip32 path
*
* @returns {Utxo}
*
* @memberof transaction
* @inner
*/
utxoFromHistoryOutput(
txId: string,
index: number,
txout: HistoryTransactionOutput,
{ addressPath = '' }: { addressPath?: string },
): Utxo {
const isAuthority = wallet.isAuthorityOutput(txout);

return {
txId,
index,
addressPath,
address: txout.decoded && txout.decoded.address || '',
timelock: txout.decoded && txout.decoded.timelock || null,
tokenId: txout.token,
value: txout.value,
authorities: isAuthority ? txout.value : 0,
heightlock: null, // not enough info to determine this.
locked: false,
};
},
}

export default transaction;
55 changes: 45 additions & 10 deletions src/wallet/partialTxProposal.ts
Original file line number Diff line number Diff line change
@@ -17,8 +17,13 @@ import { HATHOR_TOKEN_CONFIG } from '../constants';

import transaction from '../transaction';
import helpers from '../utils/helpers';
import transactionUtils from '../utils/transaction';

import { OutputType } from './types';
import { OutputType, Utxo } from './types';

interface UtxoExtended extends Utxo {
tokenData?: number
}

class PartialTxProposal {

@@ -48,44 +53,74 @@ class PartialTxProposal {
*
* @returns {PartialTxProposal}
*/
static fromPartialTx(serialized: string, network: Network) {
static fromPartialTx(serialized: string, network: Network): PartialTxProposal {
const partialTx = PartialTx.deserialize(serialized, network);
const proposal = new PartialTxProposal(network);
proposal.partialTx = partialTx;
return proposal;
}

/**
* Get all available utxos on the wallet history for a token.
*
* @param {HathorWallet} wallet Wallet which will provide the tokens.
* @param {string?} [token='00'] UID of token that is being sent
*
* @returns {UtxoExtended[]}
*/
getWalletUtxos(wallet: HathorWallet, token: string = HATHOR_TOKEN_CONFIG.uid): UtxoExtended[] {
const historyTransactions = wallet.getFullHistory();
const allUtxos = [...wallet.getAllUtxos({ token })].filter(utxo => utxo.authorities === 0);
const allExtendedUtxos: UtxoExtended[] = [];
for (const utxo of allUtxos) {
// Since we chose the utxos from the historyTransactions, we can be sure this exists.
const txout = historyTransactions[utxo.txId].outputs[utxo.index];
allExtendedUtxos.push({
...utxo,
tokenData: txout.token_data,
});
}

return allExtendedUtxos;
}

/**
* Add inputs sending the amount of tokens specified, may add a change output.
*
* @param {HathorWallet} wallet Wallet which will provide the tokens.
* @param {string} token UID of token that is being sent
* @param {number} value Quantity of tokens being sent
* @param {Object} [options]
* @param {UtxoExtended[]|null} [options.changeAddress=null] If we add change, use this address instead of getting a new one from the wallet.
* @param {string|null} [options.changeAddress=null] If we add change, use this address instead of getting a new one from the wallet.
* @param {boolean} [options.markAsSelected=true] Mark the utxo with `selected_as_input`.
*/
addSend(
wallet: HathorWallet,
token: string,
value: number,
{ changeAddress = null, markAsSelected = true }: { changeAddress?: string|null, markAsSelected?: boolean } = {},
{ utxos = null, changeAddress = null, markAsSelected = true }: { utxos?: UtxoExtended[]|null, changeAddress?: string|null, markAsSelected?: boolean } = {},
) {
this.resetSignatures();

const historyTransactions = wallet.getFullHistory();
// Since the selectUtxos returns a list of Utxo, we need a way to access the original utxo for the tokenData.
const utxosDict: Record<string, UtxoExtended> = {};
// Use the pool of utxos or all wallet utxos.
const allUtxos: UtxoExtended[] = utxos || this.getWalletUtxos(wallet, token);
for (const utxo of allUtxos) {
utxosDict[utxo.txId] = utxo;
}
const utxosDetails = transactionUtils.selectUtxos(allUtxos, value);

const utxosDetails = wallet.getUtxosForAmount(value, { token });
for (const utxo of utxosDetails.utxos) {
// Since we chose the utxos from the historyTransactions, we can be sure this exists.
const txout = historyTransactions[utxo.txId].outputs[utxo.index];
const tokenData = utxosDict[utxo.txId].tokenData;
this.addInput(
wallet,
utxo.txId,
utxo.index,
utxo.value,
utxo.address,
{ token: utxo.tokenId, tokenData: txout.token_data, markAsSelected },
{ token: utxo.tokenId, tokenData, markAsSelected },
);
}

@@ -226,7 +261,7 @@ class PartialTxProposal {
*
* @returns {boolean}
*/
isComplete() {
isComplete(): boolean {
return (!!this.signatures) && this.partialTx.isComplete() && this.signatures.isComplete();
}

@@ -279,7 +314,7 @@ class PartialTxProposal {
*
* @returns {Transaction}
*/
prepareTx() {
prepareTx(): Transaction {
if (!this.partialTx.isComplete()) {
throw new InvalidPartialTxError('Incomplete data');
}