-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for freezing and confiscation (#12)
* Support for confiscation * Freezing is working * Fixes for linter * Fix go mod
- Loading branch information
Showing
10 changed files
with
224 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,60 @@ | ||
package models | ||
|
||
import "context" | ||
import ( | ||
"context" | ||
"encoding/binary" | ||
"encoding/hex" | ||
"fmt" | ||
|
||
// AlertMessageConfiscateUtxo is a confiscate utxo alert | ||
type AlertMessageConfiscateUtxo struct { | ||
"github.com/libsv/go-bn/models" | ||
) | ||
|
||
// AlertMessageConfiscateTransaction is a confiscate utxo alert | ||
type AlertMessageConfiscateTransaction struct { | ||
AlertMessage | ||
// TODO finish building out this alert type | ||
Transactions []models.ConfiscationTransactionDetails | ||
} | ||
|
||
// ConfiscateTransaction defines the parameters for the confiscation transaction | ||
type ConfiscateTransaction struct { | ||
EnforceAtHeight [8]byte | ||
ID [32]byte | ||
} | ||
|
||
// Read reads the alert | ||
func (a *AlertMessageConfiscateUtxo) Read(_ []byte) error { | ||
func (a *AlertMessageConfiscateTransaction) Read(raw []byte) error { | ||
a.Config().Services.Log.Infof("%x", raw) | ||
if len(raw) < 40 { | ||
return fmt.Errorf("confiscation alert is less than 41 bytes") | ||
} | ||
if len(raw)%40 != 0 { | ||
return fmt.Errorf("confiscation alert is not a multiple of 41 bytes") | ||
} | ||
txCount := len(raw) / 40 | ||
details := []models.ConfiscationTransactionDetails{} | ||
for i := 0; i < txCount; i++ { | ||
tx := ConfiscateTransaction{ | ||
EnforceAtHeight: [8]byte(raw[:8]), | ||
ID: [32]byte(raw[8:40]), | ||
} | ||
detail := models.ConfiscationTransactionDetails{ | ||
ConfiscationTransaction: models.ConfiscationTransaction{ | ||
EnforceAtHeight: int64(binary.LittleEndian.Uint64(tx.EnforceAtHeight[:])), | ||
Hex: hex.EncodeToString(tx.ID[:]), | ||
}, | ||
} | ||
details = append(details, detail) | ||
raw = raw[40:] | ||
} | ||
a.Transactions = details | ||
return nil | ||
} | ||
|
||
// Do executes the alert | ||
func (a *AlertMessageConfiscateUtxo) Do(_ context.Context) error { | ||
func (a *AlertMessageConfiscateTransaction) Do(ctx context.Context) error { | ||
_, err := a.Config().Services.Node.AddToConfiscationTransactionWhitelist(ctx, a.Transactions) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,91 @@ | ||
package models | ||
|
||
import "context" | ||
import ( | ||
"context" | ||
"encoding/binary" | ||
"encoding/hex" | ||
"fmt" | ||
|
||
"github.com/libsv/go-bn/models" | ||
) | ||
|
||
// AlertMessageFreezeUtxo is the message for freezing UTXOs | ||
type AlertMessageFreezeUtxo struct { | ||
AlertMessage | ||
// TODO finish building out this alert type | ||
Funds []models.Fund | ||
} | ||
|
||
// Fund is the struct defining funds to freeze | ||
type Fund struct { | ||
TransactionOutID [32]byte | ||
Vout uint64 | ||
EnforceAtHeightStart uint64 | ||
EnforceAtHeightEnd uint64 | ||
PolicyExpiresWithConsensus bool | ||
} | ||
|
||
// Serialize creates the raw hex string of the fund | ||
func (f *Fund) Serialize() []byte { | ||
raw := []byte{} | ||
raw = append(raw, f.TransactionOutID[:]...) | ||
raw = binary.LittleEndian.AppendUint64(raw, f.Vout) | ||
raw = binary.LittleEndian.AppendUint64(raw, f.EnforceAtHeightStart) | ||
raw = binary.LittleEndian.AppendUint64(raw, f.EnforceAtHeightEnd) | ||
expire := uint8(0) | ||
if f.PolicyExpiresWithConsensus { | ||
expire = uint8(1) | ||
} | ||
raw = append(raw, expire) | ||
return raw | ||
} | ||
|
||
// Read reads the message | ||
func (a *AlertMessageFreezeUtxo) Read(_ []byte) error { | ||
func (a *AlertMessageFreezeUtxo) Read(raw []byte) error { | ||
if len(raw) < 57 { | ||
return fmt.Errorf("freeze alert is less than 57 bytes, got %d bytes", len(raw)) | ||
} | ||
if len(raw)%57 != 0 { | ||
return fmt.Errorf("freeze alert is not a multiple of 57 bytes, got %d bytes", len(raw)) | ||
} | ||
fundCount := len(raw) / 57 | ||
funds := []models.Fund{} | ||
for i := 0; i < fundCount; i++ { | ||
fund := Fund{ | ||
TransactionOutID: [32]byte(raw[0:32]), | ||
Vout: binary.LittleEndian.Uint64(raw[32:40]), | ||
EnforceAtHeightStart: binary.LittleEndian.Uint64(raw[40:48]), | ||
EnforceAtHeightEnd: binary.LittleEndian.Uint64(raw[48:56]), | ||
} | ||
enforceByte := raw[56] | ||
|
||
if enforceByte != uint8(0) { | ||
fund.PolicyExpiresWithConsensus = true | ||
} | ||
funds = append(funds, models.Fund{ | ||
TxOut: models.TxOut{ | ||
TxId: hex.EncodeToString(fund.TransactionOutID[:]), | ||
Vout: int(fund.Vout), | ||
}, | ||
EnforceAtHeight: []models.Enforce{ | ||
{ | ||
Start: int(fund.EnforceAtHeightStart), | ||
Stop: int(fund.EnforceAtHeightEnd), | ||
}, | ||
}, | ||
PolicyExpiresWithConsensus: fund.PolicyExpiresWithConsensus, | ||
}) | ||
raw = raw[57:] | ||
} | ||
a.Funds = funds | ||
|
||
return nil | ||
} | ||
|
||
// Do performs the message | ||
func (a *AlertMessageFreezeUtxo) Do(_ context.Context) error { | ||
func (a *AlertMessageFreezeUtxo) Do(ctx context.Context) error { | ||
_, err := a.Config().Services.Node.AddToConsensusBlacklist(ctx, a.Funds) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters