@@ -6,12 +6,14 @@ import (
66 "encoding/hex"
77 "testing"
88
9+ ethCommon "github.com/ethereum/go-ethereum/common"
910 "github.com/stretchr/testify/require"
1011
1112 "github.com/oasisprotocol/oasis-core/go/common/cbor"
1213 "github.com/oasisprotocol/oasis-core/go/common/quantity"
1314
1415 "github.com/oasisprotocol/oasis-sdk/client-sdk/go/config"
16+ sdkSignature "github.com/oasisprotocol/oasis-sdk/client-sdk/go/crypto/signature"
1517)
1618
1719func TestToken (t * testing.T ) {
@@ -76,9 +78,62 @@ func TestPrettyPrintToAmount(t *testing.T) {
7678 PrettyPrintToAmount (ctx , "" , & buf , & to , amt )
7779 require .Equal ("To: test:dave (oasis1qrk58a6j2qn065m6p06jgjyt032f7qucy5wqeqpt)\n Amount: 50.0 TEST\n " , buf .String ())
7880
81+ // Preserve the user-provided Ethereum address even when the account is unnamed.
82+ buf .Reset ()
83+ ethAddr := ethCommon .HexToAddress ("0x60a6321ea71d37102dbf923aae2e08d005c4e403" )
84+ ethTo := NewAddressFromEth (ethAddr .Bytes ())
85+ ctx2 := context .Background ()
86+ ctx2 = context .WithValue (ctx2 , config .ContextKeyParaTimeCfg , ptCfg )
87+ ctx2 = context .WithValue (ctx2 , sdkSignature .ContextKeySigContext , & sdkSignature.RichContext {
88+ TxDetails : & sdkSignature.TxDetails {OrigTo : & ethAddr },
89+ })
90+ PrettyPrintToAmount (ctx2 , "" , & buf , & ethTo , amt )
91+ require .Equal ("To: " + ethAddr .Hex ()+ "\n Amount: 50.0 TEST\n " , buf .String ())
92+
7993 // No ParaTime set. Amount cannot be correctly determined.
8094 buf .Reset ()
8195 ctx = context .WithValue (ctx , config .ContextKeyParaTimeCfg , nil )
8296 PrettyPrintToAmount (ctx , "" , & buf , & to , amt )
8397 require .Equal ("To: test:dave (oasis1qrk58a6j2qn065m6p06jgjyt032f7qucy5wqeqpt)\n Amount: <error: ParaTime information not available>\n " , buf .String ())
8498}
99+
100+ func TestFormatNamedAddressWith (t * testing.T ) {
101+ require := require .New (t )
102+
103+ ethHex := "0x60a6321ea71d37102dbf923aae2e08d005c4e403"
104+ ethBytes , err := hex .DecodeString (ethHex [2 :])
105+ require .NoError (err )
106+
107+ addr := NewAddressFromEth (ethBytes )
108+ native := addr .String ()
109+
110+ t .Run ("unknown returns native" , func (_ * testing.T ) {
111+ require .Equal (native , FormatNamedAddressWith (nil , nil , addr ))
112+ require .Equal (native , FormatNamedAddressWith (AccountNames {}, map [string ]string {}, addr ))
113+ })
114+
115+ t .Run ("native fallback when eth unknown" , func (_ * testing.T ) {
116+ names := AccountNames {native : "my" }
117+ require .Equal ("my (" + native + ")" , FormatNamedAddressWith (names , nil , addr ))
118+ require .Equal ("my (" + native + ")" , FormatNamedAddressWith (names , map [string ]string {}, addr ))
119+ require .Equal ("my (" + native + ")" , FormatNamedAddressWith (names , map [string ]string {native : "" }, addr ))
120+ })
121+
122+ t .Run ("eth preferred when known" , func (_ * testing.T ) {
123+ names := AccountNames {native : "my" }
124+ ethMap := map [string ]string {native : ethHex }
125+ require .Equal ("my (" + ethHex + ")" , FormatNamedAddressWith (names , ethMap , addr ))
126+ })
127+
128+ t .Run ("name equals preferred yields preferred" , func (_ * testing.T ) {
129+ names := AccountNames {native : native }
130+ require .Equal (native , FormatNamedAddressWith (names , nil , addr ))
131+ })
132+
133+ t .Run ("ctx wrapper reads maps" , func (_ * testing.T ) {
134+ ctx := context .Background ()
135+ ctx = context .WithValue (ctx , ContextKeyAccountNames , AccountNames {native : "my" })
136+ ctx = context .WithValue (ctx , ContextKeyAccountEthMap , map [string ]string {native : ethHex })
137+ require .Equal ("my (" + ethHex + ")" , FormatNamedAddress (ctx , addr ))
138+ })
139+ }
0 commit comments