@bsv/sdk
Version:
BSV Blockchain Software Development Kit
81 lines • 3.16 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const PushDrop_js_1 = __importDefault(require("../script/templates/PushDrop.js"));
const index_js_1 = require("../primitives/index.js");
/**
* Script template enabling the creation, unlocking, and decoding of SHIP and SLAP advertisements.
*/
class OverlayAdminTokenTemplate {
/**
* Decodes a SHIP or SLAP advertisement from a given locking script.
* @param script Locking script comprising a SHIP or SLAP token to decode
* @returns Decoded SHIP or SLAP advertisement
*/
static decode(script) {
const result = PushDrop_js_1.default.decode(script);
if (result.fields.length < 4) {
throw new Error('Invalid SHIP/SLAP advertisement!');
}
const protocol = index_js_1.Utils.toUTF8(result.fields[0]);
if (protocol !== 'SHIP' && protocol !== 'SLAP') {
throw new Error('Invalid protocol type!');
}
const identityKey = index_js_1.Utils.toHex(result.fields[1]);
const domain = index_js_1.Utils.toUTF8(result.fields[2]);
const topicOrService = index_js_1.Utils.toUTF8(result.fields[3]);
return {
protocol,
identityKey,
domain,
topicOrService
};
}
/**
* Constructs a new Overlay Admin template instance
* @param wallet Wallet to use for locking and unlocking
*/
constructor(wallet) {
this.pushDrop = new PushDrop_js_1.default(wallet);
}
/**
* Creates a new advertisement locking script
* @param protocol SHIP or SLAP
* @param domain Domain where the topic or service is available
* @param topicOrService Topic or service to advertise
* @returns Locking script comprising the advertisement token
*/
async lock(protocol, domain, topicOrService) {
const { publicKey: identityKey } = await this.pushDrop.wallet.getPublicKey({
identityKey: true
});
return await this.pushDrop.lock([
index_js_1.Utils.toArray(protocol, 'utf8'),
index_js_1.Utils.toArray(identityKey, 'hex'),
index_js_1.Utils.toArray(domain, 'utf8'),
index_js_1.Utils.toArray(topicOrService, 'utf8')
], [
2,
protocol === 'SHIP'
? 'Service Host Interconnect'
: 'Service Lookup Availability'
], '1', 'self');
}
/**
* Unlocks an advertisement token as part of a transaction.
* @param protocol SHIP or SLAP, depending on the token to unlock
* @returns Script unlocker capable of unlocking the advertisement token
*/
unlock(protocol) {
return this.pushDrop.unlock([
2,
protocol === 'SHIP'
? 'Service Host Interconnect'
: 'Service Lookup Availability'
], '1', 'self');
}
}
exports.default = OverlayAdminTokenTemplate;
//# sourceMappingURL=OverlayAdminTokenTemplate.js.map