Skip to content

Commit

Permalink
simplified token owner
Browse files Browse the repository at this point in the history
Signed-off-by: Angelo De Caro <[email protected]>
  • Loading branch information
adecaro committed Nov 25, 2024
1 parent 8a17227 commit 71af03f
Show file tree
Hide file tree
Showing 24 changed files with 103 additions and 123 deletions.
2 changes: 1 addition & 1 deletion token/core/common/authrorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func NewTMSAuthorization(logger logging.Logger, publicParameters driver.PublicPa
// IsMine returns true if the passed token is owned by an owner wallet.
// It returns the ID of the owner wallet and no additional owner identifiers.
func (w *WalletBasedAuthorization) IsMine(tok *token2.Token) (string, []string, bool) {
wallet, err := w.WalletService.OwnerWallet(tok.Owner.Raw)
wallet, err := w.WalletService.OwnerWallet(tok.Owner)
if err != nil {
return "", nil, false
}
Expand Down
12 changes: 6 additions & 6 deletions token/core/common/wallets.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (w *AuditorWallet) Contains(identity driver.Identity) bool {
}

func (w *AuditorWallet) ContainsToken(token *token.UnspentToken) bool {
return w.Contains(token.Owner.Raw)
return w.Contains(token.Owner)
}

func (w *AuditorWallet) GetAuditorIdentity() (driver.Identity, error) {
Expand Down Expand Up @@ -88,7 +88,7 @@ func (w *IssuerWallet) Contains(identity driver.Identity) bool {
}

func (w *IssuerWallet) ContainsToken(token *token.UnspentToken) bool {
return w.Contains(token.Owner.Raw)
return w.Contains(token.Owner)
}

func (w *IssuerWallet) GetIssuerIdentity(tokenType string) (driver.Identity, error) {
Expand Down Expand Up @@ -116,7 +116,7 @@ func (w *IssuerWallet) HistoryTokens(opts *driver.ListTokensOptions) (*token.Iss
continue
}

if !w.Contains(t.Issuer.Raw) {
if !w.Contains(t.Issuer) {
w.Logger.Debugf("issuer wallet [%s]: discarding token, issuer does not belong to wallet", w.ID())
continue
}
Expand Down Expand Up @@ -152,7 +152,7 @@ func (w *CertifierWallet) Contains(identity driver.Identity) bool {
}

func (w *CertifierWallet) ContainsToken(token *token.UnspentToken) bool {
return w.Contains(token.Owner.Raw)
return w.Contains(token.Owner)
}

func (w *CertifierWallet) GetCertifierIdentity() (driver.Identity, error) {
Expand Down Expand Up @@ -193,7 +193,7 @@ func (w *LongTermOwnerWallet) Contains(identity driver.Identity) bool {
}

func (w *LongTermOwnerWallet) ContainsToken(token *token.UnspentToken) bool {
return w.Contains(token.Owner.Raw)
return w.Contains(token.Owner)
}

func (w *LongTermOwnerWallet) GetRecipientIdentity() (driver.Identity, error) {
Expand Down Expand Up @@ -309,7 +309,7 @@ func (w *AnonymousOwnerWallet) Contains(identity driver.Identity) bool {

// ContainsToken returns true if the passed token is owned by this wallet
func (w *AnonymousOwnerWallet) ContainsToken(token *token.UnspentToken) bool {
return w.Contains(token.Owner.Raw)
return w.Contains(token.Owner)
}

func (w *AnonymousOwnerWallet) GetRecipientIdentity() (driver.Identity, error) {
Expand Down
2 changes: 1 addition & 1 deletion token/core/fabtoken/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (t *Output) Serialize() ([]byte, error) {
// IsRedeem returns true if the owner of a Output is empty
// todo update interface to account for nil t.Output.Owner and nil t.Output
func (t *Output) IsRedeem() bool {
return len(t.Output.Owner.Raw) == 0
return len(t.Output.Owner) == 0
}

// IssueAction encodes a fabtoken Issue
Expand Down
4 changes: 1 addition & 3 deletions token/core/fabtoken/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ func (s *IssueService) Issue(ctx context.Context, issuerIdentity driver.Identity
}
outs = append(outs, &Output{
Output: &token2.Token{
Owner: &token2.Owner{
Raw: owners[i],
},
Owner: owners[i],
Type: tokenType,
Quantity: q.Hex(),
},
Expand Down
18 changes: 9 additions & 9 deletions token/core/fabtoken/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ func (s *TransferService) Transfer(ctx context.Context, txID string, wallet driv

var senders []driver.Identity
for _, tok := range inputTokens {
s.Logger.Debugf("Selected output [%s,%s,%s]", tok.Type, tok.Quantity, driver.Identity(tok.Owner.Raw))
senders = append(senders, tok.Owner.Raw)
s.Logger.Debugf("Selected output [%s,%s,%s]", tok.Type, tok.Quantity, driver.Identity(tok.Owner))
senders = append(senders, tok.Owner)
}

// prepare outputs
Expand Down Expand Up @@ -88,28 +88,28 @@ func (s *TransferService) Transfer(ctx context.Context, txID string, wallet driv
if output.Output == nil || output.Output.Owner == nil {
return nil, nil, errors.Errorf("failed to transfer: invalid output at index %d", i)
}
if len(output.Output.Owner.Raw) == 0 { // redeem
receivers = append(receivers, output.Output.Owner.Raw)
if len(output.Output.Owner) == 0 { // redeem
receivers = append(receivers, output.Output.Owner)
outputAuditInfos = append(outputAuditInfos, []byte{})
continue
}
recipients, err := s.Deserializer.Recipients(output.Output.Owner.Raw)
recipients, err := s.Deserializer.Recipients(output.Output.Owner)
if err != nil {
return nil, nil, errors.Wrap(err, "failed getting recipients")
}
receivers = append(receivers, recipients...)
auditInfo, err := s.Deserializer.GetOwnerAuditInfo(output.Output.Owner.Raw, ws)
auditInfo, err := s.Deserializer.GetOwnerAuditInfo(output.Output.Owner, ws)
if err != nil {
return nil, nil, errors.Wrapf(err, "failed getting audit info for sender identity [%s]", driver.Identity(output.Output.Owner.Raw).String())
return nil, nil, errors.Wrapf(err, "failed getting audit info for sender identity [%s]", driver.Identity(output.Output.Owner).String())
}
outputAuditInfos = append(outputAuditInfos, auditInfo...)
}

var senderAuditInfos [][]byte
for _, t := range inputTokens {
auditInfo, err := s.Deserializer.GetOwnerAuditInfo(t.Owner.Raw, ws)
auditInfo, err := s.Deserializer.GetOwnerAuditInfo(t.Owner, ws)
if err != nil {
return nil, nil, errors.Wrapf(err, "failed getting audit info for sender identity [%s]", driver.Identity(t.Owner.Raw).String())
return nil, nil, errors.Wrapf(err, "failed getting audit info for sender identity [%s]", driver.Identity(t.Owner).String())
}
senderAuditInfos = append(senderAuditInfos, auditInfo...)
}
Expand Down
18 changes: 9 additions & 9 deletions token/core/fabtoken/validator_transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ import (
func TransferSignatureValidate(ctx *Context) error {
ctx.InputTokens = ctx.TransferAction.InputTokens
for _, tok := range ctx.InputTokens {
ctx.Logger.Debugf("check sender [%s]", driver.Identity(tok.Owner.Raw).UniqueID())
verifier, err := ctx.Deserializer.GetOwnerVerifier(tok.Owner.Raw)
ctx.Logger.Debugf("check sender [%s]", driver.Identity(tok.Owner).UniqueID())
verifier, err := ctx.Deserializer.GetOwnerVerifier(tok.Owner)
if err != nil {
return errors.Wrapf(err, "failed deserializing owner [%v][%s]", tok, driver.Identity(tok.Owner.Raw).UniqueID())
return errors.Wrapf(err, "failed deserializing owner [%v][%s]", tok, driver.Identity(tok.Owner).UniqueID())
}
ctx.Logger.Debugf("signature verification [%v][%s]", tok, driver.Identity(tok.Owner.Raw).UniqueID())
sigma, err := ctx.SignatureProvider.HasBeenSignedBy(tok.Owner.Raw, verifier)
ctx.Logger.Debugf("signature verification [%v][%s]", tok, driver.Identity(tok.Owner).UniqueID())
sigma, err := ctx.SignatureProvider.HasBeenSignedBy(tok.Owner, verifier)
if err != nil {
return errors.Wrapf(err, "failed signature verification [%v][%s]", tok, driver.Identity(tok.Owner.Raw).UniqueID())
return errors.Wrapf(err, "failed signature verification [%v][%s]", tok, driver.Identity(tok.Owner).UniqueID())
}
ctx.Signatures = append(ctx.Signatures, sigma)
}
Expand Down Expand Up @@ -90,7 +90,7 @@ func TransferHTLCValidate(ctx *Context) error {
now := time.Now()

for i, in := range ctx.InputTokens {
owner, err := identity.UnmarshalTypedIdentity(in.Owner.Raw)
owner, err := identity.UnmarshalTypedIdentity(in.Owner)
if err != nil {
return errors.Wrap(err, "failed to unmarshal owner of input token")
}
Expand All @@ -115,7 +115,7 @@ func TransferHTLCValidate(ctx *Context) error {
}

// check owner field
script, op, err := htlc2.VerifyOwner(ctx.InputTokens[0].Owner.Raw, tok.Owner.Raw, now)
script, op, err := htlc2.VerifyOwner(ctx.InputTokens[0].Owner, tok.Owner, now)
if err != nil {
return errors.Wrap(err, "failed to verify transfer from htlc script")
}
Expand All @@ -142,7 +142,7 @@ func TransferHTLCValidate(ctx *Context) error {
}

// if it is an htlc script then the deadline must still be valid
owner, err := identity.UnmarshalTypedIdentity(out.Output.Owner.Raw)
owner, err := identity.UnmarshalTypedIdentity(out.Output.Owner)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion token/core/zkatdlog/crypto/token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (t *Token) GetTokenInTheClear(meta *Metadata, pp *crypto.PublicParams) (*to
return &token2.Token{
Type: meta.Type,
Quantity: "0x" + meta.Value.String(),
Owner: &token2.Owner{Raw: t.Owner},
Owner: t.Owner,
}, nil
}

Expand Down
14 changes: 7 additions & 7 deletions token/core/zkatdlog/nogh/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,20 @@ func (s *TransferService) Transfer(ctx context.Context, txID string, wallet driv
if output.Owner == nil {
return nil, nil, errors.Errorf("failed to get owner for %dth output: nil owner", i)
}
owners = append(owners, output.Owner.Raw)
if len(output.Owner.Raw) == 0 { // redeem
receivers = append(receivers, output.Owner.Raw)
owners = append(owners, output.Owner)
if len(output.Owner) == 0 { // redeem
receivers = append(receivers, output.Owner)
outputAuditInfos = append(outputAuditInfos, []byte{})
continue
}
recipients, err := s.Deserializer.Recipients(output.Owner.Raw)
recipients, err := s.Deserializer.Recipients(output.Owner)
if err != nil {
return nil, nil, errors.Wrap(err, "failed getting recipients")
}
receivers = append(receivers, recipients...)
auditInfo, err := s.Deserializer.GetOwnerAuditInfo(output.Owner.Raw, s.WalletService)
auditInfo, err := s.Deserializer.GetOwnerAuditInfo(output.Owner, s.WalletService)
if err != nil {
return nil, nil, errors.Wrapf(err, "failed getting audit info for sender identity [%s]", driver.Identity(output.Owner.Raw).String())
return nil, nil, errors.Wrapf(err, "failed getting audit info for sender identity [%s]", driver.Identity(output.Owner).String())
}
outputAuditInfos = append(outputAuditInfos, auditInfo...)
}
Expand Down Expand Up @@ -221,7 +221,7 @@ func (s *TransferService) VerifyTransfer(action driver.TransferAction, outputsMe
if err != nil {
return errors.Wrap(err, "failed getting token in the clear")
}
s.Logger.Debugf("transfer output [%s,%s,%s]", tok.Type, tok.Quantity, driver.Identity(tok.Owner.Raw))
s.Logger.Debugf("transfer output [%s,%s,%s]", tok.Type, tok.Quantity, driver.Identity(tok.Owner))
}

return transfer.NewVerifier(getTokenData(tr.InputTokens), com, pp).Verify(tr.Proof)
Expand Down
12 changes: 6 additions & 6 deletions token/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ func (r *Request) extractIssueOutputs(i int, counter uint64, issueAction driver.
outputs = append(outputs, &Output{
ActionIndex: i,
Index: counter,
Owner: tok.Owner.Raw,
Owner: tok.Owner,
OwnerAuditInfo: issueMeta.ReceiversAuditInfos[j],
EnrollmentID: eID,
RevocationHandler: rID,
Expand Down Expand Up @@ -545,7 +545,7 @@ func (r *Request) extractTransferOutputs(i int, counter uint64, transferAction d
var eID string
var rID string
var receiverAuditInfo []byte
if len(tok.Owner.Raw) != 0 {
if len(tok.Owner) != 0 {
receiverAuditInfo = transferMeta.ReceiverAuditInfos[j]
eID, rID, err = tms.WalletService().GetEIDAndRH(transferMeta.Receivers[j], receiverAuditInfo)
if err != nil {
Expand All @@ -565,7 +565,7 @@ func (r *Request) extractTransferOutputs(i int, counter uint64, transferAction d
outputs = append(outputs, &Output{
ActionIndex: i,
Index: counter,
Owner: tok.Owner.Raw,
Owner: tok.Owner,
OwnerAuditInfo: receiverAuditInfo,
EnrollmentID: eID,
RevocationHandler: rID,
Expand Down Expand Up @@ -1012,7 +1012,7 @@ func (r *Request) AuditRecord() (*AuditRecord, error) {
in.Quantity = q

// retrieve the owner's audit info
ownerAuditInfo, err := r.TokenService.tms.WalletService().GetAuditInfo(toks[i].Owner.Raw)
ownerAuditInfo, err := r.TokenService.tms.WalletService().GetAuditInfo(toks[i].Owner)
if err != nil {
return nil, errors.Wrapf(err, "failed getting audit info for owner [%s]", toks[i].Owner)
}
Expand Down Expand Up @@ -1184,7 +1184,7 @@ func (r *Request) prepareTransfer(redeem bool, wallet *OwnerWallet, tokenType st
}

outputTokens = append(outputTokens, &token.Token{
Owner: &token.Owner{Raw: restIdentity},
Owner: restIdentity,
Type: tokenType,
Quantity: diff.Hex(),
})
Expand Down Expand Up @@ -1229,7 +1229,7 @@ func (r *Request) genOutputs(values []uint64, owners []Identity, tokenType strin

// single output is fine
outputTokens = append(outputTokens, &token.Token{
Owner: &token.Owner{Raw: owners[i]},
Owner: owners[i],
Type: tokenType,
Quantity: q.Hex(),
})
Expand Down
12 changes: 6 additions & 6 deletions token/services/db/sql/common/test_cases.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func TSaveAndGetToken(t *testing.T, db *TokenDB) {
assert.Len(t, tok.ByType("TST").Tokens, 23, "expect filter on type to work")
for _, token := range tok.Tokens {
assert.NotNil(t, token.Owner, "expected owner to not be nil")
assert.NotEmpty(t, token.Owner.Raw, "expected owner raw to not be empty")
assert.NotEmpty(t, token.Owner, "expected owner raw to not be empty")
}

tokens := getTokensBy(t, db, "alice", "")
Expand Down Expand Up @@ -477,7 +477,7 @@ func TListAuditTokens(t *testing.T, db *TokenDB) {
assert.Equal(t, "0x02", tok[1].Quantity, "expected tx101-1 to be returned")
for _, token := range tok {
assert.NotNil(t, token.Owner, "expected owner to not be nil")
assert.NotEmpty(t, token.Owner.Raw, "expected owner raw to not be empty")
assert.NotEmpty(t, token.Owner, "expected owner raw to not be empty")
}

tok, err = db.ListAuditTokens()
Expand Down Expand Up @@ -552,19 +552,19 @@ func TListIssuedTokens(t *testing.T, db *TokenDB) {
assert.Equal(t, "0x03", tok.Tokens[2].Quantity, "expected tx102-0 to be returned")
for _, token := range tok.Tokens {
assert.NotNil(t, token.Issuer, "expected issuer to not be nil")
assert.NotEmpty(t, token.Issuer.Raw, "expected issuer raw to not be empty")
assert.NotEmpty(t, token.Issuer, "expected issuer raw to not be empty")
assert.NotNil(t, token.Owner, "expected owner to not be nil")
assert.NotEmpty(t, token.Owner.Raw, "expected owner raw to not be empty")
assert.NotEmpty(t, token.Owner, "expected owner raw to not be empty")
}

tok, err = db.ListHistoryIssuedTokens()
assert.NoError(t, err)
assert.Len(t, tok.ByType("DEF").Tokens, 1, "expected tx102-0 to be filtered")
for _, token := range tok.Tokens {
assert.NotNil(t, token.Issuer, "expected issuer to not be nil")
assert.NotEmpty(t, token.Issuer.Raw, "expected issuer raw to not be empty")
assert.NotEmpty(t, token.Issuer, "expected issuer raw to not be empty")
assert.NotNil(t, token.Owner, "expected owner to not be nil")
assert.NotEmpty(t, token.Owner.Raw, "expected owner raw to not be empty")
assert.NotEmpty(t, token.Owner, "expected owner raw to not be empty")
}
}

Expand Down
28 changes: 9 additions & 19 deletions token/services/db/sql/common/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,13 +259,11 @@ func (db *TokenDB) ListAuditTokens(ids ...*token.ID) ([]*token.Token, error) {
for rows.Next() {
id := token.ID{}
tok := token.Token{
Owner: &token.Owner{
Raw: []byte{},
},
Owner: []byte{},
Type: "",
Quantity: "",
}
if err := rows.Scan(&id.TxId, &id.Index, &tok.Owner.Raw, &tok.Type, &tok.Quantity); err != nil {
if err := rows.Scan(&id.TxId, &id.Index, &tok.Owner, &tok.Type, &tok.Quantity); err != nil {
return tokens, err
}

Expand Down Expand Up @@ -317,16 +315,12 @@ func (db *TokenDB) ListHistoryIssuedTokens() (*token.IssuedTokens, error) {
TxId: "",
Index: 0,
},
Owner: &token.Owner{
Raw: []byte{},
},
Owner: []byte{},
Type: "",
Quantity: "",
Issuer: &token.Owner{
Raw: []byte{},
},
Issuer: []byte{},
}
if err := rows.Scan(&tok.Id.TxId, &tok.Id.Index, &tok.Owner.Raw, &tok.Type, &tok.Quantity, &tok.Issuer.Raw); err != nil {
if err := rows.Scan(&tok.Id.TxId, &tok.Id.Index, &tok.Owner, &tok.Type, &tok.Quantity, &tok.Issuer); err != nil {
return nil, err
}
tokens = append(tokens, &tok)
Expand Down Expand Up @@ -498,7 +492,7 @@ func (db *TokenDB) GetTokens(inputs ...*token.ID) ([]*token.Token, error) {
return tokens, err
}
tok := &token.Token{
Owner: &token.Owner{Raw: ownerRaw},
Owner: ownerRaw,
Type: typ,
Quantity: quantity,
}
Expand Down Expand Up @@ -927,9 +921,7 @@ func (t *TokenTransaction) GetToken(ctx context.Context, txID string, index uint
return nil, owners, nil
}
return &token.Token{
Owner: &token.Owner{
Raw: raw,
},
Owner: raw,
Type: tokenType,
Quantity: quantity,
}, owners, nil
Expand Down Expand Up @@ -1076,10 +1068,8 @@ func (u *UnspentTokensIterator) Next() (*token.UnspentToken, error) {
return nil, err
}
return &token.UnspentToken{
Id: &id,
Owner: &token.Owner{
Raw: owner,
},
Id: &id,
Owner: owner,
Type: typ,
Quantity: quantity,
}, err
Expand Down
Loading

0 comments on commit 71af03f

Please sign in to comment.