Skip to content

Commit

Permalink
Added sing_payload message handling;
Browse files Browse the repository at this point in the history
  • Loading branch information
k-karuna committed May 31, 2022
1 parent 50039e3 commit c9e8dca
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 73 deletions.
10 changes: 5 additions & 5 deletions Beacon.Sdk/Beacon/Operation/OperationRequest.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Collections.Generic;
using Beacon.Sdk.Beacon.Permission;

namespace Beacon.Sdk.Beacon.Operation
{
using System.Collections.Generic;
using Permission;

public record OperationRequest : BaseBeaconMessage
{
public OperationRequest(
Expand All @@ -12,8 +12,8 @@ public OperationRequest(
string senderId,
Network network,
List<PartialTezosTransactionOperation> operationDetails,
string sourceAddress)
: base(type, version, id, senderId)
string sourceAddress
) : base(type, version, id, senderId)
{
Network = network;
OperationDetails = operationDetails;
Expand Down
6 changes: 4 additions & 2 deletions Beacon.Sdk/Beacon/Operation/TezosTransactionOperation.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Newtonsoft.Json.Linq;

namespace Beacon.Sdk.Beacon.Operation
{
public record TezosTransactionOperation(
Expand All @@ -9,11 +11,11 @@ public record TezosTransactionOperation(
string storage_limit,
string amount,
string destination)
: TezosBaseOperation(kind); //, IPartialTezosOperation;
: TezosBaseOperation(kind);

public record PartialTezosTransactionOperation(
string Amount,
string Destination,
string Parameters)
JObject? Parameters)
: TezosBaseOperation(TezosOperationType.transaction), IPartialTezosOperation;
}
24 changes: 24 additions & 0 deletions Beacon.Sdk/Beacon/Sign/SignPayloadRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace Beacon.Sdk.Beacon.Sign
{
public record SignPayloadRequest : BaseBeaconMessage
{
public SignPayloadRequest(
string id,
string version,
string senderId,
SignPayloadType? signingType,
string payload,
string sourceAddress
)
: base(BeaconMessageType.sign_payload_request, version, id, senderId)
{
SigningType = signingType;
Payload = payload;
SourceAddress = sourceAddress;
}

public SignPayloadType? SigningType { get; }
public string Payload { get; }
public string SourceAddress { get; }
}
}
17 changes: 17 additions & 0 deletions Beacon.Sdk/Beacon/Sign/SignPayloadResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Beacon.Sdk.Beacon.Sign
{
public record SignPayloadResponse : BaseBeaconMessage
{
public SignPayloadResponse(
string signature,
string version,
string id,
string senderId
) : base(BeaconMessageType.sign_payload_response, version, id, senderId)
{
Signature = signature;
}

public string Signature { get; }
}
}
9 changes: 9 additions & 0 deletions Beacon.Sdk/Beacon/Sign/SignPayloadType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Beacon.Sdk.Beacon.Sign
{
public enum SignPayloadType
{
raw,
operation,
micheline
}
}
61 changes: 14 additions & 47 deletions Beacon.Sdk/Core/Domain/RequestMessageHandler.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Beacon.Sdk.Beacon.Sign;

namespace Beacon.Sdk.Core.Domain
{
using System;
Expand All @@ -20,74 +22,39 @@ public RequestMessageHandler(IAppMetadataRepository appMetadataRepository,

public (AcknowledgeResponse, BaseBeaconMessage) Handle(string message, string senderId)
{
BaseBeaconMessage baseBeaconMessage = _jsonSerializerService.Deserialize<BaseBeaconMessage>(message);
var baseBeaconMessage = _jsonSerializerService.Deserialize<BaseBeaconMessage>(message);
var ack = new AcknowledgeResponse(baseBeaconMessage.Id, senderId, baseBeaconMessage.Version);

return baseBeaconMessage.Type switch
{
BeaconMessageType.permission_request => (ack, HandlePermissionRequest(message)),
BeaconMessageType.operation_request => (ack, HandleOperationRequest(message)),
BeaconMessageType.sign_payload_request => (ack, HandleSignPayloadRequest(message)),
_ => throw new Exception("Unknown beaconMessage.Type.")
};
}

private BaseBeaconMessage HandlePermissionRequest(string message)
{
PermissionRequest request = _jsonSerializerService.Deserialize<PermissionRequest>(message);

var request = _jsonSerializerService.Deserialize<PermissionRequest>(message);
_ = _appMetadataRepository.CreateOrUpdateAsync(request.AppMetadata).Result;

return request;
}

private BaseBeaconMessage HandleOperationRequest(string message)
{
// todo: Deserialize this
// {
// "id": "39250d96-050d-3d0c-8fe8-369545d82a40",
// "version": "2",
// "senderId": "2Z5r4gMaaPvwc",
// "type": "operation_request",
// "network": {
// "type": "mainnet",
// "name": "Mainnet",
// "rpcUrl": "https://rpc.tzkt.io/mainnet"
// },
// "operationDetails": [{
// "kind": "transaction",
// "amount": "1000000",
// "destination": "KT1D6XTy8oAHkUWdzuQrzySECCDMnANEchQq",
// "parameters": {
// "entrypoint": "bet",
// "value": {
// "prim": "Pair",
// "args": [{
// "prim": "Pair",
// "args": [{
// "prim": "Left",
// "args": [{
// "prim": "Unit"
// }]
// }, {
// "int": "1515"
// }]
// }, {
// "int": "1933606"
// }]
// }
// }
// }],
// "sourceAddress": "tz1RDE4JdUo73bx23cwjr97gDkcP63b4NfGD"
// }
// Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: {. Path 'operationDetails[0].parameters', line 1, position 326.
// at Newtonsoft.Json.JsonTextReader.ReadStringValue(ReadType readType)
// at Newtonsoft.Json.Json…

OperationRequest request = _jsonSerializerService.Deserialize<OperationRequest>(message);
var operationRequest = _jsonSerializerService.Deserialize<OperationRequest>(message);
_ = _appMetadataRepository.TryReadAsync(operationRequest.SenderId).Result;

return operationRequest;
}

_ = _appMetadataRepository.TryReadAsync(request.SenderId).Result;

return request;
private BaseBeaconMessage HandleSignPayloadRequest(string message)
{
var signPayloadRequest = _jsonSerializerService.Deserialize<SignPayloadRequest>(message);
return signPayloadRequest;
}
}
}
26 changes: 16 additions & 10 deletions Beacon.Sdk/Core/Domain/ResponseMessageHandler.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Beacon.Sdk.Beacon.Sign;

namespace Beacon.Sdk.Core.Domain
{
using System;
Expand Down Expand Up @@ -35,11 +37,16 @@ PermissionInfoFactory permissionInfoFactory
public string Handle(BaseBeaconMessage response, string receiverId) =>
response.Type switch
{
BeaconMessageType.acknowledge => HandleAcknowledge(response as AcknowledgeResponse),
BeaconMessageType.permission_response => HandlePermissionResponse(receiverId,
response as PermissionResponse),
BeaconMessageType.operation_response => HandleOperationResponse(response as OperationResponse),
BeaconMessageType.error => HandleError(response as BaseBeaconError),
BeaconMessageType.acknowledge =>
HandleAcknowledge(response as AcknowledgeResponse),
BeaconMessageType.permission_response =>
HandlePermissionResponse(receiverId, response as PermissionResponse),
BeaconMessageType.operation_response =>
HandleOperationResponse(response as OperationResponse),
BeaconMessageType.sign_payload_response =>
HandleSignPayloadResponse(response as SignPayloadResponse),
BeaconMessageType.error =>
HandleError(response as BaseBeaconError),

_ => throw new ArgumentException("Invalid beacon message type")
};
Expand All @@ -61,20 +68,19 @@ private string HandlePermissionResponse(string receiverId, PermissionResponse re
response.Network,
response.Scopes);


info = _permissionInfoRepository.CreateOrUpdateAsync(info).Result;

OnDappConnected?.Invoke(this, new DappConnectedEventArgs(receiverAppMetadata, info));

return _jsonSerializerService.Serialize(response);
}
//response.PublicKey,
// PubKey.FromBase64(response.PublicKey),
// PubKey.FromBase58(response.PublicKey),

private string HandleOperationResponse(OperationResponse response) =>
_jsonSerializerService.Serialize(response);

private string HandleSignPayloadResponse(SignPayloadResponse response) =>
_jsonSerializerService.Serialize(response);

private string HandleError(BaseBeaconError response)
{
if (response!.ErrorType == BeaconErrorType.ABORTED_ERROR)
Expand Down
33 changes: 24 additions & 9 deletions Beacon.Sdk/WalletBeaconClient/WalletBeaconClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,31 @@ private async Task HandleMessage(string message)
_logger.LogInformation("Received message have not permission");
}

private async Task<bool> HasPermission(BaseBeaconMessage beaconRequest) =>
beaconRequest.Type switch
private async Task<bool> HasPermission(BaseBeaconMessage beaconRequest)
{
switch (beaconRequest.Type)
{
BeaconMessageType.permission_request => true,
BeaconMessageType.broadcast_request => true,
BeaconMessageType.operation_request => await HandleOperationRequest(beaconRequest as OperationRequest),
_ => false
};
case BeaconMessageType.permission_request:
case BeaconMessageType.broadcast_request:
return true;

case BeaconMessageType.operation_request:
{
var request = beaconRequest as OperationRequest;
var permissionInfo = await TryReadPermissionInfo(request!.SourceAddress, request.SenderId, request.Network);

return permissionInfo != null && permissionInfo.Scopes.Contains(PermissionScope.operation_request);
}
// todo: handle this permission
case BeaconMessageType.sign_payload_request:
return true;

default:
return false;
}
}

private async Task<bool> HandleOperationRequest(OperationRequest request)
private async Task<bool> CheckOperationRequestHasPermission(OperationRequest request)
{
PermissionInfo? permissionInfo = await TryReadPermissionInfo(request.SourceAddress, request.SenderId, request.Network);

Expand All @@ -178,7 +193,7 @@ private async Task<bool> HandleOperationRequest(OperationRequest request)

public async Task<PermissionInfo?> TryReadPermissionInfo(string sourceAddress, string senderId, Network network)
{
string accountId = AccountService.GetAccountId(sourceAddress, network);
var accountId = AccountService.GetAccountId(sourceAddress, network);

return await PermissionInfoRepository.TryReadAsync(senderId, accountId);
}
Expand Down

0 comments on commit c9e8dca

Please sign in to comment.