本文整理汇总了TypeScript中bn.js.default.toString方法的典型用法代码示例。如果您正苦于以下问题:TypeScript js.default.toString方法的具体用法?TypeScript js.default.toString怎么用?TypeScript js.default.toString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bn.js.default
的用法示例。
在下文中一共展示了js.default.toString方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: makeTokenTransferSkeleton
export function makeTokenTransferSkeleton(recipientAddress: string, consensusHash: string,
tokenType: string, tokenAmount: BN,
scratchArea: string
) {
/*
Format:
0 2 3 19 38 46 80
|-----|--|--------------|----------|-----------|-------------------------|
magic op consensus_hash token_type amount (BE) scratch area
(ns_id)
output 0: token transfer code
output 1: recipient address
*/
if (scratchArea.length > 34) {
throw new Error('Invalid scratch area: must be no more than 34 bytes')
}
const opReturnBuffer = Buffer.alloc(46 + scratchArea.length)
const tokenTypeHex = Buffer.from(tokenType).toString('hex')
const tokenTypeHexPadded = `00000000000000000000000000000000000000${tokenTypeHex}`.slice(-38)
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 = bitcoin.payments.embed({ data: [opReturnBuffer] }).output
const tx = makeTXbuilder()
tx.addOutput(nullOutput, 0)
tx.addOutput(recipientAddress, DUST_MINIMUM)
return tx.buildIncomplete()
}
示例2: bnToBytes
export function bnToBytes(bn: BN, length: number) {
return hexToBytes(bn.toString(16, length));
}