@bsv/sdk
Version:
BSV Blockchain Software Development Kit
76 lines • 2.78 kB
JavaScript
import PushDrop from '../script/templates/PushDrop.js';
import { Utils } from '../primitives/index.js';
/**
* Script template enabling the creation, unlocking, and decoding of SHIP and SLAP advertisements.
*/
export default class OverlayAdminTokenTemplate {
pushDrop;
/**
* 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.decode(script);
if (result.fields.length < 4) {
throw new Error('Invalid SHIP/SLAP advertisement!');
}
const protocol = Utils.toUTF8(result.fields[0]);
if (protocol !== 'SHIP' && protocol !== 'SLAP') {
throw new Error('Invalid protocol type!');
}
const identityKey = Utils.toHex(result.fields[1]);
const domain = Utils.toUTF8(result.fields[2]);
const topicOrService = 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(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([
Utils.toArray(protocol, 'utf8'),
Utils.toArray(identityKey, 'hex'),
Utils.toArray(domain, 'utf8'),
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');
}
}
//# sourceMappingURL=OverlayAdminTokenTemplate.js.map