Skip to content

check owner to identify redeemed tokens #1039

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions token/services/tokens/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,9 @@ func (t *Tokens) extractActions(tmsID token.TMSID, txID string, request *token.R
if err != nil {
return nil, nil, errors.WithMessagef(err, "failed to get request's outputs")
}
toSpend, toAppend := t.parse(auth, txID, md, is, os, auditorFlag, precision, graphHiding)
toSpend, toAppend, err := t.parse(auth, txID, md, is, os, auditorFlag, precision, graphHiding)
logger.Debugf("transaction [%s] parsed [%d] inputs and [%d] outputs", txID, len(toSpend), len(toAppend))
return toSpend, toAppend, nil
return toSpend, toAppend, err
}

// parse returns the tokens to store and spend as the result of a transaction
Expand All @@ -373,7 +373,7 @@ func (t *Tokens) parse(
auditorFlag bool,
precision uint64,
graphHiding bool,
) (toSpend []*token2.ID, toAppend []TokenToAppend) {
) (toSpend []*token2.ID, toAppend []TokenToAppend, err error) {
if graphHiding {
ids := md.SpentTokenID()
logger.Debugf("transaction [%s] with graph hiding, delete inputs [%v]", txID, ids)
Expand All @@ -394,13 +394,14 @@ func (t *Tokens) parse(

// parse the outputs
for _, output := range os.Outputs() {
// get token in the clear
if len(output.LedgerOutput) == 0 {
logger.Debugf("transaction [%s] without graph hiding, delete input [%d]", txID, output.Index)
toSpend = append(toSpend, &token2.ID{TxId: txID, Index: output.Index})

// if this is a redeem, then skip
if len(output.Token.Owner) == 0 {
logger.Debugf("output [%s:%d] is a redeem", txID, output.Index)
continue
}

// process the output to identify the relations with the current TMS
issuerFlag := !output.Issuer.IsNone() && auth.Issued(output.Issuer, &output.Token)
ownerWalletID, ids, mine := auth.IsMine(&output.Token)
if logger.IsEnabledFor(zapcore.DebugLevel) {
Expand All @@ -421,8 +422,7 @@ func (t *Tokens) parse(

ownerType, ownerIdentity, err := auth.OwnerType(output.Token.Owner)
if err != nil {
logger.Errorf("could not unmarshal identity when storing token: %s", err.Error())
continue
return nil, nil, errors.Wrapf(err, "failed to extract owner type for token [%s:%d]", txID, output.Index)
}

tta := TokenToAppend{
Expand Down
24 changes: 15 additions & 9 deletions token/services/tokens/tokens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ func TestParse(t *testing.T) {
}
output1 := &token.Output{
Token: token2.Token{
Type: "TOK",
Type: "TOK",
Owner: []byte("alice"),
},
ActionIndex: 0,
Index: 0,
Expand All @@ -75,7 +76,8 @@ func TestParse(t *testing.T) {
is := token.NewInputStream(qsMock{}, []*token.Input{input1}, 64)
os := token.NewOutputStream([]*token.Output{output1}, 64)

spend, store := tokens.parse(&authMock{}, "tx1", md, is, os, false, 64, false)
spend, store, err := tokens.parse(&authMock{}, "tx1", md, is, os, false, 64, false)
assert.NoError(t, err)

assert.Len(t, spend, 1)
assert.Equal(t, "in", spend[0].TxId)
Expand All @@ -91,11 +93,12 @@ func TestParse(t *testing.T) {
assert.Equal(t, uint64(64), store[0].precision)
assert.Equal(t, output1.Type, store[0].tok.Type)

// no ledger output -> spend
output1.LedgerOutput = []byte{}
// no owner, then a redeemed token
output1.Token.Owner = []byte{}
os = token.NewOutputStream([]*token.Output{output1}, 64)
spend, store = tokens.parse(&authMock{}, "tx1", md, is, os, false, 64, false)
assert.Len(t, spend, 2)
spend, store, err = tokens.parse(&authMock{}, "tx1", md, is, os, false, 64, false)
assert.NoError(t, err)
assert.Len(t, spend, 1)
assert.Len(t, store, 0)

// transfer with several inputs and outputs
Expand All @@ -121,7 +124,8 @@ func TestParse(t *testing.T) {
}
output1 = &token.Output{
Token: token2.Token{
Type: "TOK",
Type: "TOK",
Owner: []byte("alice"),
},
ActionIndex: 0,
Index: 0,
Expand All @@ -132,7 +136,8 @@ func TestParse(t *testing.T) {
}
output2 := &token.Output{
Token: token2.Token{
Type: "TOK",
Type: "TOK",
Owner: []byte("bob"),
},
ActionIndex: 0,
Index: 1,
Expand All @@ -144,7 +149,8 @@ func TestParse(t *testing.T) {
is = token.NewInputStream(qsMock{}, []*token.Input{input1, input2}, 64)
os = token.NewOutputStream([]*token.Output{output1, output2}, 64)

spend, store = tokens.parse(&authMock{}, "tx2", md, is, os, false, 64, false)
spend, store, err = tokens.parse(&authMock{}, "tx2", md, is, os, false, 64, false)
assert.NoError(t, err)
assert.Len(t, spend, 2)
assert.Equal(t, "in1", spend[0].TxId)
assert.Equal(t, uint64(1), spend[0].Index)
Expand Down