forked from LFDT-Panurus/panurus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclaim.go
More file actions
81 lines (65 loc) · 2.46 KB
/
Copy pathclaim.go
File metadata and controls
81 lines (65 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package hashescrow
import (
"encoding/json"
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
"github.com/hyperledger-labs/fabric-smart-client/platform/common/utils/assert"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/id"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/view"
"github.com/hyperledger-labs/fabric-token-sdk/token"
"github.com/hyperledger-labs/fabric-token-sdk/token/services/interop/hashescrow"
"github.com/hyperledger-labs/fabric-token-sdk/token/services/ttx"
)
type Claim struct {
TMSID token.TMSID
Wallet string
PreImage []byte
}
type ClaimView struct {
*Claim
}
func (r *ClaimView) Call(ctx view.Context) (res interface{}, err error) {
var tx *hashescrow.Transaction
defer func() {
if e := recover(); e != nil {
txID := "none"
if tx != nil {
txID = tx.ID()
}
if err == nil {
err = errors.Errorf("<<<[%s]>>>: %s", txID, e)
} else {
err = errors.Errorf("<<<[%s]>>>: %s", txID, err)
}
}
}()
claimWallet := hashescrow.GetWallet(ctx, r.Wallet, token.WithTMSID(r.TMSID))
assert.NotNil(claimWallet, "wallet [%s] not found", r.Wallet)
matched, err := hashescrow.Wallet(claimWallet).ListByPreImage(ctx.Context(), r.PreImage)
assert.NoError(err, "failed looking up hash escrow script")
assert.True(matched.Count() == 1, "expected only one hash escrow script to match [%s], got [%d]", view.Identity(r.PreImage), matched.Count())
idProvider, err := id.GetProvider(ctx)
assert.NoError(err, "failed getting id provider")
tx, err = hashescrow.NewAnonymousTransaction(
ctx,
ttx.WithAuditor(idProvider.Identity("auditor")),
ttx.WithTMSID(r.TMSID),
)
assert.NoError(err, "failed to create a hash escrow transaction")
assert.NoError(tx.Claim(claimWallet, matched.At(0), r.PreImage), "failed adding a hash escrow claim for [%s]", matched.At(0).Id)
_, err = ctx.RunView(ttx.NewCollectEndorsementsView(tx.Transaction))
assert.NoError(err, "failed to collect endorsements on hash escrow transaction")
_, err = ctx.RunView(ttx.NewOrderingAndFinalityView(tx.Transaction))
assert.NoError(err, "failed to commit hash escrow transaction")
return tx.ID(), nil
}
type ClaimViewFactory struct{}
func (p *ClaimViewFactory) NewView(in []byte) (view.View, error) {
f := &ClaimView{Claim: &Claim{}}
err := json.Unmarshal(in, f.Claim)
assert.NoError(err, "failed unmarshalling input")
return f, nil
}