def create_URI(addr, amount, message): import parkbyte if not parkbyte.is_address(addr): return "" query = [] if amount: query.append('amount=%s'%format_satoshis_plain(amount)) if message: if type(message) == unicode: message = message.encode('utf8') query.append('message=%s'%urllib.quote(message)) p = urlparse.ParseResult(scheme='parkbyte', netloc='', path=addr, params='', query='&'.join(query), fragment='') return urlparse.urlunparse(p)
def resolve(self, k): if parkbyte.is_address(k): return { 'address': k, 'type': 'address' } if k in self.keys(): _type, addr = self[k] if _type == 'address': return { 'address': addr, 'type': 'contact' } out = self.resolve_openalias(k) if out: address, name, validated = out return { 'address': address, 'name': name, 'type': 'openalias', 'validated': validated } raise Exception("Invalid ParkByte address or alias", k)
def parse_URI(uri, on_pr=None): import parkbyte from parkbyte import COIN if ':' not in uri: if not parkbyte.is_address(uri): raise BaseException("Not a parkbyte address") return {'address': uri} u = urlparse.urlparse(uri) if u.scheme != 'parkbyte': raise BaseException("Not a parkbyte URI") address = u.path # python for android fails to parse query if address.find('?') > 0: address, query = u.path.split('?') pq = urlparse.parse_qs(query) else: pq = urlparse.parse_qs(u.query) for k, v in pq.items(): if len(v)!=1: raise Exception('Duplicate Key', k) out = {k: v[0] for k, v in pq.items()} if address: if not parkbyte.is_address(address): raise BaseException("Invalid parkbyte address:" + address) out['address'] = address if 'amount' in out: am = out['amount'] m = re.match('([0-9\.]+)X([0-9])', am) if m: k = int(m.group(2)) - 8 amount = Decimal(m.group(1)) * pow( Decimal(10) , k) else: amount = Decimal(am) * COIN out['amount'] = int(amount) if 'message' in out: out['message'] = out['message'].decode('utf8') out['memo'] = out['message'] if 'time' in out: out['time'] = int(out['time']) if 'exp' in out: out['exp'] = int(out['exp']) if 'sig' in out: out['sig'] = parkbyte.base_decode(out['sig'], None, base=58).encode('hex') r = out.get('r') sig = out.get('sig') name = out.get('name') if r or (name and sig): def get_payment_request_thread(): import paymentrequest as pr if name and sig: s = pr.serialize_request(out).SerializeToString() request = pr.PaymentRequest(s) else: request = pr.get_payment_request(r) on_pr(request) t = threading.Thread(target=get_payment_request_thread) t.setDaemon(True) t.start() return out
def validateaddress(self, address): """Check that an address is valid. """ return is_address(address)