airgap-coin-lib
Version:
The airgap-coin-lib is a protocol agnostic library to prepare, sign and broadcast cryptocurrency transactions.
98 lines • 4.03 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var bs58 = require("../../../../../dependencies/src/bs58-4.0.1");
var blake2b_1 = require("../../../../../utils/blake2b");
var hex_1 = require("../../../../../utils/hex");
var SubstrateNetwork_1 = require("../../../SubstrateNetwork");
// If changed, the test address in `test/protocols/specs/kusama.ts` must be changed accordingly
var SS58Format = new Map([
[SubstrateNetwork_1.SubstrateNetwork.POLKADOT, 0],
[SubstrateNetwork_1.SubstrateNetwork.KUSAMA, 2]
]);
var SS58_PREFIX = 'SS58PRE';
/*
* An address doesn't have a fixed length. Interpretation:
* [total bytes: version bytes, payload bytes, checksum bytes]
* 3: 1, 1, 1
* 4: 1, 2, 1
* 6: 1, 4, 1
* 35: 1, 32, 2
*/
var SubstrateAddress = /** @class */ (function () {
function SubstrateAddress(version, payload, checksum) {
this.version = version;
this.payload = payload;
this.checksum = checksum;
this.encoded = null;
}
SubstrateAddress.createPlaceholder = function () {
if (!SubstrateAddress.placeholder) {
var payload = new Uint8Array(32);
payload.fill(0);
SubstrateAddress.placeholder = new SubstrateAddress(Buffer.from([0]), Buffer.from(payload), Buffer.from([0, 0]));
}
return SubstrateAddress.placeholder;
};
SubstrateAddress.from = function (accountId, network) {
if (typeof accountId === 'string' && hex_1.isHex(accountId)) {
return this.fromPublicKey(accountId, network);
}
else if (typeof accountId === 'string') {
return this.fromEncoded(accountId);
}
else {
return accountId;
}
};
SubstrateAddress.fromPublicKey = function (payload, network) {
var ss58Format = SS58Format.get(network);
return this.fromPayload(hex_1.hexToBytes(payload), ss58Format !== undefined ? ss58Format : 42);
};
SubstrateAddress.fromEncoded = function (encoded) {
return this.fromBytes(bs58.decode(encoded));
};
SubstrateAddress.fromBytes = function (bytes) {
var buffer = Buffer.isBuffer(bytes) ? bytes : Buffer.from(bytes);
var checksumBytes = buffer.length === 35 ? 2 : 1;
var version = buffer.slice(0, 1);
var payload = buffer.slice(1, -checksumBytes);
var checksum = buffer.slice(-checksumBytes);
return new SubstrateAddress(version, payload, checksum);
};
SubstrateAddress.fromPayload = function (payload, format) {
var version = Buffer.from([format]);
var checksum = this.generateChecksum(Buffer.concat([version, payload]));
var checksumBytes = payload.length === 32 ? 2 : 1;
return new SubstrateAddress(version, payload, checksum.slice(0, checksumBytes));
};
SubstrateAddress.generateChecksum = function (input) {
var prefixBuffer = Buffer.from(SS58_PREFIX);
return Buffer.from(blake2b_1.blake2bAsBytes(Buffer.concat([prefixBuffer, input]), 512));
};
SubstrateAddress.prototype.compare = function (other) {
if (typeof other === 'string' && hex_1.isHex(other)) {
return this.payload.compare(Buffer.from(other, 'hex'));
}
else if (typeof other === 'string') {
return this.toString().localeCompare(other);
}
else {
return this.payload.compare(other.payload);
}
};
SubstrateAddress.prototype.toString = function () {
if (!this.encoded) {
this.encoded = bs58.encode(Buffer.concat([this.version, this.payload, this.checksum]));
}
return this.encoded;
};
SubstrateAddress.prototype.getBufferPublicKey = function () {
return this.payload;
};
SubstrateAddress.prototype.getHexPublicKey = function () {
return this.payload.toString('hex');
};
return SubstrateAddress;
}());
exports.SubstrateAddress = SubstrateAddress;
//# sourceMappingURL=SubstrateAddress.js.map