Skip to content
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
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
version: "2"
linters:
enable:
- containedctx
- asasalint # Check for pass []any as any in variadic func(...any).
- canonicalheader # Canonicalheader checks whether net/http.Header uses canonical header. [auto-fix]
- copyloopvar # A linter detects places where loop variables are copied. [fast, auto-fix]
Expand Down
12 changes: 7 additions & 5 deletions docs/token_sdk_usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ if err != nil {
// 3. Issue Tokens
// Use the issuer wallet to issue 'quantity' of 'tokenType' to 'recipient'.
wallet := ttx.GetIssuerWallet(context, issuerWalletID)
err = tx.Issue(wallet, recipient, tokenType, quantity)
err = tx.Issue(context.Context(), wallet, recipient, tokenType, quantity)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -99,6 +99,7 @@ if err != nil {
// Sender wallet is used to select input tokens.
senderWallet := ttx.GetWallet(context, senderWalletID)
err = tx.Transfer(
context.Context(),
senderWallet,
tokenType,
[]uint64{amount},
Expand Down Expand Up @@ -147,6 +148,7 @@ if err != nil {
// If needed, also pin the issuer signing key expected by public parameters.
senderWallet := ttx.GetWallet(context, senderWalletID)
err = tx.Redeem(
context.Context(),
senderWallet,
tokenType,
amount,
Expand Down Expand Up @@ -195,7 +197,7 @@ if err != nil {

// 3. Add Alice's Transfer
senderWallet := ttx.GetWallet(context, aliceWallet)
err = tx.Transfer(senderWallet, aliceTokenType, []uint64{aliceAmount}, []view.Identity{other})
err = tx.Transfer(context.Context(), senderWallet, aliceTokenType, []uint64{aliceAmount}, []view.Identity{other})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -239,7 +241,7 @@ if err != nil {

// 3. Add Bob's Transfer
bobWallet := ttx.MyWalletFromTx(context, tx)
err = tx.Transfer(bobWallet, action.Type, []uint64{action.Amount}, []view.Identity{action.Recipient})
err = tx.Transfer(context.Context(), bobWallet, action.Type, []uint64{action.Amount}, []view.Identity{action.Recipient})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -343,8 +345,8 @@ if err != nil {
Uses an **Initiator-Responder Inversion** pattern. The user requests a withdrawal, and the Issuer (responder) becomes the initiator of the Token Transaction to issue the tokens.

### Multisig ([`multisig.go`](../integration/token/fungible/views/multisig.go))
* **Lock**: `multisig.Wrap(tx).Lock(...)`
* **Spend**: `multisig.Wrap(tx).Spend(...)`. Requires `multisig.Wallet` to list co-owned tokens.
* **Lock**: `multisig.Wrap(tx).Lock(ctx, ...)`
* **Spend**: `multisig.Wrap(tx).Spend(ctx, ...)`. Requires `multisig.Wallet` to list co-owned tokens.

---

Expand Down
1 change: 1 addition & 0 deletions docs/upgradability.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ tx, err := ttx.NewTransaction(context, nil, ttx.WithTMSID(upgradeRequest.TMSID))

// The Upgrade call consumes old tokens and issues new ones in one atomic step
err = tx.Upgrade(
context.Context(),
issuerWallet,
upgradeRequest.RecipientIdentity,
upgradeRequest.Challenge,
Expand Down
2 changes: 1 addition & 1 deletion integration/token/dvp/views/auditor.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (a *AuditView) Call(context view.Context) (any, error) {
// Validate
auditor, err := ttx.NewAuditor(context, w)
assert.NoError(err, "failed to get auditor instance")
assert.NoError(auditor.Validate(tx), "failed auditing verification")
assert.NoError(auditor.Validate(context.Context(), tx), "failed auditing verification")

return context.RunView(ttx.NewAuditApproveView(w, tx))
}
Expand Down
3 changes: 2 additions & 1 deletion integration/token/dvp/views/buyer.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (b *BuyHouseView) Call(context view.Context) (any, error) {

// check transaction, it must contain the house transfer
nfttx := nfttx.Wrap(tx)
outputs, err := nfttx.Outputs()
outputs, err := nfttx.Outputs(context.Context())
assert.NoError(err, "failed getting outputs")
assert.NoError(outputs.Validate(), "failed validating outputs")
assert.True(outputs.Count() == 1, "the transaction must contain one output")
Expand All @@ -54,6 +54,7 @@ func (b *BuyHouseView) Call(context view.Context) (any, error) {

// Append the cash transfer to the transaction
err = tx.Transfer(
context.Context(),
ttx.MyWalletFromTx(context, tx),
action.Type,
[]uint64{action.Amount},
Expand Down
2 changes: 1 addition & 1 deletion integration/token/dvp/views/cash/accept.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (a *AcceptCashView) Call(context view.Context) (any, error) {
// The recipient can perform any check on the transaction as required by the business process
// In particular, here, the recipient checks that the transaction contains at least one output, and
// that there is at least one output that names the recipient. (The recipient is receiving something.
outputs, err := tx.Outputs()
outputs, err := tx.Outputs(context.Context())
assert.NoError(err, "failed getting outputs")
assert.True(outputs.Count() > 0)
assert.True(outputs.ByRecipient(id).Count() > 0)
Expand Down
1 change: 1 addition & 0 deletions integration/token/dvp/views/cash/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func (p *IssueCashView) Call(context view.Context) (any, error) {
wallet := ttx.GetIssuerWallet(context, p.IssuerWallet)
assert.NotNil(wallet, "issuer wallet [%s] not found", p.IssuerWallet)
err = tx.Issue(
context.Context(),
wallet,
recipient,
p.TokenType,
Expand Down
2 changes: 1 addition & 1 deletion integration/token/dvp/views/house/accept.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (a *AcceptHouseView) Call(context view.Context) (any, error) {
// The recipient can perform any check on the transaction as required by the business process
// In particular, here, the recipient checks that the transaction contains one output that names the recipient.
// (The recipient is receiving something)
outputs, err := tx.Outputs()
outputs, err := tx.Outputs(context.Context())
assert.NoError(err, "failed getting outputs")
assert.NoError(outputs.Validate(), "failed validating outputs")
assert.True(outputs.Count() == 1, "the transaction must contain one output")
Expand Down
2 changes: 1 addition & 1 deletion integration/token/dvp/views/house/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (p *IssueHouseView) Call(context view.Context) (any, error) {
uniqueID, err := uniqueness.GetService(context).ComputeID(context.Context(), h.Address)
assert.NoError(err, "failed computing unique ID")

err = tx.Issue(wallet, h, recipient, nfttx.WithUniqueID(uniqueID))
err = tx.Issue(context.Context(), wallet, h, recipient, nfttx.WithUniqueID(uniqueID))
assert.NoError(err, "failed adding new issued token")

// The issuer is ready to collect all the required signatures.
Expand Down
2 changes: 1 addition & 1 deletion integration/token/dvp/views/seller.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (d *SellHouseView) prepareHouseTransfer(context view.Context, tx *ttx.Trans
assert.NotNil(wallet, "failed getting default wallet")

// Transfer ownership of the house to the buyer
assert.NoError(nfttx.Wrap(tx).Transfer(wallet, house, buyer), "failed transferring house")
assert.NoError(nfttx.Wrap(tx).Transfer(context.Context(), wallet, house, buyer), "failed transferring house")

return tx, house, nil
}
Expand Down
9 changes: 5 additions & 4 deletions integration/token/fungible/dlogstress/support/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ type Task func()
type Pool struct {
taskQueue chan Task
wg sync.WaitGroup
ctx context.Context
cancel context.CancelFunc
label string
stop atomic.Bool
//nolint:containedctx // long-running worker-pool lifecycle, not a per-request context
ctx context.Context
cancel context.CancelFunc
label string
stop atomic.Bool
}

func NewPool(label string, numWorkers int) *Pool {
Expand Down
4 changes: 2 additions & 2 deletions integration/token/fungible/views/accept.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (a *AcceptCashView) Call(context view.Context) (any, error) {
// The recipient can perform any check on the transaction as required by the business process
// In particular, here, the recipient checks that the transaction contains at least one output, and
// that there is at least one output that names the recipient.(The recipient is receiving something).
outputs, err := tx.Outputs()
outputs, err := tx.Outputs(context.Context())
assert.NoError(err, "failed getting outputs")
assert.True(outputs.Count() > 0)
assert.True(outputs.ByRecipient(id).Count() > 0)
Expand Down Expand Up @@ -97,7 +97,7 @@ func (a *AcceptPreparedCashView) Call(context view.Context) (any, error) {
// The recipient can perform any check on the transaction as required by the business process
// In particular, here, the recipient checks that the transaction contains at least one output, and
// that there is at least one output that names the recipient (The recipient is receiving something).
outputs, err := tx.Outputs()
outputs, err := tx.Outputs(context.Context())
assert.NoError(err, "failed getting outputs")
assert.True(outputs.Count() > 0)
assert.True(outputs.ByRecipient(id).Count() > 0)
Expand Down
2 changes: 1 addition & 1 deletion integration/token/fungible/views/auditor.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (a *AuditView) Call(context view.Context) (any, error) {
logger.Debugf("AuditView: get auditor [%s]", tx.ID())
auditor, err := ttx.NewAuditor(context, w)
assert.NoError(err, "failed to get auditor instance")
assert.NoError(auditor.Validate(tx), "failed auditing verification")
assert.NoError(auditor.Validate(context.Context(), tx), "failed auditing verification")
logger.Debugf("AuditView: get auditor done [%s]", tx.ID())

// Check ValidationRecords
Expand Down
4 changes: 2 additions & 2 deletions integration/token/fungible/views/boolpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (lv *PolicyLockView) Call(context view.Context) (any, error) {
)
assert.NoError(err, "failed creating transaction")

assert.NoError(bptx.Wrap(tx).Lock(senderWallet, lv.Type, lv.Amount, recipient), "failed adding lock")
assert.NoError(bptx.Wrap(tx).Lock(context.Context(), senderWallet, lv.Type, lv.Amount, recipient), "failed adding lock")

_, err = context.RunView(ttx.NewCollectEndorsementsView(tx))
assert.NoError(err, "failed to collect endorsements")
Expand Down Expand Up @@ -157,7 +157,7 @@ func (r *PolicySpendView) Call(context view.Context) (any, error) {
TxOpts(r.TMSID, ttx.WithAuditor(idProvider.Identity(r.Auditor)))...,
)
assert.NoError(err, "failed to create policy transaction")
assert.NoError(bptx.Wrap(tx).Spend(spendWallet, matched.At(0), recipient), "failed adding spend")
assert.NoError(bptx.Wrap(tx).Spend(context.Context(), spendWallet, matched.At(0), recipient), "failed adding spend")

var collectOpts []token2.ServiceOption
if len(r.Signers) > 0 {
Expand Down
1 change: 1 addition & 0 deletions integration/token/fungible/views/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func (p *IssueCashView) Call(context view.Context) (any, error) {

// The issuer adds a new issue operation to the transaction following the instruction received
err = tx.Issue(
context.Context(),
wallet,
recipient,
p.TokenType,
Expand Down
4 changes: 2 additions & 2 deletions integration/token/fungible/views/multisig.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (lv *MultiSigLockView) Call(context view.Context) (txID any, err error) {
assert.NoError(err, "failed creating transaction")

// lock
err = multisig.Wrap(tx).Lock(senderWallet, lv.Type, lv.Amount, recipient)
err = multisig.Wrap(tx).Lock(context.Context(), senderWallet, lv.Type, lv.Amount, recipient)
assert.NoError(err, "failed adding transfer action [%d:%v]", lv.Amount, recipient)

_, err = context.RunView(ttx.NewCollectEndorsementsView(tx))
Expand Down Expand Up @@ -148,7 +148,7 @@ func (r *MultiSigSpendView) Call(context view.Context) (res any, err error) {
TxOpts(r.TMSID, ttx.WithAuditor(idProvider.Identity(r.Auditor)))...,
)
assert.NoError(err, "failed to create an multisig transaction")
assert.NoError(multisig.Wrap(tx).Spend(spendWallet, matched.At(0), recipient), "failed adding a spend for [%s]", matched.At(0).Id)
assert.NoError(multisig.Wrap(tx).Spend(context.Context(), spendWallet, matched.At(0), recipient), "failed adding a spend for [%s]", matched.At(0).Id)

_, err = context.RunView(ttx.NewCollectEndorsementsView(tx))
assert.NoError(err, "failed to collect endorsements on multisig transaction")
Expand Down
1 change: 1 addition & 0 deletions integration/token/fungible/views/redeem.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func (t *RedeemView) Call(context view.Context) (any, error) {
opts = append(opts, ttx.WithIssuerPublicParamsPublicKey(t.IssuerPublicParamsPublicKey))
}
err = tx.Redeem(
context.Context(),
senderWallet,
t.Type,
t.Amount,
Expand Down
6 changes: 4 additions & 2 deletions integration/token/fungible/views/swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func (t *SwapInitiatorView) Call(context view.Context) (any, error) {

// Alice adds a new transfer operation to the transaction following the instruction received.
err = tx.Transfer(
context.Context(),
senderWallet,
t.FromAliceType,
[]uint64{t.FromAliceAmount},
Expand All @@ -87,7 +88,7 @@ func (t *SwapInitiatorView) Call(context view.Context) (any, error) {
// Alice doubles check that the content of the transaction is the one expected.
assert.NoError(tx.IsValid(context.Context()), "failed verifying transaction")

outputs, err := tx.Outputs()
outputs, err := tx.Outputs(context.Context())
assert.NoError(err, "failed getting outputs")
// get outputs by the type of tokens received from Alice.
os := outputs.ByRecipient(other).ByType(t.FromAliceType)
Expand Down Expand Up @@ -157,6 +158,7 @@ func (t *SwapResponderView) Call(context view.Context) (any, error) {
bobWallet := ttx.MyWalletFromTx(context, tx)
assert.NotNil(bobWallet, "To's default wallet not found")
err = tx.Transfer(
context.Context(),
bobWallet,
action.Type,
[]uint64{action.Amount},
Expand Down Expand Up @@ -191,7 +193,7 @@ func (t *SwapResponderView) Call(context view.Context) (any, error) {
assert.Equal(ttx.Confirmed, vc, "transaction [%s] should be in valid state", tx.ID())

// Check that the tokens are or are not in the db
outputs, err := tx.Outputs()
outputs, err := tx.Outputs(context.Context())
assert.NoError(err, "failed to retrieve outputs")
AssertTokens(context, tx, outputs, me)

Expand Down
8 changes: 7 additions & 1 deletion integration/token/fungible/views/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ func (t *TransferView) Call(context view.Context) (txID any, err error) {
// token2.WithTokenSelector(selector).
logger.DebugfContext(context.Context(), "Append transfer")
err = tx.Transfer(
context.Context(),
senderWallet,
t.Type,
[]uint64{t.Amount},
Expand All @@ -160,6 +161,7 @@ func (t *TransferView) Call(context view.Context) (txID any, err error) {
}

err = tx.Transfer(
context.Context(),
senderWallet,
t.Type,
[]uint64{action.Amount},
Expand Down Expand Up @@ -333,6 +335,7 @@ func (t *TransferWithSelectorView) Call(context view.Context) (any, error) {
// The sender adds a new transfer operation to the transaction following the instruction received.
// Notice the use of `token2.WithTokenIDs(t.TokenIDs...)` to pass the token ids selected above.
err = tx.Transfer(
context.Context(),
ttx.GetWallet(context, t.Wallet),
t.Type,
[]uint64{t.Amount},
Expand Down Expand Up @@ -429,6 +432,7 @@ func (t *PrepareTransferView) Call(context view.Context) (any, error) {
// It is also possible to pass a custom token selector to the Transfer function by using the relative opt:
// token2.WithTokenSelector(selector).
err = tx.Transfer(
context.Context(),
senderWallet,
t.Type,
[]uint64{t.Amount},
Expand All @@ -447,7 +451,7 @@ func (t *PrepareTransferView) Call(context view.Context) (any, error) {
_, err = context.RunView(ttx.NewCollectEndorsementsView(tx))
assert.NoError(err, "failed to sign transaction")

txRaw, err := tx.Bytes()
txRaw, err := tx.Bytes(context.Context())
assert.NoError(err, "failed to serialize transaction")

return &PrepareTransferResult{TxID: tx.ID(), TXRaw: txRaw}, nil
Expand Down Expand Up @@ -624,6 +628,7 @@ func (t *MaliciousTransferView) Call(context view.Context) (txID any, err error)
// It is also possible to pass a custom token selector to the MaliciousTransfer function by using the relative opt:
// token2.WithTokenSelector(selector).
err = tx.Transfer(
context.Context(),
senderWallet,
t.Type,
[]uint64{t.Amount},
Expand Down Expand Up @@ -670,6 +675,7 @@ func (t *MaliciousTransferView) Call(context view.Context) (txID any, err error)
self, err := senderWallet.GetRecipientIdentity(context.Context())
assert.NoError(err, "failed create recipient identity")
err = tx2.Transfer(
context.Context(),
senderWallet,
t.Type,
[]uint64{t.Amount},
Expand Down
3 changes: 2 additions & 1 deletion integration/token/fungible/views/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (i *TokensUpgradeInitiatorView) Call(context view.Context) (any, error) {
// The recipient can perform any check on the transaction as required by the business process
// In particular, here, the recipient checks that the transaction contains at least one output, and
// that there is at least one output that names the recipient.(The recipient is receiving something).
outputs, err := tx.Outputs()
outputs, err := tx.Outputs(context.Context())
assert.NoError(err, "failed getting outputs")
assert.True(outputs.Count() > 0, "expected at least one output")
assert.True(outputs.ByRecipient(id).Count() > 0, "expected at least one output assigned to [%s]", id)
Expand Down Expand Up @@ -176,6 +176,7 @@ func (p *TokensUpgradeResponderView) Call(context view.Context) (any, error) {

// The issuer adds a new issue operation to the transaction following the instruction received
err = tx.Upgrade(
context.Context(),
wallet,
upgradeRequest.RecipientData.Identity,
upgradeRequest.ID,
Expand Down
3 changes: 2 additions & 1 deletion integration/token/fungible/views/withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (i *WithdrawalInitiatorView) Call(context view.Context) (any, error) {
// The recipient can perform any check on the transaction as required by the business process
// In particular, here, the recipient checks that the transaction contains at least one output, and
// that there is at least one output that names the recipient.(The recipient is receiving something).
outputs, err := tx.Outputs()
outputs, err := tx.Outputs(context.Context())
assert.NoError(err, "failed getting outputs")
assert.True(outputs.Count() > 0, "expected at least one output")
assert.True(outputs.ByRecipient(id).Count() > 0, "expected at least one output assigned to [%s]", id)
Expand Down Expand Up @@ -163,6 +163,7 @@ func (p *WithdrawalResponderView) Call(context view.Context) (any, error) {

// The issuer adds a new issue operation to the transaction following the instruction received
err = tx.Issue(
context.Context(),
wallet,
issueRequest.RecipientData.Identity,
issueRequest.TokenType,
Expand Down
2 changes: 1 addition & 1 deletion integration/token/interop/views/auditor.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (a *AuditView) Call(context view.Context) (any, error) {
assert.NotNil(w, "failed getting default auditor wallet")
auditor, err := ttx.NewAuditor(context, w)
assert.NoError(err, "failed to get auditor instance")
assert.NoError(auditor.Validate(tx), "failed auditing verification")
assert.NoError(auditor.Validate(context.Context(), tx), "failed auditing verification")

// Check limits

Expand Down
2 changes: 1 addition & 1 deletion integration/token/interop/views/htlc/claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (r *ClaimView) Call(ctx view.Context) (res any, err error) {
ttx.WithTMSID(r.TMSID),
)
assert.NoError(err, "failed to create an htlc transaction")
assert.NoError(tx.Claim(claimWallet, matched.At(0), preImage), "failed adding a claim for [%s]", matched.At(0).Id)
assert.NoError(tx.Claim(ctx.Context(), claimWallet, matched.At(0), preImage), "failed adding a claim for [%s]", matched.At(0).Id)

_, err = ctx.RunView(htlc.NewCollectEndorsementsView(tx))
assert.NoError(err, "failed to collect endorsements on htlc transaction")
Expand Down
4 changes: 2 additions & 2 deletions integration/token/interop/views/htlc/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (v *FastExchangeInitiatorView) Call(context view.Context) (any, error) {
tx, err := htlc.ReceiveTransaction(context)
assert.NoError(err, "failed to receive tokens")

outputs, err := tx.Outputs()
outputs, err := tx.Outputs(context.Context())
assert.NoError(err, "failed getting outputs")
assert.True(outputs.Count() >= 1, "expected at least one output, got [%d]", outputs.Count())
outputs = outputs.ByScript()
Expand Down Expand Up @@ -187,7 +187,7 @@ func (v *FastExchangeResponderView) Call(ctx view.Context) (any, error) {
tx, err := htlc.ReceiveTransaction(ctx)
assert.NoError(err, "failed to receive tokens")

outputs, err := tx.Outputs()
outputs, err := tx.Outputs(ctx.Context())
assert.NoError(err, "failed getting outputs")
assert.True(outputs.Count() >= 1, "expected at least one output, got [%d]", outputs.Count())
outputs = outputs.ByScript()
Expand Down
Loading
Loading