Skip to content
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

feat: new API to get Public key by address #704

Merged
merged 13 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func NewNode(genDoc *genesis.Genesis, conf *config.Config,
}

http := http.NewServer(conf.HTTP)
grpc := grpc.NewServer(conf.GRPC, state, sync, consMgr)
grpc := grpc.NewServer(conf.GRPC, state, store, sync, consMgr)
kehiy marked this conversation as resolved.
Show resolved Hide resolved
nanomsg := nanomsg.NewServer(conf.Nanomsg, eventCh)

node := &Node{
Expand Down
6 changes: 6 additions & 0 deletions wallet/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ func (s *blockchainServer) GetValidator(_ context.Context,
return nil, fmt.Errorf("unknown request")
}

func (s *blockchainServer) GetPublicKey(_ context.Context,
_ *pactus.GetPublicKeyRequest,
) (*pactus.GetPublicKeyResponse, error) {
return &pactus.GetPublicKeyResponse{}, nil
}

func (s *transactionServer) GetTransaction(_ context.Context,
_ *pactus.GetTransactionRequest,
) (*pactus.GetTransactionResponse, error) {
Expand Down
18 changes: 18 additions & 0 deletions www/grpc/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/pactus-project/pactus/crypto"
"github.com/pactus-project/pactus/crypto/hash"
"github.com/pactus-project/pactus/state"
"github.com/pactus-project/pactus/store"
"github.com/pactus-project/pactus/types/account"
"github.com/pactus-project/pactus/types/validator"
"github.com/pactus-project/pactus/types/vote"
Expand All @@ -18,6 +19,7 @@ import (

type blockchainServer struct {
state state.Facade
store store.Reader
consMgr consensus.ManagerReader
logger *logger.SubLogger
}
Expand Down Expand Up @@ -222,6 +224,22 @@ func (s *blockchainServer) GetValidatorAddresses(_ context.Context,
return &pactus.GetValidatorAddressesResponse{Addresses: addressesPB}, nil
}

func (s *blockchainServer) GetPublicKey(_ context.Context,
req *pactus.GetPublicKeyRequest,
) (*pactus.GetPublicKeyResponse, error) {
addr, err := crypto.AddressFromString(req.Address)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid account address: %v", err.Error())
}

publicKey, err := s.store.PublicKey(addr)
if err != nil {
return nil, status.Errorf(codes.NotFound, "account not found")
kehiy marked this conversation as resolved.
Show resolved Hide resolved
}

return &pactus.GetPublicKeyResponse{PublicKey: publicKey.String()}, nil
}

func validatorToProto(val *validator.Validator) *pactus.ValidatorInfo {
data, _ := val.Bytes()
return &pactus.ValidatorInfo{
Expand Down
33 changes: 33 additions & 0 deletions www/grpc/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,39 @@ func TestGetValidatorAddresses(t *testing.T) {
assert.Nil(t, conn.Close(), "Error closing connection")
}

func TestGetPublicKey(t *testing.T) {
ts := testsuite.NewTestSuite(t)

conn, client := testBlockchainClient(t)
_, signer := tMockState.TestStore.AddTestAccount()

t.Run("Should return error for non-parsable address ", func(t *testing.T) {
res, err := client.GetPublicKey(tCtx,
&pactus.GetPublicKeyRequest{Address: ""})

assert.Error(t, err)
assert.Nil(t, res)
})

t.Run("Should return nil for non existing account ", func(t *testing.T) {
kehiy marked this conversation as resolved.
Show resolved Hide resolved
res, err := client.GetPublicKey(tCtx,
&pactus.GetPublicKeyRequest{Address: ts.RandAddress().String()})

assert.Error(t, err)
assert.Nil(t, res)
})

t.Run("Should return account public key", func(t *testing.T) {
kehiy marked this conversation as resolved.
Show resolved Hide resolved
res, err := client.GetPublicKey(tCtx,
&pactus.GetPublicKeyRequest{Address: signer.Address().String()})

assert.Nil(t, err)
assert.NotNil(t, res)
assert.Equal(t, res.PublicKey, signer.PublicKey().String())
})
assert.Nil(t, conn.Close(), "Error closing connection")
}

func TestConsensusInfo(t *testing.T) {
ts := testsuite.NewTestSuite(t)

Expand Down
98 changes: 98 additions & 0 deletions www/grpc/gen/dart/blockchain.pb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,100 @@ class GetValidatorResponse extends $pb.GeneratedMessage {
ValidatorInfo ensureValidator() => $_ensure(0);
}

class GetPublicKeyRequest extends $pb.GeneratedMessage {
static final $pb.BuilderInfo _i = $pb.BuilderInfo(const $core.bool.fromEnvironment('protobuf.omit_message_names') ? '' : 'GetPublicKeyRequest', package: const $pb.PackageName(const $core.bool.fromEnvironment('protobuf.omit_message_names') ? '' : 'pactus'), createEmptyInstance: create)
..aOS(1, const $core.bool.fromEnvironment('protobuf.omit_field_names') ? '' : 'address')
..hasRequiredFields = false
;

GetPublicKeyRequest._() : super();
factory GetPublicKeyRequest({
$core.String? address,
}) {
final _result = create();
if (address != null) {
_result.address = address;
}
return _result;
}
factory GetPublicKeyRequest.fromBuffer($core.List<$core.int> i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromBuffer(i, r);
factory GetPublicKeyRequest.fromJson($core.String i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromJson(i, r);
@$core.Deprecated(
'Using this can add significant overhead to your binary. '
'Use [GeneratedMessageGenericExtensions.deepCopy] instead. '
'Will be removed in next major version')
GetPublicKeyRequest clone() => GetPublicKeyRequest()..mergeFromMessage(this);
@$core.Deprecated(
'Using this can add significant overhead to your binary. '
'Use [GeneratedMessageGenericExtensions.rebuild] instead. '
'Will be removed in next major version')
GetPublicKeyRequest copyWith(void Function(GetPublicKeyRequest) updates) => super.copyWith((message) => updates(message as GetPublicKeyRequest)) as GetPublicKeyRequest; // ignore: deprecated_member_use
$pb.BuilderInfo get info_ => _i;
@$core.pragma('dart2js:noInline')
static GetPublicKeyRequest create() => GetPublicKeyRequest._();
GetPublicKeyRequest createEmptyInstance() => create();
static $pb.PbList<GetPublicKeyRequest> createRepeated() => $pb.PbList<GetPublicKeyRequest>();
@$core.pragma('dart2js:noInline')
static GetPublicKeyRequest getDefault() => _defaultInstance ??= $pb.GeneratedMessage.$_defaultFor<GetPublicKeyRequest>(create);
static GetPublicKeyRequest? _defaultInstance;

@$pb.TagNumber(1)
$core.String get address => $_getSZ(0);
@$pb.TagNumber(1)
set address($core.String v) { $_setString(0, v); }
@$pb.TagNumber(1)
$core.bool hasAddress() => $_has(0);
@$pb.TagNumber(1)
void clearAddress() => clearField(1);
}

class GetPublicKeyResponse extends $pb.GeneratedMessage {
static final $pb.BuilderInfo _i = $pb.BuilderInfo(const $core.bool.fromEnvironment('protobuf.omit_message_names') ? '' : 'GetPublicKeyResponse', package: const $pb.PackageName(const $core.bool.fromEnvironment('protobuf.omit_message_names') ? '' : 'pactus'), createEmptyInstance: create)
..aOS(1, const $core.bool.fromEnvironment('protobuf.omit_field_names') ? '' : 'publicKey')
..hasRequiredFields = false
;

GetPublicKeyResponse._() : super();
factory GetPublicKeyResponse({
$core.String? publicKey,
}) {
final _result = create();
if (publicKey != null) {
_result.publicKey = publicKey;
}
return _result;
}
factory GetPublicKeyResponse.fromBuffer($core.List<$core.int> i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromBuffer(i, r);
factory GetPublicKeyResponse.fromJson($core.String i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromJson(i, r);
@$core.Deprecated(
'Using this can add significant overhead to your binary. '
'Use [GeneratedMessageGenericExtensions.deepCopy] instead. '
'Will be removed in next major version')
GetPublicKeyResponse clone() => GetPublicKeyResponse()..mergeFromMessage(this);
@$core.Deprecated(
'Using this can add significant overhead to your binary. '
'Use [GeneratedMessageGenericExtensions.rebuild] instead. '
'Will be removed in next major version')
GetPublicKeyResponse copyWith(void Function(GetPublicKeyResponse) updates) => super.copyWith((message) => updates(message as GetPublicKeyResponse)) as GetPublicKeyResponse; // ignore: deprecated_member_use
$pb.BuilderInfo get info_ => _i;
@$core.pragma('dart2js:noInline')
static GetPublicKeyResponse create() => GetPublicKeyResponse._();
GetPublicKeyResponse createEmptyInstance() => create();
static $pb.PbList<GetPublicKeyResponse> createRepeated() => $pb.PbList<GetPublicKeyResponse>();
@$core.pragma('dart2js:noInline')
static GetPublicKeyResponse getDefault() => _defaultInstance ??= $pb.GeneratedMessage.$_defaultFor<GetPublicKeyResponse>(create);
static GetPublicKeyResponse? _defaultInstance;

@$pb.TagNumber(1)
$core.String get publicKey => $_getSZ(0);
@$pb.TagNumber(1)
set publicKey($core.String v) { $_setString(0, v); }
@$pb.TagNumber(1)
$core.bool hasPublicKey() => $_has(0);
@$pb.TagNumber(1)
void clearPublicKey() => clearField(1);
}

class GetBlockRequest extends $pb.GeneratedMessage {
static final $pb.BuilderInfo _i = $pb.BuilderInfo(const $core.bool.fromEnvironment('protobuf.omit_message_names') ? '' : 'GetBlockRequest', package: const $pb.PackageName(const $core.bool.fromEnvironment('protobuf.omit_message_names') ? '' : 'pactus'), createEmptyInstance: create)
..a<$core.int>(1, const $core.bool.fromEnvironment('protobuf.omit_field_names') ? '' : 'height', $pb.PbFieldType.OU3)
Expand Down Expand Up @@ -1610,5 +1704,9 @@ class BlockchainApi {
var emptyResponse = GetValidatorAddressesResponse();
return _client.invoke<GetValidatorAddressesResponse>(ctx, 'Blockchain', 'GetValidatorAddresses', request, emptyResponse);
}
$async.Future<GetPublicKeyResponse> getPublicKey($pb.ClientContext? ctx, GetPublicKeyRequest request) {
var emptyResponse = GetPublicKeyResponse();
return _client.invoke<GetPublicKeyResponse>(ctx, 'Blockchain', 'GetPublicKey', request, emptyResponse);
}
}

25 changes: 24 additions & 1 deletion www/grpc/gen/dart/blockchain.pbjson.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,26 @@ const GetValidatorResponse$json = const {

/// Descriptor for `GetValidatorResponse`. Decode as a `google.protobuf.DescriptorProto`.
final $typed_data.Uint8List getValidatorResponseDescriptor = $convert.base64Decode('ChRHZXRWYWxpZGF0b3JSZXNwb25zZRIzCgl2YWxpZGF0b3IYASABKAsyFS5wYWN0dXMuVmFsaWRhdG9ySW5mb1IJdmFsaWRhdG9y');
@$core.Deprecated('Use getPublicKeyRequestDescriptor instead')
const GetPublicKeyRequest$json = const {
'1': 'GetPublicKeyRequest',
'2': const [
const {'1': 'address', '3': 1, '4': 1, '5': 9, '10': 'address'},
],
};

/// Descriptor for `GetPublicKeyRequest`. Decode as a `google.protobuf.DescriptorProto`.
final $typed_data.Uint8List getPublicKeyRequestDescriptor = $convert.base64Decode('ChNHZXRQdWJsaWNLZXlSZXF1ZXN0EhgKB2FkZHJlc3MYASABKAlSB2FkZHJlc3M=');
@$core.Deprecated('Use getPublicKeyResponseDescriptor instead')
const GetPublicKeyResponse$json = const {
'1': 'GetPublicKeyResponse',
'2': const [
const {'1': 'public_key', '3': 1, '4': 1, '5': 9, '10': 'publicKey'},
],
};

/// Descriptor for `GetPublicKeyResponse`. Decode as a `google.protobuf.DescriptorProto`.
final $typed_data.Uint8List getPublicKeyResponseDescriptor = $convert.base64Decode('ChRHZXRQdWJsaWNLZXlSZXNwb25zZRIdCgpwdWJsaWNfa2V5GAEgASgJUglwdWJsaWNLZXk=');
@$core.Deprecated('Use getBlockRequestDescriptor instead')
const GetBlockRequest$json = const {
'1': 'GetBlockRequest',
Expand Down Expand Up @@ -308,6 +328,7 @@ const $core.Map<$core.String, $core.dynamic> BlockchainServiceBase$json = const
const {'1': 'GetValidator', '2': '.pactus.GetValidatorRequest', '3': '.pactus.GetValidatorResponse'},
const {'1': 'GetValidatorByNumber', '2': '.pactus.GetValidatorByNumberRequest', '3': '.pactus.GetValidatorResponse'},
const {'1': 'GetValidatorAddresses', '2': '.pactus.GetValidatorAddressesRequest', '3': '.pactus.GetValidatorAddressesResponse'},
const {'1': 'GetPublicKey', '2': '.pactus.GetPublicKeyRequest', '3': '.pactus.GetPublicKeyResponse'},
],
};

Expand Down Expand Up @@ -342,7 +363,9 @@ const $core.Map<$core.String, $core.Map<$core.String, $core.dynamic>> Blockchain
'.pactus.GetValidatorByNumberRequest': GetValidatorByNumberRequest$json,
'.pactus.GetValidatorAddressesRequest': GetValidatorAddressesRequest$json,
'.pactus.GetValidatorAddressesResponse': GetValidatorAddressesResponse$json,
'.pactus.GetPublicKeyRequest': GetPublicKeyRequest$json,
'.pactus.GetPublicKeyResponse': GetPublicKeyResponse$json,
};

/// Descriptor for `Blockchain`. Decode as a `google.protobuf.ServiceDescriptorProto`.
final $typed_data.Uint8List blockchainServiceDescriptor = $convert.base64Decode('CgpCbG9ja2NoYWluEj0KCEdldEJsb2NrEhcucGFjdHVzLkdldEJsb2NrUmVxdWVzdBoYLnBhY3R1cy5HZXRCbG9ja1Jlc3BvbnNlEkkKDEdldEJsb2NrSGFzaBIbLnBhY3R1cy5HZXRCbG9ja0hhc2hSZXF1ZXN0GhwucGFjdHVzLkdldEJsb2NrSGFzaFJlc3BvbnNlEk8KDkdldEJsb2NrSGVpZ2h0Eh0ucGFjdHVzLkdldEJsb2NrSGVpZ2h0UmVxdWVzdBoeLnBhY3R1cy5HZXRCbG9ja0hlaWdodFJlc3BvbnNlElgKEUdldEJsb2NrY2hhaW5JbmZvEiAucGFjdHVzLkdldEJsb2NrY2hhaW5JbmZvUmVxdWVzdBohLnBhY3R1cy5HZXRCbG9ja2NoYWluSW5mb1Jlc3BvbnNlElUKEEdldENvbnNlbnN1c0luZm8SHy5wYWN0dXMuR2V0Q29uc2Vuc3VzSW5mb1JlcXVlc3QaIC5wYWN0dXMuR2V0Q29uc2Vuc3VzSW5mb1Jlc3BvbnNlEkMKCkdldEFjY291bnQSGS5wYWN0dXMuR2V0QWNjb3VudFJlcXVlc3QaGi5wYWN0dXMuR2V0QWNjb3VudFJlc3BvbnNlEkk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nBhY3R1cy5HZXRWYWxpZGF0b3JSZXF1ZXN0GhwucGFjdHVzLkdldFZhbGlkYXRvclJlc3BvbnNlElkKFEdldFZhbGlkYXRvckJ5TnVtYmVyEiMucGFjdHVzLkdldFZhbGlkYXRvckJ5TnVtYmVyUmVxdWVzdBocLnBhY3R1cy5HZXRWYWxpZGF0b3JSZXNwb25zZRJkChVHZXRWYWxpZGF0b3JBZGRyZXNzZXMSJC5wYWN0dXMuR2V0VmFsaWRhdG9yQWRkcmVzc2VzUmVxdWVzdBolLnBhY3R1cy5HZXRWYWxpZGF0b3JBZGRyZXNzZXNSZXNwb25zZRJJCgxHZXRQdWJsaWNLZXkSGy5wYWN0dXMuR2V0UHVibGljS2V5UmVxdWVzdBocLnBhY3R1cy5HZXRQdWJsaWNLZXlSZXNwb25zZQ==');
3 changes: 3 additions & 0 deletions www/grpc/gen/dart/blockchain.pbserver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ abstract class BlockchainServiceBase extends $pb.GeneratedService {
$async.Future<$1.GetValidatorResponse> getValidator($pb.ServerContext ctx, $1.GetValidatorRequest request);
$async.Future<$1.GetValidatorResponse> getValidatorByNumber($pb.ServerContext ctx, $1.GetValidatorByNumberRequest request);
$async.Future<$1.GetValidatorAddressesResponse> getValidatorAddresses($pb.ServerContext ctx, $1.GetValidatorAddressesRequest request);
$async.Future<$1.GetPublicKeyResponse> getPublicKey($pb.ServerContext ctx, $1.GetPublicKeyRequest request);

$pb.GeneratedMessage createRequest($core.String method) {
switch (method) {
Expand All @@ -37,6 +38,7 @@ abstract class BlockchainServiceBase extends $pb.GeneratedService {
case 'GetValidator': return $1.GetValidatorRequest();
case 'GetValidatorByNumber': return $1.GetValidatorByNumberRequest();
case 'GetValidatorAddresses': return $1.GetValidatorAddressesRequest();
case 'GetPublicKey': return $1.GetPublicKeyRequest();
default: throw $core.ArgumentError('Unknown method: $method');
}
}
Expand All @@ -52,6 +54,7 @@ abstract class BlockchainServiceBase extends $pb.GeneratedService {
case 'GetValidator': return this.getValidator(ctx, request as $1.GetValidatorRequest);
case 'GetValidatorByNumber': return this.getValidatorByNumber(ctx, request as $1.GetValidatorByNumberRequest);
case 'GetValidatorAddresses': return this.getValidatorAddresses(ctx, request as $1.GetValidatorAddressesRequest);
case 'GetPublicKey': return this.getPublicKey(ctx, request as $1.GetPublicKeyRequest);
default: throw $core.ArgumentError('Unknown method: $method');
}
}
Expand Down
Loading
Loading