Skip to content

Commit 8939b42

Browse files
committed
tapsend: remove now unused functions
We are now validating the input amount and types when creating the allocations and no longer need this function.
1 parent 0c4e418 commit 8939b42

File tree

2 files changed

+0
-320
lines changed

2 files changed

+0
-320
lines changed

tapsend/send.go

-102
Original file line numberDiff line numberDiff line change
@@ -277,108 +277,6 @@ func DescribeAddrs(addrs []*address.Tap) (*FundingDescriptor, error) {
277277
return desc, nil
278278
}
279279

280-
// AssetFromTapCommitment uses a script key to extract an asset from a given
281-
// Taproot Asset commitment.
282-
func AssetFromTapCommitment(tapCommitment *commitment.TapCommitment,
283-
specifier asset.Specifier,
284-
inputScriptKey btcec.PublicKey) (*asset.Asset, error) {
285-
286-
// The top-level Taproot Asset tree must have a non-empty asset tree at
287-
// the leaf specified by the funding descriptor's asset (group) specific
288-
// commitment locator.
289-
tapKey := asset.TapCommitmentKey(specifier)
290-
assetCommitments := tapCommitment.Commitments()
291-
assetCommitment, ok := assetCommitments[tapKey]
292-
if !ok {
293-
return nil, fmt.Errorf("input commitment does "+
294-
"not contain asset=%s: %w", specifier,
295-
ErrMissingInputAsset)
296-
}
297-
298-
// Determine whether issuance is disabled for the asset.
299-
issuanceDisabled := !specifier.HasGroupPubKey()
300-
301-
assetId, err := specifier.UnwrapIdOrErr()
302-
if err != nil {
303-
return nil, fmt.Errorf("asset from tap commitment: %w", err)
304-
}
305-
306-
// The asset tree must have a non-empty Asset at the location
307-
// specified by the sender's script key.
308-
assetCommitmentKey := asset.AssetCommitmentKey(
309-
assetId, &inputScriptKey, issuanceDisabled,
310-
)
311-
inputAsset, ok := assetCommitment.Asset(assetCommitmentKey)
312-
if !ok {
313-
return nil, fmt.Errorf("input commitment does not "+
314-
"contain leaf with script_key=%x: %w",
315-
inputScriptKey.SerializeCompressed(),
316-
ErrMissingInputAsset)
317-
}
318-
319-
return inputAsset, nil
320-
}
321-
322-
// ValidateInputs validates a set of inputs against a funding request. It
323-
// returns true if the inputs would be spent fully, otherwise false.
324-
func ValidateInputs(inputCommitments tappsbt.InputCommitments,
325-
expectedAssetType asset.Type, specifier asset.Specifier,
326-
outputAmount uint64) (bool, error) {
327-
328-
// Extract the input assets from the input commitments.
329-
inputAssets := make([]*asset.Asset, 0, len(inputCommitments))
330-
for prevID := range inputCommitments {
331-
tapCommitment := inputCommitments[prevID]
332-
senderScriptKey, err := prevID.ScriptKey.ToPubKey()
333-
if err != nil {
334-
return false, fmt.Errorf("unable to parse sender "+
335-
"script key: %v", err)
336-
}
337-
338-
// Gain the asset that we'll use as an input and in the process
339-
// validate the selected input and commitment.
340-
inputAsset, err := AssetFromTapCommitment(
341-
tapCommitment, specifier, *senderScriptKey,
342-
)
343-
if err != nil {
344-
return false, err
345-
}
346-
347-
// Ensure input asset has the expected type.
348-
if inputAsset.Type != expectedAssetType {
349-
return false, fmt.Errorf("unexpected input asset type")
350-
}
351-
352-
inputAssets = append(inputAssets, inputAsset)
353-
}
354-
355-
// Validate total amount of input assets and determine full value spend
356-
// status.
357-
var isFullValueSpend bool
358-
switch expectedAssetType {
359-
case asset.Normal:
360-
// Sum the total amount of the input assets.
361-
var totalInputsAmount uint64
362-
for _, inputAsset := range inputAssets {
363-
totalInputsAmount += inputAsset.Amount
364-
}
365-
366-
// Ensure that the input assets are sufficient to cover the amount
367-
// being sent.
368-
if totalInputsAmount < outputAmount {
369-
return false, ErrInsufficientInputAssets
370-
}
371-
372-
// Check if the input assets are fully spent.
373-
isFullValueSpend = totalInputsAmount == outputAmount
374-
375-
case asset.Collectible:
376-
isFullValueSpend = true
377-
}
378-
379-
return isFullValueSpend, nil
380-
}
381-
382280
// ValidateCommitmentKeysUnique makes sure the outputs of a set of virtual
383281
// packets don't lead to collisions in and of the trees (e.g. two asset outputs
384282
// with the same asset ID and script key in the same anchor output) or with

tapsend/send_test.go

-218
Original file line numberDiff line numberDiff line change
@@ -1895,224 +1895,6 @@ func TestProofVerifyFullValueSplit(t *testing.T) {
18951895
require.NoError(t, err)
18961896
}
18971897

1898-
// TestAddressValidInput tests edge cases around validating inputs for asset
1899-
// transfers with isValidInput.
1900-
func TestAddressValidInput(t *testing.T) {
1901-
t.Parallel()
1902-
1903-
for _, testCase := range addressValidInputTestCases {
1904-
success := t.Run(testCase.name, func(t *testing.T) {
1905-
inputAsset, checkedInputAsset, err := testCase.f(t)
1906-
require.ErrorIs(t, err, testCase.err)
1907-
if testCase.err == nil {
1908-
require.True(t, inputAsset.DeepEqual(
1909-
checkedInputAsset,
1910-
))
1911-
}
1912-
})
1913-
if !success {
1914-
return
1915-
}
1916-
}
1917-
}
1918-
1919-
func addrToFundDesc(addr address.Tap) *tapsend.FundingDescriptor {
1920-
assetSpecifier := asset.NewSpecifierOptionalGroupPubKey(
1921-
addr.AssetID, addr.GroupKey,
1922-
)
1923-
1924-
return &tapsend.FundingDescriptor{
1925-
AssetSpecifier: assetSpecifier,
1926-
Amount: addr.Amount,
1927-
}
1928-
}
1929-
1930-
type addressValidInputTestCase struct {
1931-
name string
1932-
f func(t *testing.T) (*asset.Asset, *asset.Asset, error)
1933-
err error
1934-
}
1935-
1936-
var addressValidInputTestCases = []addressValidInputTestCase{{
1937-
name: "valid normal",
1938-
f: func(t *testing.T) (*asset.Asset, *asset.Asset, error) {
1939-
state := initSpendScenario(t)
1940-
fundDesc := addrToFundDesc(state.address1)
1941-
1942-
inputAsset, err := tapsend.AssetFromTapCommitment(
1943-
&state.asset1TapTree, fundDesc.AssetSpecifier,
1944-
state.spenderScriptKey,
1945-
)
1946-
if err != nil {
1947-
return nil, nil, err
1948-
}
1949-
1950-
fullValue, err := tapsend.ValidateInputs(
1951-
tappsbt.InputCommitments{
1952-
state.asset1PrevID: &state.asset1TapTree,
1953-
}, inputAsset.Type, fundDesc.AssetSpecifier,
1954-
fundDesc.Amount,
1955-
)
1956-
if err != nil {
1957-
return nil, nil, err
1958-
}
1959-
require.True(t, fullValue)
1960-
1961-
return &state.asset1, inputAsset, nil
1962-
},
1963-
err: nil,
1964-
}, {
1965-
name: "valid collectible with group key",
1966-
f: func(t *testing.T) (*asset.Asset, *asset.Asset, error) {
1967-
state := initSpendScenario(t)
1968-
fundDesc := addrToFundDesc(state.address1CollectGroup)
1969-
1970-
inputAsset, err := tapsend.AssetFromTapCommitment(
1971-
&state.asset1CollectGroupTapTree,
1972-
fundDesc.AssetSpecifier, state.spenderScriptKey,
1973-
)
1974-
if err != nil {
1975-
return nil, nil, err
1976-
}
1977-
1978-
inputCommitment := &state.asset1CollectGroupTapTree
1979-
fullValue, err := tapsend.ValidateInputs(
1980-
tappsbt.InputCommitments{
1981-
state.asset1CollectGroupPrevID: inputCommitment,
1982-
}, inputAsset.Type, fundDesc.AssetSpecifier,
1983-
fundDesc.Amount,
1984-
)
1985-
if err != nil {
1986-
return nil, nil, err
1987-
}
1988-
require.True(t, fullValue)
1989-
1990-
return &state.asset1CollectGroup, inputAsset, nil
1991-
},
1992-
err: nil,
1993-
}, {
1994-
name: "valid asset split",
1995-
f: func(t *testing.T) (*asset.Asset, *asset.Asset, error) {
1996-
state := initSpendScenario(t)
1997-
fundDesc := addrToFundDesc(state.address1)
1998-
1999-
inputAsset, err := tapsend.AssetFromTapCommitment(
2000-
&state.asset2TapTree, fundDesc.AssetSpecifier,
2001-
state.spenderScriptKey,
2002-
)
2003-
if err != nil {
2004-
return nil, nil, err
2005-
}
2006-
2007-
fullValue, err := tapsend.ValidateInputs(
2008-
tappsbt.InputCommitments{
2009-
state.asset2PrevID: &state.asset2TapTree,
2010-
}, inputAsset.Type, fundDesc.AssetSpecifier,
2011-
fundDesc.Amount,
2012-
)
2013-
if err != nil {
2014-
return nil, nil, err
2015-
}
2016-
require.False(t, fullValue)
2017-
2018-
return &state.asset2, inputAsset, nil
2019-
},
2020-
err: nil,
2021-
}, {
2022-
name: "normal with insufficient amount",
2023-
f: func(t *testing.T) (*asset.Asset, *asset.Asset, error) {
2024-
state := initSpendScenario(t)
2025-
fundDesc := addrToFundDesc(state.address2)
2026-
2027-
inputAsset, err := tapsend.AssetFromTapCommitment(
2028-
&state.asset1TapTree, fundDesc.AssetSpecifier,
2029-
state.spenderScriptKey,
2030-
)
2031-
if err != nil {
2032-
return nil, nil, err
2033-
}
2034-
2035-
fullValue, err := tapsend.ValidateInputs(
2036-
tappsbt.InputCommitments{
2037-
state.asset1PrevID: &state.asset1TapTree,
2038-
}, inputAsset.Type, fundDesc.AssetSpecifier,
2039-
fundDesc.Amount,
2040-
)
2041-
if err != nil {
2042-
return nil, nil, err
2043-
}
2044-
require.True(t, fullValue)
2045-
2046-
return &state.asset1, inputAsset, nil
2047-
},
2048-
err: tapsend.ErrInsufficientInputAssets,
2049-
}, {
2050-
name: "collectible with missing input asset",
2051-
f: func(t *testing.T) (*asset.Asset, *asset.Asset, error) {
2052-
state := initSpendScenario(t)
2053-
fundDesc := addrToFundDesc(state.address1CollectGroup)
2054-
2055-
inputAsset, err := tapsend.AssetFromTapCommitment(
2056-
&state.asset1TapTree, fundDesc.AssetSpecifier,
2057-
state.spenderScriptKey,
2058-
)
2059-
if err != nil {
2060-
return nil, nil, err
2061-
}
2062-
2063-
fullValue, err := tapsend.ValidateInputs(
2064-
tappsbt.InputCommitments{
2065-
state.asset1PrevID: &state.asset1TapTree,
2066-
}, inputAsset.Type, fundDesc.AssetSpecifier,
2067-
fundDesc.Amount,
2068-
)
2069-
if err != nil {
2070-
return nil, nil, err
2071-
}
2072-
require.False(t, fullValue)
2073-
2074-
return &state.asset1, inputAsset, nil
2075-
},
2076-
err: tapsend.ErrMissingInputAsset,
2077-
}, {
2078-
name: "normal with bad sender script key",
2079-
f: func(t *testing.T) (*asset.Asset, *asset.Asset, error) {
2080-
state := initSpendScenario(t)
2081-
2082-
address1testnet, err := address.New(
2083-
address.V0, state.genesis1, nil, nil,
2084-
state.receiverPubKey, state.receiverPubKey,
2085-
state.normalAmt1, nil, &address.TestNet3Tap,
2086-
address.RandProofCourierAddr(t),
2087-
)
2088-
require.NoError(t, err)
2089-
2090-
fundDesc := addrToFundDesc(*address1testnet)
2091-
2092-
inputAsset, err := tapsend.AssetFromTapCommitment(
2093-
&state.asset1TapTree, fundDesc.AssetSpecifier,
2094-
state.receiverPubKey,
2095-
)
2096-
if err != nil {
2097-
return nil, nil, err
2098-
}
2099-
2100-
fullValue, err := tapsend.ValidateInputs(
2101-
tappsbt.InputCommitments{
2102-
state.asset1PrevID: &state.asset1TapTree,
2103-
}, inputAsset.Type, fundDesc.AssetSpecifier,
2104-
fundDesc.Amount,
2105-
)
2106-
if err != nil {
2107-
return nil, nil, err
2108-
}
2109-
require.True(t, fullValue)
2110-
2111-
return &state.asset1, inputAsset, nil
2112-
},
2113-
err: tapsend.ErrMissingInputAsset,
2114-
}}
2115-
21161898
// TestPayToAddrScript tests edge cases around creating a P2TR script with
21171899
// PayToAddrScript.
21181900
func TestPayToAddrScript(t *testing.T) {

0 commit comments

Comments
 (0)