UNPKG

3.91 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5var __importStar = (this && this.__importStar) || function (mod) {
6 if (mod && mod.__esModule) return mod;
7 var result = {};
8 if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
9 result["default"] = mod;
10 return result;
11};
12Object.defineProperty(exports, "__esModule", { value: true });
13var ripemd160_1 = __importDefault(require("ripemd160"));
14var crypto_utils_1 = require("./crypto-utils");
15var pb = __importStar(require("./proto/loom_pb"));
16var LocalAddress = /** @class */ (function () {
17 function LocalAddress(bytes) {
18 this.bytes = bytes;
19 }
20 LocalAddress.prototype.isEmpty = function () {
21 return this.bytes && this.bytes.length === 0;
22 };
23 LocalAddress.prototype.toString = function () {
24 // TODO: checksum encoding like go-loom
25 return ('0x' +
26 Buffer.from(this.bytes.buffer, this.bytes.byteOffset, this.bytes.byteLength).toString('hex'));
27 };
28 LocalAddress.prototype.equals = function (other) {
29 // Node API docs say parameters can be Buffer | Uint8Array... so shush TypeScript
30 return Buffer.compare(this.bytes, other.bytes) === 0;
31 };
32 LocalAddress.fromHexString = function (hexAddr) {
33 if (!hexAddr.startsWith('0x')) {
34 throw new Error('hexAddr argument has no 0x prefix');
35 }
36 var bytes = Buffer.from(hexAddr.slice(2), 'hex');
37 if (bytes.length !== 20) {
38 throw new Error("Invalid local address, expected 20 bytes, got " + bytes.length);
39 }
40 return new LocalAddress(bytes);
41 };
42 /**
43 * Converts a public key to a local address (which is used as unique identifier within a DAppChain).
44 * @param publicKey 32-byte public key.
45 * @returns Array of bytes representing a local address.
46 */
47 LocalAddress.fromPublicKey = function (publicKey) {
48 if (publicKey.length !== 32) {
49 throw new Error("Invalid public key, expected 32 bytes, go " + publicKey.length);
50 }
51 var hasher = new ripemd160_1.default();
52 hasher.update(Buffer.from(publicKey.buffer, publicKey.byteOffset, publicKey.byteLength));
53 return new LocalAddress(hasher.digest());
54 };
55 return LocalAddress;
56}());
57exports.LocalAddress = LocalAddress;
58var Address = /** @class */ (function () {
59 function Address(chainId, local) {
60 this.chainId = chainId;
61 this.local = local;
62 }
63 Address.prototype.isEmpty = function () {
64 return this.chainId === '' && this.local.isEmpty();
65 };
66 Address.prototype.toString = function () {
67 return this.chainId + ":" + this.local.toString();
68 };
69 Address.prototype.MarshalPB = function () {
70 var addr = new pb.Address();
71 addr.setChainId(this.chainId);
72 addr.setLocal(crypto_utils_1.bufferToProtobufBytes(this.local.bytes));
73 return addr;
74 };
75 Address.prototype.equals = function (other) {
76 return this.chainId === other.chainId && this.local.equals(other.local);
77 };
78 Address.UmarshalPB = function (pb) {
79 return new Address(pb.getChainId(), new LocalAddress(pb.getLocal_asU8()));
80 };
81 /**
82 * Converts a string to an address.
83 * @param address String representation of an address, in the format "chain:0x...".
84 */
85 Address.fromString = function (address) {
86 var parts = address.split(':');
87 if (parts.length !== 2) {
88 throw new Error('Invalid address string');
89 }
90 return new Address(parts[0], LocalAddress.fromHexString(parts[1]));
91 };
92 return Address;
93}());
94exports.Address = Address;
95//# sourceMappingURL=address.js.map
\No newline at end of file