Skip to content

Commit

Permalink
feat: extract payment portion from address
Browse files Browse the repository at this point in the history
  • Loading branch information
agaffney committed Dec 28, 2023
1 parent 8f2eb43 commit 1b29581
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
35 changes: 32 additions & 3 deletions ledger/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,14 +271,43 @@ func (a *Address) MarshalCBOR() ([]byte, error) {
return cbor.Encode(a.Bytes())
}

// PaymentAddress returns a new Address with only the payment address portion. This will return nil for anything other than payment and script addresses
func (a Address) PaymentAddress() *Address {
var addrType uint8
if a.addressType == AddressTypeKeyKey ||
a.addressType == AddressTypeKeyNone {
addrType = AddressTypeKeyNone
} else if a.addressType == AddressTypeScriptKey ||
a.addressType == AddressTypeScriptNone ||
a.addressType == AddressTypeScriptScript {
addrType = AddressTypeScriptNone
} else {
// Unsupported address type
return nil
}
newAddr := &Address{
addressType: addrType,
networkId: a.networkId,
paymentAddress: a.paymentAddress[:],
}
return newAddr
}

// StakeAddress returns a new Address with only the stake key portion. This will return nil if the address is not a payment/staking key pair
func (a Address) StakeAddress() *Address {
if a.addressType != AddressTypeKeyKey &&
a.addressType != AddressTypeScriptKey {
var addrType uint8
if a.addressType == AddressTypeKeyKey ||
a.addressType == AddressTypeScriptKey {
addrType = AddressTypeNoneKey
} else if a.addressType == AddressTypeScriptScript ||
a.addressType == AddressTypeNoneScript {
addrType = AddressTypeNoneScript
} else {
// Unsupported address type
return nil
}
newAddr := &Address{
addressType: AddressTypeNoneKey,
addressType: addrType,
networkId: a.networkId,
stakingAddress: a.stakingAddress[:],
}
Expand Down
34 changes: 34 additions & 0 deletions ledger/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,40 @@ func TestAddressFromParts(t *testing.T) {
}
}

func TestAddressPaymentAddress(t *testing.T) {
testDefs := []struct {
address string
expectedPaymentAddress string
}{
{
address: "addr_test1qqawz5hm2tchtmarkfn2tamzvd2spatl89gtutgra6zwc3ktqj7p944ckc9lq7u36jrq99znwhzlq6jfv2j4ql92m4rq07hp8t",
expectedPaymentAddress: "addr_test1vqawz5hm2tchtmarkfn2tamzvd2spatl89gtutgra6zwc3s5t5a7q",
},
{
address: "addr_test1vpmwd5tk8quxnzxq46h8vztf00xtphrd7zd0al5ur5jsylg3r9v4l",
expectedPaymentAddress: "addr_test1vpmwd5tk8quxnzxq46h8vztf00xtphrd7zd0al5ur5jsylg3r9v4l",
},
// Script address with script staking key
{
address: "addr1x8nz307k3sr60gu0e47cmajssy4fmld7u493a4xztjrll0aj764lvrxdayh2ux30fl0ktuh27csgmpevdu89jlxppvrswgxsta",
expectedPaymentAddress: "addr1w8nz307k3sr60gu0e47cmajssy4fmld7u493a4xztjrll0cm9703s",
},
}
for _, testDef := range testDefs {
addr, err := NewAddress(testDef.address)
if err != nil {
t.Fatalf("failed to decode address: %s", err)
}
if addr.PaymentAddress().String() != testDef.expectedPaymentAddress {
t.Fatalf(
"payment address did not match expected value, got: %s, wanted: %s",
addr.PaymentAddress().String(),
testDef.expectedPaymentAddress,
)
}
}
}

func TestAddressStakeAddress(t *testing.T) {
testDefs := []struct {
address string
Expand Down

0 comments on commit 1b29581

Please sign in to comment.