|
1 | 1 | package keeper |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "context" |
| 6 | + "encoding/hex" |
| 7 | + "errors" |
5 | 8 | "fairyring/x/pep/types" |
| 9 | + "fmt" |
| 10 | + "strconv" |
| 11 | + |
| 12 | + enc "github.com/FairBlock/DistributedIBE/encryption" |
| 13 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 14 | + bls "github.com/drand/kyber-bls12381" |
6 | 15 | ) |
7 | 16 |
|
8 | 17 | func (k msgServer) CreateAggregatedKeyShare(goCtx context.Context, msg *types.MsgCreateAggregatedKeyShare) (*types.MsgCreateAggregatedKeyShareResponse, error) { |
9 | | - // No execution of this message is done here. |
10 | | - // Validation and actual execution happens in the beginblock directly from the mempool |
| 18 | + ctx := sdk.UnwrapSDKContext(goCtx) |
| 19 | + |
| 20 | + params := k.GetParams(ctx) |
| 21 | + var trusted bool = false |
| 22 | + |
| 23 | + for _, trustedAddr := range params.TrustedAddresses { |
| 24 | + if trustedAddr == msg.Creator { |
| 25 | + trusted = true |
| 26 | + break |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + if !trusted { |
| 31 | + return nil, errors.New("msg not from trusted source") |
| 32 | + } |
| 33 | + |
| 34 | + var dummyData = "test data" |
| 35 | + var encryptedDataBytes bytes.Buffer |
| 36 | + var dummyDataBuffer bytes.Buffer |
| 37 | + dummyDataBuffer.Write([]byte(dummyData)) |
| 38 | + var decryptedDataBytes bytes.Buffer |
| 39 | + |
| 40 | + ak, found := k.GetActivePubKey(ctx) |
| 41 | + if !found { |
| 42 | + k.Logger(ctx).Error("Active key not found") |
| 43 | + return nil, errors.New("active key not found") |
| 44 | + } |
| 45 | + |
| 46 | + keyByte, _ := hex.DecodeString(msg.Data) |
| 47 | + publicKeyByte, _ := hex.DecodeString(ak.PublicKey) |
| 48 | + |
| 49 | + suite := bls.NewBLS12381Suite() |
| 50 | + publicKeyPoint := suite.G1().Point() |
| 51 | + if err := publicKeyPoint.UnmarshalBinary(publicKeyByte); err != nil { |
| 52 | + return nil, err |
| 53 | + } |
| 54 | + |
| 55 | + skPoint := suite.G2().Point() |
| 56 | + if err := skPoint.UnmarshalBinary(keyByte); err != nil { |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + |
| 60 | + processHeightStr := strconv.FormatUint(msg.Height, 10) |
| 61 | + if err := enc.Encrypt(publicKeyPoint, []byte(processHeightStr), &encryptedDataBytes, &dummyDataBuffer); err != nil { |
| 62 | + return nil, err |
| 63 | + } |
| 64 | + |
| 65 | + err := enc.Decrypt(publicKeyPoint, skPoint, &decryptedDataBytes, &encryptedDataBytes) |
| 66 | + if err != nil { |
| 67 | + k.Logger(ctx).Error("Decryption error when verifying aggregated keyshare") |
| 68 | + k.Logger(ctx).Error(err.Error()) |
| 69 | + ctx.EventManager().EmitEvent( |
| 70 | + sdk.NewEvent(types.KeyShareVerificationType, |
| 71 | + sdk.NewAttribute(types.KeyShareVerificationCreator, msg.Creator), |
| 72 | + sdk.NewAttribute(types.KeyShareVerificationHeight, strconv.FormatUint(msg.Height, 10)), |
| 73 | + sdk.NewAttribute(types.KeyShareVerificationReason, err.Error()), |
| 74 | + ), |
| 75 | + ) |
| 76 | + return nil, err |
| 77 | + } |
| 78 | + |
| 79 | + if decryptedDataBytes.String() != dummyData { |
| 80 | + k.Logger(ctx).Error("Decrypted data does not match original data") |
| 81 | + k.Logger(ctx).Error(err.Error()) |
| 82 | + ctx.EventManager().EmitEvent( |
| 83 | + sdk.NewEvent(types.KeyShareVerificationType, |
| 84 | + sdk.NewAttribute(types.KeyShareVerificationCreator, msg.Creator), |
| 85 | + sdk.NewAttribute(types.KeyShareVerificationHeight, strconv.FormatUint(msg.Height, 10)), |
| 86 | + sdk.NewAttribute(types.KeyShareVerificationReason, "decrypted data does not match original data"), |
| 87 | + ), |
| 88 | + ) |
| 89 | + return nil, err |
| 90 | + } |
| 91 | + |
| 92 | + k.SetAggregatedKeyShare(ctx, types.AggregatedKeyShare{ |
| 93 | + Height: msg.Height, |
| 94 | + Data: msg.Data, |
| 95 | + Creator: msg.Creator, |
| 96 | + }) |
| 97 | + |
| 98 | + latestHeight, err := strconv.ParseUint(k.GetLatestHeight(ctx), 10, 64) |
| 99 | + if err != nil { |
| 100 | + latestHeight = 0 |
| 101 | + } |
| 102 | + |
| 103 | + if latestHeight < msg.Height { |
| 104 | + k.SetLatestHeight(ctx, strconv.FormatUint(msg.Height, 10)) |
| 105 | + } |
| 106 | + |
| 107 | + k.Logger(ctx).Info(fmt.Sprintf("[ProcessUnconfirmedTxs] Aggregated Key Added, height: %d", msg.Height)) |
11 | 108 |
|
12 | 109 | return &types.MsgCreateAggregatedKeyShareResponse{}, nil |
13 | 110 | } |
0 commit comments