예제 #1
0
class Input(ormin.Model):
    in_pos = ormin.IntField()

    coinbase = ormin.StringField(required=False)
    txid = ormin.StringField(required=False)
    vout = ormin.IntField(required=False)
    scriptsig_asm = ormin.TextField(required=False)
    scriptsig_hex = ormin.TextField(required=False)
    scriptwitness = ormin.BlobField(required=False)
    pegin_witness = ormin.BlobField(required=False)
    sequence = ormin.BigIntField(required=False)

    def __init__(self, json_dict=None, *args, **kwargs):

        if 'scriptWitness' in json_dict:
            json_dict['scriptwitness'] = json.dumps(json_dict['scriptWitness'])

        if 'pegin_witness' in json_dict:
            json_dict['pegin_witness'] = json_dict['pegin_witness']

        if 'scriptSig' in json_dict:
            json_dict['scriptsig_asm'] = json_dict['scriptSig']['asm']
            json_dict['scriptsig_hex'] = json_dict['scriptSig']['hex']

        super(Input, self).__init__(json_dict=json_dict, *args, **kwargs)

    def new_id(self):
        self.id = "%s_%s" % (self.tx_id, self.in_pos)
예제 #2
0
class Chaininfo(RpcCachedModel):
    bestblockhash = ormin.StringField()
    blocks = ormin.IntField()
    mediantime = ormin.IntField()
    cached_blocks = ormin.IntField()
    caching_first = ormin.IntField()
    caching_blockhash = ormin.StringField()
    caching_last = ormin.IntField()
    signblock_asm = ormin.StringField(required=False)
    signblock_hex = ormin.StringField(required=False)

    @classmethod
    def truth_src_get(cls, req_id):
        json_result = super(Chaininfo,
                            cls)._rpccaller.RpcCall('getblockchaininfo', {})
        if 'error' in json_result:
            return json_result

        chaininfo = cls(json_dict=json_result)
        chaininfo.id = req_id
        chaininfo.start_caching_progress()
        chaininfo.save()
        return chaininfo

    def clean_caching_progress_fields(self):
        self.caching_first = -1
        self.caching_blockhash = ''
        self.caching_last = -1

    def start_caching_progress(self):
        self.cached_blocks = -1
        self.clean_caching_progress_fields()
예제 #3
0
class Register(User):
    password = ormin.StringField()
    password2 = ormin.StringField()

    @staticmethod
    def get_name():
        return 'user'

    def validate(self):
        if super(Register, self).validate():
            return self.errors

        if self.password != self.password2:
            self.errors['password'] = "******"

        return self.errors

    def insert(self):
        user = User(self.json(False))
        user.hash_password(self.password)
        return user.insert()
예제 #4
0
class UpdateUser(User):
    password = ormin.StringField()
    new_password = ormin.StringField(required=False)

    def validate(self):

        user = User.search({'username': self.username})
        if not user.verify_password(self.password):
            self.errors['password'] = '******'
            return self.errors

        # Prevent unique validations from raising
        self.username = '******'
        if user.email == self.email:
            self.email = '_dummy'

        super(UpdateUser, self).validate()
        return self.errors

    def update(self):
        user = User(self.json())
        if self.new_password:
            user.hash_password(self.new_password)
        return user.update()
예제 #5
0
class User(ormin.Model):
    username = ormin.StringField(unique=True)
    email = ormin.StringField(unique=True)
    password_hash = ormin.StringField(required=False)

    def new_id(self):
        self.id = self.username

    def hash_password(self, password):
        self.password_hash = pwd_context.encrypt(password)

    def verify_password(self, password):
        return pwd_context.verify(password, self.password_hash)

    def generate_token(self):
        s = Serializer(SECRET_KEY, expires_in=600)
        return s.dumps({'id': self.id})

    @staticmethod
    def verify_token(token):
        s = Serializer(SECRET_KEY)
        try:
            data = s.loads(token)
        except SignatureExpired:
            return None  # valid token, but expired
        except BadSignature:
            return None  # invalid token
        return User.get(data['id'])

    def json(self, full=False):
        json = super(User, self).json(full)
        if full:
            json['token'] = self.generate_token()
            del json['password_hash']
            del json['email']
        return json
예제 #6
0
class Tx(RpcCachedModel):
    blockhash = ormin.StringField(index=True)
    time = ormin.IntField(required=False)
    hex = ormin.TextField()
    size = ormin.IntField()
    vsize = ormin.IntField()
    version = ormin.IntField()
    locktime = ormin.BigIntField()

    vin = ormin.OneToManyField(Input, 'tx_id', cascade=True)
    vout = ormin.OneToManyField(Output, 'tx_id', cascade=True)

    @classmethod
    def truth_src_get(cls, req_id):
        json_result = super(Tx,
                            cls)._rpccaller.RpcCall('getrawtransaction', {
                                'txid': req_id,
                                'verbose': 1
                            })
        if 'error' in json_result:
            return json_result

        # TODO Return this from the daemons?
        # Even if the numbers were wrong due to json non-specified "magic",
        # we should be fine since this is only needed for guaranteeing
        # a unique id for inputs
        for it in xrange(len(json_result['vin'])):
            json_result['vin'][it]['in_pos'] = it

        tx = Tx(json_dict=json_result)
        tx.id = req_id

        # Don't cache mempool txs
        if 'blockhash' in json_result and json_result['blockhash']:
            tx.save()
        return tx
예제 #7
0
class Block(RpcCachedModel):
    height = ormin.IntField(index=True, unique=True)
    previousblockhash = ormin.StringField(index=True, required=False)

    # For chains with pow (optional)
    bits = ormin.StringField(required=False)
    chainwork = ormin.StringField(required=False)
    difficulty = ormin.BigIntField(required=False)
    nonce = ormin.BigIntField(required=False)

    # For chains with signed blocks (optional)
    signblock_witness_asm = ormin.StringField(required=False)
    signblock_witness_hex = ormin.StringField(required=False)

    mediantime = ormin.IntField()
    merkleroot = ormin.StringField()
    size = ormin.IntField()
    # strippedsize = ormin.IntField()
    time = ormin.IntField()
    # version = ormin.IntField()
    # versionhex = ormin.StringField()
    weight = ormin.IntField()

    tx = ormin.BlobField()

    @classmethod
    def truth_src_get(cls, req_id):
        json_result = super(Block,
                            cls)._rpccaller.RpcCall('getblock',
                                                    {'blockhash': req_id})
        if 'error' in json_result:
            return json_result

        block = Block(json_dict=json_result)
        block.id = req_id
        block.save()
        return block
예제 #8
0
class Chaininfo(ormin.Model):
    bestblockhash = ormin.StringField()
    blocks = ormin.IntField()
    mediantime = ormin.IntField()
예제 #9
0
class Tx(ormin.Model):
    blockhash = ormin.StringField(index=True)
    blob = ormin.TextField()
예제 #10
0
class LoginForm(ormin.Form):
    username = ormin.StringField()
    password = ormin.StringField()
예제 #11
0
class Output(ormin.Model):
    n = ormin.IntField()

    value = ormin.StringField(required=False)
    value_minimum = ormin.StringField(required=False)
    value_maximum = ormin.StringField(required=False)
    ct_exponent = ormin.StringField(required=False)
    ct_bits = ormin.StringField(required=False)

    asset = ormin.StringField(required=False)
    assetcommitment = ormin.StringField(required=False)
    amountcommitment = ormin.StringField(required=False)

    scriptpubkey_asm = ormin.TextField()
    scriptpubkey_hex = ormin.TextField()
    scriptpubkey_type = ormin.StringField()
    scriptpubkey_reqsigs = ormin.IntField(required=False)
    scriptpubkey_addresses = ormin.BlobField(required=False)

    pegout_chain = ormin.StringField(required=False)
    pegout_scriptpubkey_asm = ormin.TextField(required=False)
    pegout_scriptpubkey_hex = ormin.TextField(required=False)
    pegout_scriptpubkey_type = ormin.StringField(required=False)
    pegout_scriptpubkey_reqsigs = ormin.IntField(required=False)
    pegout_scriptpubkey_addresses = ormin.BlobField(required=False)

    def __init__(self, json_dict=None, *args, **kwargs):

        if 'value-minimum' in json_dict:
            json_dict['value_minimum'] = json_dict['value-minimum']

        if 'value-maximum' in json_dict:
            json_dict['value_maximum'] = json_dict['value-maximum']

        if 'ct-exponent' in json_dict:
            json_dict['ct_exponent'] = json_dict['ct-exponent']

        if 'ct-bits' in json_dict:
            json_dict['ct_bits'] = json_dict['ct-bits']

        if 'scriptPubKey' in json_dict:
            json_dict['scriptpubkey_asm'] = json_dict['scriptPubKey']['asm']
            json_dict['scriptpubkey_hex'] = json_dict['scriptPubKey']['hex']
            json_dict['scriptpubkey_type'] = json_dict['scriptPubKey']['type']
            if 'reqSigs' in json_dict['scriptPubKey']:
                json_dict['scriptpubkey_reqsigs'] = json_dict['scriptPubKey'][
                    'reqSigs']
            if 'addresses' in json_dict['scriptPubKey']:
                json_dict['scriptpubkey_addresses'] = json.dumps(
                    json_dict['scriptPubKey']['addresses'])

            if 'pegout_asm' in json_dict['scriptPubKey']:
                json_dict['pegout_scriptpubkey_asm'] = json_dict[
                    'scriptPubKey']['pegout_asm']
            if 'pegout_hex' in json_dict['scriptPubKey']:
                json_dict['pegout_scriptpubkey_hex'] = json_dict[
                    'scriptPubKey']['pegout_hex']
            if 'pegout_type' in json_dict['scriptPubKey']:
                json_dict['pegout_scriptpubkey_type'] = json_dict[
                    'scriptPubKey']['pegout_type']
            if 'pegout_reqSigs' in json_dict['scriptPubKey']:
                json_dict['pegout_scriptpubkey_reqsigs'] = json_dict[
                    'scriptPubKey']['pegout_reqSigs']
            if 'pegout_addresses' in json_dict['scriptPubKey']:
                json_dict['pegout_scriptpubkey_addresses'] = json.dumps(
                    json_dict['scriptPubKey']['pegout_addresses'])

        super(Output, self).__init__(json_dict=json_dict, *args, **kwargs)

    def new_id(self):
        self.id = "%s_%s" % (self.tx_id, self.n)