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

feat: detect multiple UTXOs per output #41

Merged
merged 1 commit into from
Jan 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
26 changes: 20 additions & 6 deletions lib/consts/Database.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Sequelize, { DataTypeAbstract, DefineAttributeColumnOptions } from 'sequelize';
import { WalletInfo } from './Types';

// TODO: don't have currency in OutputFactory and UtxoFactory
export type SequelizeAttributes<T extends { [key: string]: any }> = {
[P in keyof T]: string | DataTypeAbstract | DefineAttributeColumnOptions
};
Expand All @@ -23,18 +24,31 @@ export type WalletAttributes = WalletFactory;

export type WalletInstance = WalletAttributes & Sequelize.Instance<WalletAttributes>;

export type UtxoFactory = {
txHash: string;
export type OutputFactory = {
script: string;
redeemScript?: string;
currency: string;
keyIndex: number;
type: number;
};

export type OutputAttributes = OutputFactory & {
id: number;
};

export type OutputInstance = OutputAttributes & Sequelize.Instance<OutputAttributes>;

export type UtxoFactory = {
txHash: string;
vout: number;
script: string;
redeemScript?: string;
currency: string,
value: number;
type: number;
confirmed: boolean;
outputId: number;
};

export type UtxoAttributes = UtxoFactory;
export type UtxoAttributes = UtxoFactory & {
id: number;
};

export type UtxoInstance = UtxoAttributes & Sequelize.Instance<UtxoAttributes>;
2 changes: 2 additions & 0 deletions lib/db/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Logger from '../Logger';

type Models = {
Wallet: Sequelize.Model<db.WalletInstance, db.WalletAttributes>;
Output: Sequelize.Model<db.OutputInstance, db.OutputAttributes>;
Utxo: Sequelize.Model<db.UtxoInstance, db.UtxoAttributes>;
};

Expand Down Expand Up @@ -38,6 +39,7 @@ class Db {

await Promise.all([
this.models.Wallet.sync(),
this.models.Output.sync(),
this.models.Utxo.sync(),
]);
}
Expand Down
28 changes: 28 additions & 0 deletions lib/db/models/Ouput.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import Sequelize from 'sequelize';
import * as db from '../../consts/Database';

export default (sequelize: Sequelize.Sequelize, dataTypes: Sequelize.DataTypes) => {
const attributes: db.SequelizeAttributes<db.OutputAttributes> = {
id: { type: dataTypes.INTEGER, primaryKey: true, autoIncrement: true },
script: { type: dataTypes.STRING, allowNull: false },
redeemScript: { type: dataTypes.STRING, allowNull: true },
currency: { type: dataTypes.STRING, allowNull: false },
keyIndex: { type: dataTypes.INTEGER, allowNull: false },
type: { type: dataTypes.INTEGER, allowNull: false },
};

const options: Sequelize.DefineOptions<db.OutputInstance> = {
tableName: 'outputs',
timestamps: false,
};

const Output = sequelize.define<db.OutputInstance, db.OutputAttributes>('Output', attributes, options);

Output.associate = (models: Sequelize.Models) => {
models.Utxo.belongsTo(models.Wallet, {
foreignKey: 'currency',
});
};

return Output;
};
14 changes: 6 additions & 8 deletions lib/db/models/Utxo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ import * as db from '../../consts/Database';

export default (sequelize: Sequelize.Sequelize, dataTypes: Sequelize.DataTypes) => {
const attributes: db.SequelizeAttributes<db.UtxoAttributes> = {
txHash: { type: dataTypes.STRING, primaryKey: true, allowNull: false },
currency: { type: dataTypes.STRING, allowNull: false },
keyIndex: { type: dataTypes.INTEGER, allowNull: false },
id: { type: dataTypes.INTEGER, primaryKey: true, autoIncrement: true },
txHash: { type: dataTypes.STRING, allowNull: false },
vout: { type: dataTypes.INTEGER, allowNull: false },
script: { type: dataTypes.STRING, allowNull: false },
redeemScript: { type: dataTypes.STRING, allowNull: true },
currency: { type: dataTypes.STRING, allowNull: false },
value: { type: dataTypes.INTEGER, allowNull: false },
type: { type: dataTypes.INTEGER, allowNull: false },
confirmed: { type: dataTypes.BOOLEAN, allowNull: true },
outputId: { type: dataTypes.INTEGER, allowNull: false },
};

const options: Sequelize.DefineOptions<db.UtxoInstance> = {
Expand All @@ -22,8 +20,8 @@ export default (sequelize: Sequelize.Sequelize, dataTypes: Sequelize.DataTypes)
const Utxo = sequelize.define<db.UtxoInstance, db.UtxoAttributes>('Utxo', attributes, options);

Utxo.associate = (models: Sequelize.Models) => {
models.Utxo.belongsTo(models.Wallet, {
foreignKey: 'currency',
models.Utxo.belongsTo(models.Output, {
foreignKey: 'outputId',
});
};

Expand Down
9 changes: 3 additions & 6 deletions lib/swap/SwapManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BIP32 } from 'bip32';
import { Transaction, crypto, address } from 'bitcoinjs-lib';
import { Transaction, address } from 'bitcoinjs-lib';
import { OutputType, TransactionOutput, Scripts, pkRefundSwap, constructClaimTransaction } from 'boltz-core';
import Logger from '../Logger';
import { getHexBuffer, getHexString, getScriptHashEncodeFunction, reverseString } from '../Utils';
Expand All @@ -8,7 +8,7 @@ import WalletManager, { Currency } from '../wallet/WalletManager';
import { OrderSide } from '../proto/boltzrpc_pb';
import LndClient from '../lightning/LndClient';

const { p2wpkhOutput, p2shP2wshOutput } = Scripts;
const { p2shP2wshOutput } = Scripts;

type BaseSwapDetails = {
redeemScript: Buffer;
Expand Down Expand Up @@ -87,10 +87,7 @@ class SwapManager {

this.logger.verbose(`Creating new Swap from ${receivingCurrency.symbol} to ${sendingCurrency.symbol} with preimage hash: ${paymentHash}`);

const { keys, index } = receivingCurrency.wallet.getNewKeys();

// Listen to the address to which the swap output will be claimed
await receivingCurrency.wallet.listenToOutput(p2wpkhOutput(crypto.hash160(keys.publicKey)), index, OutputType.Bech32);
const { keys } = receivingCurrency.wallet.getNewKeys();

const timeoutBlockHeight = bestBlock.height + 10;
const redeemScript = pkRefundSwap(
Expand Down
20 changes: 20 additions & 0 deletions lib/wallet/OutputRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Models } from '../db/Database';
import * as db from '../consts/Database';

class OutputRepository {
constructor(private models: Models) {}

public getOutputs = async (currency: string) => {
return this.models.Output.findAll({
where: {
currency,
},
});
}

public addOutput = async (output: db.OutputFactory) => {
return this.models.Output.create(<db.OutputAttributes>output);
}
}

export default OutputRepository;
2 changes: 1 addition & 1 deletion lib/wallet/UtxoRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class UtxoRepository {
}

public addUtxo = async (utxo: db.UtxoFactory) => {
return this.models.Utxo.create(utxo);
return this.models.Utxo.create(<db.UtxoAttributes>utxo);
}

public removeUtxo = async (txHash: string) => {
Expand Down
Loading