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

Asset plutus type #33

Merged
merged 34 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
b2e83e2
AssetPlutusType
Salvionied Aug 20, 2024
331b07f
Add Custom PlutusType with interface PlutusMarshaler
Salvionied Aug 20, 2024
2b7cc05
fix sub map elements
Salvionied Aug 20, 2024
9d785ab
add plutusKey for maps in Marshal
Salvionied Aug 20, 2024
958349d
Add PlutusEncoder support for big.Int
Salvionied Aug 21, 2024
7ab227e
add big.Ints support
Salvionied Aug 23, 2024
ff54a3c
fix address
Salvionied Aug 23, 2024
20c9e7f
change script_data_hash calculation
Salvionied Aug 24, 2024
906cba3
Add Chang fork and fix ogmios chain context
Salvionied Aug 24, 2024
37ca525
remove prints
Salvionied Sep 1, 2024
b67a5b7
Merge branch 'master' into assetPlutusType
Salvionied Sep 1, 2024
7cb6557
fix-tws
Salvionied Sep 2, 2024
ea018e9
update costmodels
Salvionied Sep 2, 2024
657cc32
fix dep
Salvionied Sep 2, 2024
883f2a7
fix script refs
Salvionied Sep 2, 2024
4381b58
remove print
Salvionied Sep 2, 2024
ea622eb
fix ScriptDataHashCalculator
Salvionied Sep 3, 2024
f2c6691
fix fee calculation for included scripts
Salvionied Sep 3, 2024
403bc11
fix fees for refscripts
Salvionied Sep 3, 2024
b79efb6
add signature changes
Salvionied Sep 6, 2024
b8592f2
add utility function
Salvionied Sep 7, 2024
09a909b
fix usedUtxos to consider manually added inputs
Salvionied Sep 8, 2024
51cb5b9
fix
Salvionied Sep 8, 2024
e50b26f
erge
Salvionied Sep 8, 2024
a410a61
remove blockfrost submission fatal
Salvionied Sep 27, 2024
fdbcc7d
adjust fee calculation
Salvionied Oct 17, 2024
be57749
fix fee x byte
Salvionied Oct 17, 2024
f05243f
fix tx fee
Salvionied Oct 17, 2024
326c701
fix fee calculation
Salvionied Oct 17, 2024
f2da1ed
adjust tests for conway fees
Salvionied Oct 17, 2024
99d52bb
remove all fatals and panics
Salvionied Dec 5, 2024
4cc73b0
chore: merge branch 'origin/master'
wolf31o2 Jan 13, 2025
4e7756b
fix: restore kupmios
wolf31o2 Jan 13, 2025
4b838c8
chore: go mod tidy
wolf31o2 Jan 13, 2025
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
301 changes: 217 additions & 84 deletions ApolloBuilder.go

Large diffs are not rendered by default.

159 changes: 129 additions & 30 deletions ApolloBuilder_test.go

Large diffs are not rendered by default.

46 changes: 33 additions & 13 deletions Models.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ type Unit struct {
Quantity int
}

/**
/*
*

ToValue converts a Unit to a Value object.

Returns:
Expand All @@ -35,7 +37,9 @@ func (u *Unit) ToValue() Value.Value {
return val
}

/**
/*
*

NewUnit creates a new Unit with the provided information.

Params:
Expand Down Expand Up @@ -69,7 +73,9 @@ type Payment struct {
IsInline bool
}

/**
/*
*

PaymentFromTxOut creates a Payment object from a TransactionOutput.

Params:
Expand Down Expand Up @@ -110,10 +116,12 @@ func PaymentFromTxOut(txOut *TransactionOutput.TransactionOutput) *Payment {
return payment
}

/**
/*
*

NewPayment creates a new Payment object.

Params:
Params:
receiver (string): The receiver's address.
lovelace (int): The amount in Lovelace.
units ([]Unit): The assets units to be included.
Expand All @@ -130,7 +138,9 @@ func NewPayment(receiver string, lovelace int, units []Unit) *Payment {
}
}

/**
/*
*

NewPaymentFromValue creates a new Payment object from an Address
and Value object.

Expand Down Expand Up @@ -158,7 +168,9 @@ func NewPaymentFromValue(receiver Address.Address, value Value.Value) *Payment {
return payment
}

/**
/*
*

ToValue converts a Payment to a Value object.

Returns:
Expand All @@ -173,7 +185,9 @@ func (p *Payment) ToValue() Value.Value {
return v
}

/**
/*
*

EnsureMinUTXO ensures that the payment amount meets the minimun UTXO requirement.

Params:
Expand All @@ -184,17 +198,23 @@ func (p *Payment) EnsureMinUTXO(cc Base.ChainContext) {
return
}
txOut := p.ToTxOut()
coins := Utils.MinLovelacePostAlonzo(*txOut, cc)
coins, err := Utils.MinLovelacePostAlonzo(*txOut, cc)
if err != nil {
return
}

if int64(p.Lovelace) < coins {
p.Lovelace = int(coins)
}
}

/**
ToTxOut converts a Payment object to a TransactionOutput object.
/*
*

Returns:
*TransactionOutput.TransactionOutput: The created TransactionOutput object.
ToTxOut converts a Payment object to a TransactionOutput object.

Returns:
*TransactionOutput.TransactionOutput: The created TransactionOutput object.
*/
func (p *Payment) ToTxOut() *TransactionOutput.TransactionOutput {
txOut := TransactionOutput.SimpleTransactionOutput(p.Receiver, p.ToValue())
Expand Down
4 changes: 3 additions & 1 deletion apollotypes/plutus.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ type AikenPlutusJSON struct {
} `json:"definitions"`
}

/**
/*
*

GetScript retrives a Plutus V2 script by its name from an AikenPlutusJSON object.
It searches through the Validators and returns the script if found.

Expand Down
153 changes: 126 additions & 27 deletions apollotypes/types.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
package apollotypes

import (
"bytes"

"github.com/Salvionied/apollo/serialization"
serAddress "github.com/Salvionied/apollo/serialization/Address"
"github.com/Salvionied/apollo/serialization/Key"
"github.com/Salvionied/apollo/serialization/Transaction"
"github.com/Salvionied/apollo/serialization/TransactionWitnessSet"
"github.com/Salvionied/apollo/serialization/UTxO"
"github.com/Salvionied/apollo/serialization/VerificationKeyWitness"
"github.com/Salvionied/apollo/txBuilding/Backend/Base"
)

type Wallet interface {
GetAddress() *serAddress.Address
SignTx(tx Transaction.Transaction) TransactionWitnessSet.TransactionWitnessSet
SignTx(tx Transaction.Transaction, usedUtxos []UTxO.UTxO) TransactionWitnessSet.TransactionWitnessSet
PkeyHash() serialization.PubKeyHash
SkeyHash() serialization.PubKeyHash
//SignMessage(address serAddress.Address, message []uint8) []uint8
}

type ExternalWallet struct {
Address serAddress.Address
}

/**
/*
*

GetAddress returns the address associated with an external wallet.

Returns:
Expand All @@ -31,7 +37,9 @@ func (ew *ExternalWallet) GetAddress() *serAddress.Address {
return &ew.Address
}

/**
/*
*

SignTx signs a transaction using an external wallet.

Params:
Expand All @@ -40,13 +48,15 @@ func (ew *ExternalWallet) GetAddress() *serAddress.Address {
Returns:
TransactionWitnessSet.TransactionWitnessSet: The withness set associated with the signed transaction.
*/
func (ew *ExternalWallet) SignTx(tx Transaction.Transaction) TransactionWitnessSet.TransactionWitnessSet {
func (ew *ExternalWallet) SignTx(tx Transaction.Transaction, usedUtxos []UTxO.UTxO) TransactionWitnessSet.TransactionWitnessSet {
return tx.TransactionWitnessSet
}

/**
/*
*

PkeyHash returns the public key hash assoicated with an external wallet.
It computes and returns the public key hash based on the PaymentPart
It computes and returns the public key hash based on the PaymentPart
of the wallet's address.

Returns:
Expand All @@ -57,6 +67,10 @@ func (ew *ExternalWallet) PkeyHash() serialization.PubKeyHash {
return res
}

func (ew *ExternalWallet) SkeyHash() serialization.PubKeyHash {
return serialization.PubKeyHash{}
}

type GenericWallet struct {
SigningKey Key.SigningKey
VerificationKey Key.VerificationKey
Expand All @@ -65,20 +79,29 @@ type GenericWallet struct {
StakeVerificationKey Key.StakeVerificationKey
}

/**
PkeyHash calculates and returns the public key hash associated with a generic wallet.
It computes the public key hash by calling the Hash() method on the wallet's VerificationKey.
Then it returns as a serialization.PubKeyHas type.

Returns:
serialization.PubKeyHash: The public key hash of the generic wallet.
/*
*

PkeyHash calculates and returns the public key hash associated with a generic wallet.
It computes the public key hash by calling the Hash() method on the wallet's VerificationKey.
Then it returns as a serialization.PubKeyHas type.

Returns:
serialization.PubKeyHash: The public key hash of the generic wallet.
*/
func (gw *GenericWallet) PkeyHash() serialization.PubKeyHash {
res, _ := gw.VerificationKey.Hash()
return res
}

/**
func (gw *GenericWallet) SkeyHash() serialization.PubKeyHash {
res, _ := gw.StakeVerificationKey.Hash()
return res
}

/*
*

GetAddress returns the address associated with a generic wallet.

Returns:
Expand All @@ -88,27 +111,103 @@ func (gw *GenericWallet) GetAddress() *serAddress.Address {
return &gw.Address
}

/**
SignTx signs a transaction using a generic wallet and returns the updated TransactionWitnessSet.
It takes a transaction of type Transaction.Transaction and signs it using the wallet's SigningKey.
Then it appends the corresponding VerificationKeyWitness to the TransactionWitnessSet and returns
the updated witness set.
/*
*

Parameters:
wallet (*GenericWallet): A pointer to a generic wallet.
tx (Transaction.Transaction): The transaction to be signed.
SignTx signs a transaction using a generic wallet and returns the updated TransactionWitnessSet.
It takes a transaction of type Transaction.Transaction and signs it using the wallet's SigningKey.
Then it appends the corresponding VerificationKeyWitness to the TransactionWitnessSet and returns
the updated witness set.

Returns:
TransactionWitnessSet.TransactionWitnessSet: The updated TransactionWitnessSet after signing the transaction.
Parameters:
wallet (*GenericWallet): A pointer to a generic wallet.
tx (Transaction.Transaction): The transaction to be signed.

Returns:
TransactionWitnessSet.TransactionWitnessSet: The updated TransactionWitnessSet after signing the transaction.
*/
func (wallet *GenericWallet) SignTx(tx Transaction.Transaction) TransactionWitnessSet.TransactionWitnessSet {
func (wallet *GenericWallet) SignTx(
tx Transaction.Transaction,
usedUtxos []UTxO.UTxO,
) TransactionWitnessSet.TransactionWitnessSet {
witness_set := tx.TransactionWitnessSet
txHash, _ := tx.TransactionBody.Hash()
signature, _ := wallet.SigningKey.Sign(txHash)
witness_set.VkeyWitnesses = append(witness_set.VkeyWitnesses, VerificationKeyWitness.VerificationKeyWitness{Vkey: wallet.VerificationKey, Signature: signature})
if isKeyHashUsedFromUtxos(usedUtxos, wallet.PkeyHash()) ||
isKeyHashUsedFromTx(tx, wallet.PkeyHash()) {
signature, _ := wallet.SigningKey.Sign(txHash)

witness_set.VkeyWitnesses = append(
witness_set.VkeyWitnesses,
VerificationKeyWitness.VerificationKeyWitness{
Vkey: wallet.VerificationKey,
Signature: signature,
},
)
}

if isKeyHashUsedFromUtxos(usedUtxos, wallet.SkeyHash()) ||
isKeyHashUsedFromTx(tx, wallet.SkeyHash()) {
signature, _ := wallet.StakeSigningKey.Sign(txHash)

witness_set.VkeyWitnesses = append(
witness_set.VkeyWitnesses,
VerificationKeyWitness.VerificationKeyWitness{
Vkey: Key.VerificationKey(wallet.StakeVerificationKey),
Signature: signature,
},
)
}

return witness_set
}

func isKeyHashUsedFromUtxos(
usedUtxos []UTxO.UTxO,
keyHash serialization.PubKeyHash,
) bool {
for _, utxo := range usedUtxos {
utxoKeyHash := serialization.PubKeyHash(utxo.Output.GetAddress().PaymentPart)
if utxoKeyHash == keyHash {
return true
}
}
return false
}

func isKeyHashUsedFromTx(
tx Transaction.Transaction,
keyHash serialization.PubKeyHash,
) bool {
keyHashBytes := keyHash[:]
if tx.TransactionBody.Certificates != nil {
for _, certificate := range *tx.TransactionBody.Certificates {
if certificate.StakeCredential.KeyHash() == keyHash {
return true
}
}
}
if tx.TransactionBody.Withdrawals != nil {
for withdrawal := range *tx.TransactionBody.Withdrawals {
withdrawalBytes := withdrawal[1:]
if bytes.Equal(withdrawalBytes, keyHashBytes) {
return true
}
}
}
for _, requiredSigner := range tx.TransactionBody.RequiredSigners {
if requiredSigner == keyHash {
return true
}
}
for _, nativeScript := range tx.TransactionWitnessSet.NativeScripts {
if bytes.Equal(nativeScript.KeyHash, keyHashBytes) {
return true
}
}

return false
}

type Backend Base.ChainContext

type Address serAddress.Address
8 changes: 4 additions & 4 deletions backends.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,28 @@ func NewBlockfrostBackend(
constants.BLOCKFROST_BASE_URL_MAINNET,
int(constants.MAINNET),
projectId,
), nil
)
case constants.TESTNET:

return BlockFrostChainContext.NewBlockfrostChainContext(
constants.BLOCKFROST_BASE_URL_TESTNET,
int(constants.TESTNET),
projectId,
), nil
)
case constants.PREVIEW:

return BlockFrostChainContext.NewBlockfrostChainContext(
constants.BLOCKFROST_BASE_URL_PREVIEW,
int(constants.TESTNET),
projectId,
), nil
)
case constants.PREPROD:

return BlockFrostChainContext.NewBlockfrostChainContext(
constants.BLOCKFROST_BASE_URL_PREPROD,
int(constants.TESTNET),
projectId,
), nil
)
default:
return BlockFrostChainContext.BlockFrostChainContext{}, fmt.Errorf("Invalid network")
}
Expand Down
Loading
Loading