Skip to content

Commit

Permalink
feat: port BIP32 tests from the rust library
Browse files Browse the repository at this point in the history
  • Loading branch information
a5i committed Jul 4, 2021
1 parent 621bc63 commit c1de21c
Show file tree
Hide file tree
Showing 9 changed files with 3,708 additions and 103 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,8 @@ Address API example:
addrBase := NewBaseAddress(5, StakeCredetialFromKeyHash(keyBytes[:]), StakeCredetialFromScriptHash(scriptBytes[:]))
addrBytes := addrBase.ToBytes()
```
## Tests
You can run all tests with the command:
```go
go test ./...
```
12 changes: 12 additions & 0 deletions address/byron_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,15 @@ func (b ByronAddress) NetworkId() (uint8, error) {
}
return 0, errors.New("unexpected protocol magic")
}

// IcarusFromKey implements https://github.com/Emurgo/cardano-serialization-lib/blob/0e89deadf9183a129b9a25c0568eed177d6c6d7c/rust/src/address.rs#L229
// icarus-style address (Ae2)
func IcarusFromKey(key []byte, protocolMagic uint32) ByronAddress {
var filteredProtocolMagic *uint32
if protocolMagic == MainNet().ProtocolMagic {
filteredProtocolMagic = nil
} else {
filteredProtocolMagic = &protocolMagic
}
return NewSimpleExtendedAddr(key, filteredProtocolMagic).ToByronAddress()
}
21 changes: 14 additions & 7 deletions address/extended_addr.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"errors"
"fmt"
"github.com/fivebinaries/go-cardano-serialization/bip32"
"github.com/fivebinaries/go-cardano-serialization/crypto"
"github.com/fxamacker/cbor/v2"
"golang.org/x/crypto/blake2b"
"golang.org/x/crypto/sha3"
"hash/crc32"
"log"
Expand Down Expand Up @@ -84,6 +84,12 @@ func NewExtendedAddr(pub bip32.XPub, attributes AddrAttributes) *ExtendedAddr {
}
}

// NewSimpleExtendedAddr implements https://github.com/Emurgo/cardano-serialization-lib/blob/0e89deadf9183a129b9a25c0568eed177d6c6d7c/rust/src/legacy_address/address.rs#L295
// bootstrap era + no hdpayload address
func NewSimpleExtendedAddr(pub bip32.XPub, protocolMagic *uint32) *ExtendedAddr {
return NewExtendedAddr(pub, NewBootstrapEra(nil, protocolMagic))
}

// ToExtendedAddr convert extendedAddrCBOR to ExtendedAddr
func (addr extendedAddrCBOR) ToExtendedAddr() (ExtendedAddr, error) {
attr, err := addr.Attributes.ProcessAttributes()
Expand Down Expand Up @@ -113,12 +119,8 @@ func SHA3ThenBlake2b224(data []byte) []byte {
sh3 := sha3.New256()
sh3.Write(data)
sh3Result := sh3.Sum(nil)
b2b, err := blake2b.New(28, nil)
if err != nil {
log.Fatalf("error in sha3 then blake2b224 transform: %s", err)
}
b2b.Write(sh3Result[:])
return b2b.Sum(nil)
b2result := crypto.Blake2b224(sh3Result)
return b2result[:]
}

// ProcessAttributes method for converting AddrAttributesRaw to AddrAttributes
Expand Down Expand Up @@ -241,3 +243,8 @@ func (ea *ExtendedAddr) IdenticalWithPubKey(xpub *bip32.XPub) bool {
}
return bytes.Equal(addrBytes, neweaBytes)
}

// NewBootstrapEra implements https://github.com/Emurgo/cardano-serialization-lib/blob/0e89deadf9183a129b9a25c0568eed177d6c6d7c/rust/src/legacy_address/address.rs#L85
func NewBootstrapEra(hdap []byte, protocolMagic *uint32) AddrAttributes {
return AddrAttributes{DerivationPath: hdap, ProtocolMagic: protocolMagic}
}
Loading

0 comments on commit c1de21c

Please sign in to comment.