Skip to content

Commit

Permalink
Add support for fetching issued assets.
Browse files Browse the repository at this point in the history
  • Loading branch information
LINCKODE committed May 23, 2024
1 parent ab41eed commit d28afd2
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 8 deletions.
17 changes: 17 additions & 0 deletions qubic.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,23 @@ func (qc *Client) getPeers(ctx context.Context) (types.PublicPeers, error) {
return result, nil
}

func (qc *Client) GetIssuedAssets(ctx context.Context, id string) (types.IssuedAssets, error) {

identity := types.Identity(id)
pubKey, err := identity.ToPubKey(false)
if err != nil {
return types.IssuedAssets{}, errors.Wrap(err, "converting identity to public key")
}
var result types.IssuedAssets
err = qc.sendRequest(ctx, types.IssuedAssetsRequest, pubKey, &result)
if err != nil {
return types.IssuedAssets{}, errors.Wrap(err, "sending req to node")
}

return result, nil

}

func (qc *Client) GetPossessedAssets(ctx context.Context, id string) (types.PossessedAssets, error) {

identity := types.Identity(id)
Expand Down
55 changes: 47 additions & 8 deletions types/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,45 @@ type IssuedAsset struct {

type IssuedAssets []IssuedAsset

func (ia *IssuedAssets) UnmarshallFromReader(r io.Reader) error {
for {
var header RequestResponseHeader
err := binary.Read(r, binary.BigEndian, &header)
if err != nil {
return errors.Wrap(err, "reading header")
}

if header.Type == EndResponse {
break
}

if header.Type != IssuedAssetsResponse {
return errors.Errorf("Invalid header type, expected %d, found %d", IssuedAssetsResponse, header.Type)
}

var issuedAssetData IssuedAssetData
err = issuedAssetData.UnmarshallBinary(r)
if err != nil {
return errors.Wrap(err, "unmarshalling issued asset data")
}

var assetInfo AssetInfo
err = assetInfo.UnmarshallBinary(r)
if err != nil {
return errors.Wrap(err, "reading issued asset info")
}

issuedAsset := IssuedAsset{
Data: issuedAssetData,
Info: assetInfo,
}

*ia = append(*ia, issuedAsset)
}

return nil
}

func (ad *IssuedAssetData) UnmarshallBinary(r io.Reader) error {

err := binary.Read(r, binary.LittleEndian, &ad.PublicKey)
Expand Down Expand Up @@ -135,39 +174,39 @@ func (pa *PossessedAssets) UnmarshallFromReader(r io.Reader) error {
return nil
}

func (pd *PossessedAssetData) UnmarshallBinary(r io.Reader) error {
func (ad *PossessedAssetData) UnmarshallBinary(r io.Reader) error {

err := binary.Read(r, binary.LittleEndian, &pd.PublicKey)
err := binary.Read(r, binary.LittleEndian, &ad.PublicKey)
if err != nil {
return errors.Wrap(err, "reading asset data")
}

err = binary.Read(r, binary.LittleEndian, &pd.Type)
err = binary.Read(r, binary.LittleEndian, &ad.Type)
if err != nil {
return errors.Wrap(err, "reading asset type")
}

err = binary.Read(r, binary.LittleEndian, &pd.Padding)
err = binary.Read(r, binary.LittleEndian, &ad.Padding)
if err != nil {
return errors.Wrap(err, "reading asset padding")
}

err = binary.Read(r, binary.LittleEndian, &pd.ManagingContractIndex)
err = binary.Read(r, binary.LittleEndian, &ad.ManagingContractIndex)
if err != nil {
return errors.Wrap(err, "reading asset managing contract index")
}

err = binary.Read(r, binary.LittleEndian, &pd.IssuanceIndex)
err = binary.Read(r, binary.LittleEndian, &ad.IssuanceIndex)
if err != nil {
return errors.Wrap(err, "reading asset issuance index")
}

err = binary.Read(r, binary.LittleEndian, &pd.NumberOfUnits)
err = binary.Read(r, binary.LittleEndian, &ad.NumberOfUnits)
if err != nil {
return errors.Wrap(err, "reading asset number of units")
}

err = pd.OwnedAsset.UnmarshallBinary(r)
err = ad.OwnedAsset.UnmarshallBinary(r)
if err != nil {
return errors.Wrap(err, "reading owned asset")
}
Expand Down

0 comments on commit d28afd2

Please sign in to comment.