Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const tokenValueHex = tokenAmount.toString(16, 2)
if (tokenValueHex.length > 16) {
// exceeds 2**64; can't fit
throw new Error(`Cannot send tokens: cannot fit ${tokenAmount.toString()} into 8 bytes`)
}
const tokenValueHexPadded = `0000000000000000${tokenValueHex}`.slice(-16)
opReturnBuffer.write(opEncode('$'), 0, 3, 'ascii')
opReturnBuffer.write(consensusHash, 3, consensusHash.length / 2, 'hex')
opReturnBuffer.write(tokenTypeHexPadded, 19, tokenTypeHexPadded.length / 2, 'hex')
opReturnBuffer.write(tokenValueHexPadded, 38, tokenValueHexPadded.length / 2, 'hex')
opReturnBuffer.write(scratchArea, 46, scratchArea.length, 'ascii')
const nullOutput = payments.embed({ data: [opReturnBuffer] }).output
const tx = makeTXbuilder()
tx.addOutput(nullOutput, 0)
tx.addOutput(recipientAddress, DUST_MINIMUM)
return tx.buildIncomplete()
}
hashed.copy(opReturnBuffer, 3)
opReturnBuffer.write(consensusHash, 23, 16, 'hex')
if (burnAmount.units !== 'BTC') {
const burnHex = burnAmount.amount.toString(16, 2)
if (burnHex.length > 16) {
// exceeds 2**64; can't fit
throw new Error(`Cannot preorder '${fullyQualifiedName}': cannot fit price into 8 bytes`)
}
const paddedBurnHex = `0000000000000000${burnHex}`.slice(-16)
opReturnBuffer.write(paddedBurnHex, 39, 8, 'hex')
opReturnBuffer.write(burnAmount.units, 47, burnAmount.units.length, 'ascii')
}
const nullOutput = payments.embed({ data: [opReturnBuffer] }).output
const tx = makeTXbuilder()
tx.addOutput(nullOutput, 0)
tx.addOutput(preorderAddress, DUST_MINIMUM)
if (burnAmount.units === 'BTC') {
const btcBurnAmount = burnAmount.amount.toNumber()
tx.addOutput(burnAddress, btcBurnAmount)
} else {
tx.addOutput(burnAddress, DUST_MINIMUM)
}
return tx.buildIncomplete()
}
0 2 3 39
|----|--|-----------------------------|
magic op name.ns_id (37 bytes)
output 0: the revoke code
*/
const opRet = Buffer.alloc(3)
const nameBuff = Buffer.from(fullyQualifiedName, 'ascii')
opRet.write(opEncode('~'), 0, 3, 'ascii')
const opReturnBuffer = Buffer.concat([opRet, nameBuff])
const nullOutput = payments.embed({ data: [opReturnBuffer] }).output
const tx = makeTXbuilder()
tx.addOutput(nullOutput, 0)
return tx.buildIncomplete()
}
output 1: new owner
*/
const opRet = Buffer.alloc(36)
let keepChar = '~'
if (keepZonefile) {
keepChar = '>'
}
opRet.write(opEncode('>'), 0, 3, 'ascii')
opRet.write(keepChar, 3, 1, 'ascii')
const hashed = hash128(Buffer.from(fullyQualifiedName, 'ascii'))
hashed.copy(opRet, 4)
opRet.write(consensusHash, 20, 16, 'hex')
const opRetPayload = payments.embed({ data: [opRet] }).output
const tx = makeTXbuilder()
tx.addOutput(opRetPayload, 0)
tx.addOutput(newOwner, DUST_MINIMUM)
return tx.buildIncomplete()
}
0 2 3 7 8 9 10 11 12 13 14 15 16 17 18 20 39
|-----|---|----|-----|-----|----|----|----|----|----|-----|-----|-----|--------|-------|-------|
magic op life coeff. base 1-2 3-4 5-6 7-8 9-10 11-12 13-14 15-16 nonalpha version ns ID
bucket exponents no-vowel
discounts
output 0: namespace reveal code
output 1: reveal address
*/
const hexPayload = namespace.toHexPayload()
const opReturnBuffer = Buffer.alloc(3 + hexPayload.length / 2)
opReturnBuffer.write(opEncode('&'), 0, 3, 'ascii')
opReturnBuffer.write(hexPayload, 3, hexPayload.length / 2, 'hex')
const nullOutput = payments.embed({ data: [opReturnBuffer] }).output
const tx = makeTXbuilder()
tx.addOutput(nullOutput, 0)
tx.addOutput(revealAddress, DUST_MINIMUM)
return tx.buildIncomplete()
}
export function makeNamespaceReadySkeleton(namespaceID: string) {
/*
Format:
0 2 3 4 23
|-----|--|--|------------|
magic op . ns_id
output 0: namespace ready code
*/
const opReturnBuffer = Buffer.alloc(3 + namespaceID.length + 1)
opReturnBuffer.write(opEncode('!'), 0, 3, 'ascii')
opReturnBuffer.write(`.${namespaceID}`, 3, namespaceID.length + 1, 'ascii')
const nullOutput = payments.embed({ data: [opReturnBuffer] }).output
const tx = makeTXbuilder()
tx.addOutput(nullOutput, 0)
return tx.buildIncomplete()
}
}
}
const payloadLen = burnTokenAmountHex ? 65 : 57
payload = Buffer.alloc(payloadLen, 0)
payload.write(fullyQualifiedName, 0, 37, 'ascii')
payload.write(valueHash, 37, 20, 'hex')
if (!!burnTokenAmountHex) {
payload.write(burnTokenAmountHex, 57, 8, 'hex')
}
} else {
payload = Buffer.from(fullyQualifiedName, 'ascii')
}
const opReturnBuffer = Buffer.concat([Buffer.from(opEncode(':'), 'ascii'), payload])
const nullOutput = payments.embed({ data: [opReturnBuffer] }).output
const tx = makeTXbuilder()
tx.addOutput(nullOutput, 0)
tx.addOutput(ownerAddress, DUST_MINIMUM)
return tx.buildIncomplete()
}
magic op name.ns_id (37 bytes)
Output 0: the OP_RETURN
Output 1: the recipient
Output 2: the zonefile hash
*/
if (zonefileHash.length !== 40) {
throw new Error('Invalid zonefile hash: must be 20 bytes hex-encoded')
}
const network = config.network
const opReturnBuffer = Buffer.alloc(3 + name.length)
opReturnBuffer.write(opEncode(';'), 0, 3, 'ascii')
opReturnBuffer.write(name, 3, name.length, 'ascii')
const nullOutput = payments.embed({ data: [opReturnBuffer] }).output
const tx = makeTXbuilder()
const zonefileHashB58 = bjsAddress.toBase58Check(
Buffer.from(zonefileHash, 'hex'), network.layer1.pubKeyHash
)
tx.addOutput(nullOutput, 0)
tx.addOutput(recipientAddr, DUST_MINIMUM)
tx.addOutput(zonefileHashB58, DUST_MINIMUM)
return tx.buildIncomplete()
}
0 2 3 23
|----|--|-----------------------------|
magic op message hash (160-bit)
output 0: the OP_RETURN
*/
if (messageHash.length !== 40) {
throw new Error('Invalid message hash: must be 20 bytes hex-encoded')
}
const opReturnBuffer = Buffer.alloc(3 + messageHash.length / 2)
opReturnBuffer.write(opEncode('#'), 0, 3, 'ascii')
opReturnBuffer.write(messageHash, 3, messageHash.length / 2, 'hex')
const nullOutput = payments.embed({ data: [opReturnBuffer] }).output
const tx = makeTXbuilder()
tx.addOutput(nullOutput, 0)
return tx.buildIncomplete()
}