Skip to content

Commit

Permalink
INDY-1074: add ATTRIB hash and enc support, fix and unskip tests. (hy…
Browse files Browse the repository at this point in the history
…perledger#548)

* INDY-1074: add ATTRIB hash and enc support, fix and unskip tests.

Signed-off-by: Sergey Shilov <[email protected]>

* Fix GET_ATTR processing.

Signed-off-by: Sergey Shilov <[email protected]>

* Fix flake8 errors.

Signed-off-by: Sergey Shilov <[email protected]>

* INDY-1074: implement get_attr command for enc and hash (client and server sides).

Signed-off-by: Sergey Shilov <[email protected]>

* Fix and add tests for GET_ATTR msg server-side validation (hash and enc).

Signed-off-by: Sergey Shilov <[email protected]>

* Some logical re-factoring for parse_attr_txn().

Signed-off-by: Sergey Shilov <[email protected]>
  • Loading branch information
sergey-shilov authored and ashcherbakov committed Feb 2, 2018
1 parent dbe231d commit 5099cc3
Show file tree
Hide file tree
Showing 15 changed files with 293 additions and 116 deletions.
34 changes: 16 additions & 18 deletions indy_client/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
from plenum.cli.constants import PROMPT_ENV_SEPARATOR, NO_ENV
from plenum.cli.helper import getClientGrams
from plenum.cli.phrase_word_completer import PhraseWordCompleter
from plenum.common.constants import NAME, VERSION, VERKEY, DATA, TXN_ID, FORCE
from plenum.common.constants import NAME, VERSION, VERKEY, DATA, TXN_ID, FORCE, RAW
from plenum.common.exceptions import OperationError
from plenum.common.signer_did import DidSigner
from plenum.common.txn_util import createGenesisTxnFile
Expand Down Expand Up @@ -672,11 +672,15 @@ def _getAttr(self, nym, raw, enc, hsh):

def getAttrReply(reply, err, *args):
if reply and reply[DATA]:
data = json.loads(reply[DATA])
if data:
self.print(
"Found attribute {}"
.format(json.dumps(data)))
data_to_print = None
if RAW in reply:
data = json.loads(reply[DATA])
if data:
data_to_print = json.dumps(data)
else:
data_to_print = reply[DATA]
if data_to_print:
self.print("Found attribute {}".format(data_to_print))
else:
self.print("Attr not found")

Expand Down Expand Up @@ -843,12 +847,9 @@ def _sendAttribAction(self, matchedVars):
if not self.canMakeIndyRequest:
return True
nym = matchedVars.get('dest_id')
raw = matchedVars.get('raw') \
if matchedVars.get('raw') else None
enc = ast.literal_eval(matchedVars.get('enc')) \
if matchedVars.get('enc') else None
hsh = matchedVars.get('hash') \
if matchedVars.get('hash') else None
raw = matchedVars.get('raw', None)
enc = matchedVars.get('enc', None)
hsh = matchedVars.get('hash', None)
self._addAttribToNym(nym, raw, enc, hsh)
return True

Expand All @@ -859,12 +860,9 @@ def _sendGetAttrAction(self, matchedVars):
if not self.canMakeIndyRequest:
return True
nym = matchedVars.get('dest_id')
raw = matchedVars.get('raw') \
if matchedVars.get('raw') else None
enc = ast.literal_eval(matchedVars.get('enc')) \
if matchedVars.get('enc') else None
hsh = matchedVars.get('hash') \
if matchedVars.get('hash') else None
raw = matchedVars.get('raw', None)
enc = matchedVars.get('enc', None)
hsh = matchedVars.get('hash', None)
self._getAttr(nym, raw, enc, hsh)
return True

Expand Down
4 changes: 2 additions & 2 deletions indy_client/cli/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@
sendAttribCmd = Command(
id="send {attrib}".format(attrib=attribName),
title="Adds attributes to existing DID",
usage="send {attrib} dest=<target DID> [raw={{<json-data>}}] [hash=<hashed-data>] [enc: <encrypted-data>]".format(
usage="send {attrib} dest=<target DID> [raw={{<json-data>}}] [hash=<hashed-data>] [enc=<encrypted-data>]".format(
attrib=attribName),
examples='send {attrib} dest=33A18XMqWqTzDpLHXLR5nT raw={{"endpoint": "127.0.0.1:5555"}}'.format(attrib=attribName))

sendGetAttrCmd = Command(
id="send {getAttr}".format(
getAttr=getAttrName),
title="Get ATTR from indy",
usage="send {getAttr} dest=<target DID> raw=<name>".format(
usage="send {getAttr} dest=<target DID> [raw=<name>] [hash=<hashed-data>] [enc=<encrypted-data>]".format(
getAttr=getAttrName),
examples="send {getAttr} dest=33A18XMqWqTzDpLHXLR5nT raw=endpoint".format(
getAttr=getAttrName))
Expand Down
4 changes: 2 additions & 2 deletions indy_client/cli/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@
GET_ATTR_REG_EX = \
"(\s* (?P<send_get_attr>send\s+{attrib}) " \
"\s+ dest=\s*(?P<dest_id>[A-Za-z0-9+=/]+) " \
"\s+ raw=(?P<raw>[A-Za-z0-9+=/]+) \s*) ".format(
"\s+ ((raw=(?P<raw>[A-Za-z0-9+=/]+))|(hash=(?P<hash>[A-Fa-f0-9]+))|(enc=(?P<enc>[A-Za-z0-9+=/]+)) \s*) \s*) ".format(
attrib=IndyTransactions.GET_ATTR.name)

ADD_ATTRIB_REG_EX = \
"(\s* (?P<send_attrib>send\s+{attrib}) " \
"\s+ dest=\s*(?P<dest_id>[A-Za-z0-9+=/]+) " \
"\s+ raw=(?P<raw>\{{\s*.*\}}) \s*) ".format(
"\s+ ((raw=(?P<raw>\{{\s*.*\}}))|(hash=(?P<hash>[A-Fa-f0-9]+))|(enc=(?P<enc>[A-Za-z0-9+=/]+))) \s*) ".format(
attrib=IndyTransactions.ATTRIB.name)

SEND_SCHEMA_REG_EX = "(\s*(?P<send_schema>send\s+{schema})" \
Expand Down
2 changes: 1 addition & 1 deletion indy_client/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def prepare_for_state(self, result):
if request_type == GET_NYM:
return domain.prepare_get_nym_for_state(result)
if request_type == GET_ATTR:
path, value, hashed_value, value_bytes = \
attr_type, path, value, hashed_value, value_bytes = \
domain.prepare_get_attr_for_state(result)
return path, value_bytes
if request_type == GET_CLAIM_DEF:
Expand Down
26 changes: 15 additions & 11 deletions indy_client/client/wallet/attribute.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from enum import unique, IntEnum
from typing import Optional, TypeVar

from plenum.common.constants import TXN_TYPE, TARGET_NYM, RAW, ORIGIN, CURRENT_PROTOCOL_VERSION
from plenum.common.constants import TXN_TYPE, TARGET_NYM, RAW, ENC, HASH, ORIGIN, CURRENT_PROTOCOL_VERSION
from indy_common.generates_request import GeneratesRequest
from indy_common.constants import ATTRIB, GET_ATTR
from indy_common.types import Request
Expand Down Expand Up @@ -62,23 +62,27 @@ def __init__(self,
self.encKey = encKey
self.seqNo = seqNo

def _op(self):
op = {
TXN_TYPE: ATTRIB
}
if self.dest:
op[TARGET_NYM] = self.dest
def _op_fill_attr_type_and_data(self, op, data):
if self.ledgerStore == LedgerStore.RAW:
op[RAW] = self.value
op[RAW] = data
elif self.ledgerStore == LedgerStore.ENC:
raise NotImplementedError
op[ENC] = data
elif self.ledgerStore == LedgerStore.HASH:
raise NotImplementedError
op[HASH] = data
elif self.ledgerStore == LedgerStore.DONT:
raise RuntimeError("This attribute cannot be stored externally")
else:
raise RuntimeError("Unknown ledgerStore: {}".
format(self.ledgerStore))

def _op(self):
op = {
TXN_TYPE: ATTRIB
}
if self.dest:
op[TARGET_NYM] = self.dest
self._op_fill_attr_type_and_data(op, self.value)

return op

def ledgerRequest(self):
Expand All @@ -92,8 +96,8 @@ def _opForGet(self):
op = {
TARGET_NYM: self.dest,
TXN_TYPE: GET_ATTR,
RAW: self.name
}
self._op_fill_attr_type_and_data(op, self.name)
if self.origin:
op[ORIGIN] = self.origin
return op
Expand Down
9 changes: 4 additions & 5 deletions indy_client/client/wallet/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,17 +408,16 @@ def getConnectionNames(self):
def build_attrib(self, nym, raw=None, enc=None, hsh=None):
assert int(bool(raw)) + int(bool(enc)) + int(bool(hsh)) == 1
if raw:
# l = LedgerStore.RAW
store = LedgerStore.RAW
data = raw
elif enc:
# l = LedgerStore.ENC
store = LedgerStore.ENC
data = enc
elif hsh:
# l = LedgerStore.HASH
store = LedgerStore.HASH
data = hsh
else:
raise RuntimeError('One of raw, enc, or hash are required.')

# TODO looks like a possible error why we do not use `l` (see above)?
return Attribute(randomString(5), data, self.defaultId,
dest=nym, ledgerStore=LedgerStore.RAW)
dest=nym, ledgerStore=store)
29 changes: 12 additions & 17 deletions indy_client/test/cli/test_send_attrib_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,6 @@ def testSendAttribFailsIfRawContainsNoAttrs(
mapper=parameters, expect=ERROR, within=2)


@pytest.mark.skip(reason='INDY-71')
def testSendAttribFailsIfRawIsBrokenJson(
be, do, poolNodesStarted, trusteeCli):

Expand All @@ -743,11 +742,15 @@ def testSendAttribFailsIfRawIsBrokenJson(
}

be(trusteeCli)
do('send ATTRIB dest={dest} raw={raw}',
mapper=parameters, expect=INVALID_SYNTAX, within=2)

brokenJson = validJson.replace(':', '-')
parameters['raw'] = brokenJson
do('send ATTRIB dest={dest} raw={raw}',
mapper=parameters, expect=ERROR, within=2)


@pytest.mark.skip(reason='INDY-71')
def testSendAttribFailsIfRawIsHex(
be, do, poolNodesStarted, trusteeCli):

Expand All @@ -761,10 +764,9 @@ def testSendAttribFailsIfRawIsHex(

be(trusteeCli)
do('send ATTRIB dest={dest} raw={raw}',
mapper=parameters, expect=ERROR, within=2)
mapper=parameters, expect=INVALID_SYNTAX, within=2)


@pytest.mark.skip(reason='INDY-71')
def testSendAttribFailsIfRawIsHumanReadableText(
be, do, poolNodesStarted, trusteeCli):

Expand All @@ -778,10 +780,9 @@ def testSendAttribFailsIfRawIsHumanReadableText(

be(trusteeCli)
do('send ATTRIB dest={dest} raw={raw}',
mapper=parameters, expect=ERROR, within=2)
mapper=parameters, expect=INVALID_SYNTAX, within=2)


@pytest.mark.skip(reason='INDY-71')
def testSendAttribFailsIfRawIsDecimalNumber(
be, do, poolNodesStarted, trusteeCli):

Expand All @@ -795,7 +796,7 @@ def testSendAttribFailsIfRawIsDecimalNumber(

be(trusteeCli)
do('send ATTRIB dest={dest} raw={raw}',
mapper=parameters, expect=ERROR, within=2)
mapper=parameters, expect=INVALID_SYNTAX, within=2)


def testSendAttribHasInvalidSyntaxIfRawIsEmptyString(
Expand All @@ -814,7 +815,6 @@ def testSendAttribHasInvalidSyntaxIfRawIsEmptyString(
mapper=parameters, expect=INVALID_SYNTAX, within=2)


@pytest.mark.skip(reason='INDY-71')
def testSendAttribSucceedsForHexSha256Hash(
be, do, poolNodesStarted, trusteeCli):

Expand All @@ -835,7 +835,6 @@ def testSendAttribSucceedsForHexSha256Hash(
mapper=parameters, expect=ATTRIBUTE_ADDED, within=2)


@pytest.mark.skip(reason='INDY-71')
def testSendAttribSucceedsForHexHashWithLettersInBothCases(
be, do, poolNodesStarted, trusteeCli):

Expand All @@ -844,14 +843,14 @@ def testSendAttribSucceedsForHexHashWithLettersInBothCases(

parameters = {
'dest': uuidIdentifier,
'hash': '6d4a333838d0ef96756cccC680AF2531075C512502Fb68c5503c63d93de859b3'}
'hash': '6d4a333838d0ef96756cccC680AF2531075C512502Fb68c5503c63d93de859b3'
}

be(trusteeCli)
do('send ATTRIB dest={dest} hash={hash}',
mapper=parameters, expect=ATTRIBUTE_ADDED, within=2)


@pytest.mark.skip(reason='INDY-71')
def testSendAttribFailsForHashShorterThanSha256(
be, do, poolNodesStarted, trusteeCli):

Expand All @@ -868,7 +867,6 @@ def testSendAttribFailsForHashShorterThanSha256(
mapper=parameters, expect=ERROR, within=2)


@pytest.mark.skip(reason='INDY-71')
def testSendAttribFailsForHashLongerThanSha256(
be, do, poolNodesStarted, trusteeCli):

Expand All @@ -885,7 +883,6 @@ def testSendAttribFailsForHashLongerThanSha256(
mapper=parameters, expect=ERROR, within=2)


@pytest.mark.skip(reason='INDY-71')
def testSendAttribFailsForBase58Hash(
be, do, poolNodesStarted, trusteeCli):

Expand All @@ -905,10 +902,9 @@ def testSendAttribFailsForBase58Hash(

be(trusteeCli)
do('send ATTRIB dest={dest} hash={hash}',
mapper=parameters, expect=ERROR, within=2)
mapper=parameters, expect=INVALID_SYNTAX, within=2)


@pytest.mark.skip(reason='INDY-71')
def testSendAttribFailsForBase64Hash(
be, do, poolNodesStarted, trusteeCli):

Expand All @@ -928,7 +924,7 @@ def testSendAttribFailsForBase64Hash(

be(trusteeCli)
do('send ATTRIB dest={dest} hash={hash}',
mapper=parameters, expect=ERROR, within=2)
mapper=parameters, expect=INVALID_SYNTAX, within=2)


def testSendAttribHasInvalidSyntaxIfHashIsEmpty(
Expand All @@ -947,7 +943,6 @@ def testSendAttribHasInvalidSyntaxIfHashIsEmpty(
mapper=parameters, expect=INVALID_SYNTAX, within=2)


@pytest.mark.skip(reason='INDY-71')
def testSendAttribSucceedsForNonEmptyEnc(
be, do, poolNodesStarted, trusteeCli):

Expand Down
Loading

0 comments on commit 5099cc3

Please sign in to comment.