Skip to content

Commit 5420ad3

Browse files
committed
priceoraclerpc: add helper function identifying BTC asset specifier
In this commit, we introduce a function called IsAssetBtc. This function detects whether the provided AssetSpecifier represents Bitcoin (BTC).
1 parent eeca76c commit 5420ad3

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed

taprpc/priceoraclerpc/marshal.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package priceoraclerpc
2+
3+
import (
4+
"bytes"
5+
"encoding/hex"
6+
)
7+
8+
// IsAssetBtc is a helper function that returns true if the given asset
9+
// specifier represents BTC, and false otherwise.
10+
func IsAssetBtc(assetSpecifier *AssetSpecifier) bool {
11+
// An unset asset specifier does not represent BTC.
12+
if assetSpecifier == nil {
13+
return false
14+
}
15+
16+
// Verify that the asset specifier has a valid asset ID (either bytes or
17+
// string). The asset ID must be all zeros for the asset specifier to
18+
// represent BTC.
19+
assetIdBytes := assetSpecifier.GetAssetId()
20+
assetIdStr := assetSpecifier.GetAssetIdStr()
21+
22+
if len(assetIdBytes) != 32 && assetIdStr == "" {
23+
return false
24+
}
25+
26+
var assetId [32]byte
27+
copy(assetId[:], assetIdBytes)
28+
29+
var zeroAssetId [32]byte
30+
zeroAssetHexStr := hex.EncodeToString(zeroAssetId[:])
31+
32+
isAssetIdZero := bytes.Equal(assetId[:], zeroAssetId[:]) ||
33+
assetIdStr == zeroAssetHexStr
34+
35+
// Ensure that the asset specifier does not have any group key related
36+
// fields set. When specifying BTC, the group key fields must be unset.
37+
groupKeySet := assetSpecifier.GetGroupKey() != nil ||
38+
assetSpecifier.GetGroupKeyStr() != ""
39+
40+
return isAssetIdZero && !groupKeySet
41+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package priceoraclerpc
2+
3+
import (
4+
"encoding/hex"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
// isAssetBtcTC is a test case for the IsAssetBtc function.
11+
type isAssetBtcTC struct {
12+
testName string
13+
14+
assetSpecifier *AssetSpecifier
15+
expected bool
16+
}
17+
18+
// TestIsAssetBtc tests the IsAssetBtc function. The IsAssetBtc function
19+
// returns true if the given asset specifier represents BTC, and false
20+
// otherwise.
21+
func TestIsAssetBtc(t *testing.T) {
22+
t.Parallel()
23+
24+
var zeroAssetId [32]byte
25+
zeroAssetHexStr := hex.EncodeToString(zeroAssetId[:])
26+
27+
testCases := []isAssetBtcTC{
28+
{
29+
testName: "nil asset specifier",
30+
assetSpecifier: nil,
31+
expected: false,
32+
},
33+
{
34+
testName: "empty asset specifier",
35+
assetSpecifier: &AssetSpecifier{},
36+
expected: false,
37+
},
38+
{
39+
testName: "asset specifier with zero asset ID bytes",
40+
assetSpecifier: &AssetSpecifier{
41+
Id: &AssetSpecifier_AssetId{
42+
AssetId: zeroAssetId[:],
43+
},
44+
},
45+
expected: true,
46+
},
47+
{
48+
testName: "asset specifier with incorrect length " +
49+
"zero asset ID bytes",
50+
assetSpecifier: &AssetSpecifier{
51+
Id: &AssetSpecifier_AssetId{
52+
AssetId: []byte{0, 0, 0},
53+
},
54+
},
55+
expected: false,
56+
},
57+
{
58+
testName: "asset specifier with empty asset ID bytes",
59+
assetSpecifier: &AssetSpecifier{
60+
Id: &AssetSpecifier_AssetId{
61+
AssetId: []byte{},
62+
},
63+
},
64+
expected: false,
65+
},
66+
{
67+
testName: "asset specifier with zero asset ID string",
68+
assetSpecifier: &AssetSpecifier{
69+
Id: &AssetSpecifier_AssetIdStr{
70+
AssetIdStr: zeroAssetHexStr,
71+
},
72+
},
73+
expected: true,
74+
},
75+
{
76+
testName: "asset specifier with empty asset ID string",
77+
assetSpecifier: &AssetSpecifier{
78+
Id: &AssetSpecifier_AssetIdStr{
79+
AssetIdStr: "",
80+
},
81+
},
82+
expected: false,
83+
},
84+
{
85+
testName: "asset specifier with set group key bytes",
86+
assetSpecifier: &AssetSpecifier{
87+
Id: &AssetSpecifier_GroupKey{
88+
GroupKey: []byte{0, 0, 0},
89+
},
90+
},
91+
expected: false,
92+
},
93+
{
94+
testName: "asset specifier with set group key string",
95+
assetSpecifier: &AssetSpecifier{
96+
Id: &AssetSpecifier_GroupKeyStr{
97+
GroupKeyStr: "test-group-key",
98+
},
99+
},
100+
expected: false,
101+
},
102+
}
103+
104+
for _, tc := range testCases {
105+
t.Run(tc.testName, func(tt *testing.T) {
106+
// Run the test case. Ensure that the expected value is
107+
// returned.
108+
actual := IsAssetBtc(tc.assetSpecifier)
109+
require.Equal(tt, tc.expected, actual)
110+
})
111+
}
112+
}

0 commit comments

Comments
 (0)