Changeset View
Changeset View
Standalone View
Standalone View
cashtab/src/utils/__tests__/cashMethods.test.js
Show First 20 Lines • Show All 664 Lines • ▼ Show 20 Lines | it('generateAliasOpReturnScript() correctly generates OP_RETURN script for a valid alias registration for a p2pkh address', () => { | ||||
const opReturn = '6a'; | const opReturn = '6a'; | ||||
// push protocol identifier | // push protocol identifier | ||||
const prefixBytesHex = '04'; | const prefixBytesHex = '04'; | ||||
const aliasIdentifier = '2e786563'; | const aliasIdentifier = '2e786563'; | ||||
// push alias tx version | // push alias tx version | ||||
const aliasProtocolVersionNumberHex = '00'; | const aliasProtocolVersionNumberHex = '00'; | ||||
// push the alias | |||||
const aliasHexBytes = '04'; // alias.length in one byte of hex | |||||
const aliasHex = Buffer.from(alias).toString('hex'); | |||||
// push the address | // push the address | ||||
const aliasAddressBytesHex = '15'; // (1 + 20) in one byte of hex | const aliasAddressBytesHex = '15'; // (1 + 20) in one byte of hex | ||||
const p2pkhVersionByteHex = '00'; | const p2pkhVersionByteHex = '00'; | ||||
// push the alias | |||||
const aliasHexBytes = '04'; // alias.length in one byte of hex | |||||
const aliasHex = Buffer.from(alias).toString('hex'); | |||||
const aliasTxOpReturnOutputScript = [ | const aliasTxOpReturnOutputScript = [ | ||||
opReturn, | opReturn, | ||||
prefixBytesHex, | prefixBytesHex, | ||||
aliasIdentifier, | aliasIdentifier, | ||||
aliasProtocolVersionNumberHex, | aliasProtocolVersionNumberHex, | ||||
aliasHexBytes, | |||||
aliasHex, | |||||
aliasAddressBytesHex, | aliasAddressBytesHex, | ||||
p2pkhVersionByteHex, | p2pkhVersionByteHex, | ||||
hash, | hash, | ||||
aliasHexBytes, | |||||
aliasHex, | |||||
].join(''); | ].join(''); | ||||
// Calculate the expected outputScript with the tested function | // Calculate the expected outputScript with the tested function | ||||
const aliasOutputScript = generateAliasOpReturnScript(alias, address); | const aliasOutputScript = generateAliasOpReturnScript(alias, address); | ||||
// aliasOutputScript.toString('hex') | // aliasOutputScript.toString('hex') | ||||
// 6a042e78656301000474657374150095e79f51d4260bc0dc3ba7fb77c7be92d0fbdd1d | // 6a042e78656300150095e79f51d4260bc0dc3ba7fb77c7be92d0fbdd1d0474657374 | ||||
expect(aliasOutputScript.toString('hex')).toBe(aliasTxOpReturnOutputScript); | expect(aliasOutputScript.toString('hex')).toBe(aliasTxOpReturnOutputScript); | ||||
}); | }); | ||||
it('generateAliasOpReturnScript() correctly generates OP_RETURN script for a valid alias registration for a p2sh address', () => { | it('generateAliasOpReturnScript() correctly generates OP_RETURN script for a valid alias registration for a p2sh address', () => { | ||||
const alias = 'testtwo'; | const alias = 'testtwo'; | ||||
const address = 'ecash:prfhcnyqnl5cgrnmlfmms675w93ld7mvvqd0y8lz07'; | const address = 'ecash:prfhcnyqnl5cgrnmlfmms675w93ld7mvvqd0y8lz07'; | ||||
const { hash } = cashaddr.decode(address, true); | const { hash } = cashaddr.decode(address, true); | ||||
// Manually build the expected outputScript | // Manually build the expected outputScript | ||||
const opReturn = '6a'; | const opReturn = '6a'; | ||||
// push protocol identifier | // push protocol identifier | ||||
const prefixBytesHex = '04'; | const prefixBytesHex = '04'; | ||||
const aliasIdentifier = '2e786563'; | const aliasIdentifier = '2e786563'; | ||||
// push alias tx version | // push alias tx version | ||||
const aliasProtocolVersionNumberHex = '00'; | const aliasProtocolVersionNumberHex = '00'; | ||||
// push the alias | |||||
const aliasHexBytes = '07'; // alias.length in one byte of hex | |||||
const aliasHex = Buffer.from(alias).toString('hex'); | |||||
// push the address | // push the address | ||||
const aliasAddressBytesHex = '15'; // (1 + 20) in one byte of hex | const aliasAddressBytesHex = '15'; // (1 + 20) in one byte of hex | ||||
const p2shVersionByteHex = '08'; | const p2shVersionByteHex = '08'; | ||||
// push the alias | |||||
const aliasHexBytes = '07'; // alias.length in one byte of hex | |||||
const aliasHex = Buffer.from(alias).toString('hex'); | |||||
const aliasTxOpReturnOutputScript = [ | const aliasTxOpReturnOutputScript = [ | ||||
opReturn, | opReturn, | ||||
prefixBytesHex, | prefixBytesHex, | ||||
aliasIdentifier, | aliasIdentifier, | ||||
aliasProtocolVersionNumberHex, | aliasProtocolVersionNumberHex, | ||||
aliasHexBytes, | |||||
aliasHex, | |||||
aliasAddressBytesHex, | aliasAddressBytesHex, | ||||
p2shVersionByteHex, | p2shVersionByteHex, | ||||
hash, | hash, | ||||
aliasHexBytes, | |||||
aliasHex, | |||||
].join(''); | ].join(''); | ||||
// Calculate the expected outputScript with the tested function | // Calculate the expected outputScript with the tested function | ||||
const aliasOutputScript = generateAliasOpReturnScript(alias, address); | const aliasOutputScript = generateAliasOpReturnScript(alias, address); | ||||
// aliasOutputScript.toString('hex') | // aliasOutputScript.toString('hex') | ||||
// 6a042e7865630100077465737474776f1508d37c4c809fe9840e7bfa77b86bd47163f6fb6c60 | // 6a042e786563001508d37c4c809fe9840e7bfa77b86bd47163f6fb6c60077465737474776f | ||||
expect(aliasOutputScript.toString('hex')).toBe(aliasTxOpReturnOutputScript); | expect(aliasOutputScript.toString('hex')).toBe(aliasTxOpReturnOutputScript); | ||||
}); | }); | ||||
it('generateOpReturnScript() correctly generates an encrypted message script', () => { | it('generateOpReturnScript() correctly generates an encrypted message script', () => { | ||||
const optionalOpReturnMsg = 'testing generateOpReturnScript()'; | const optionalOpReturnMsg = 'testing generateOpReturnScript()'; | ||||
const encryptionFlag = true; | const encryptionFlag = true; | ||||
const airdropFlag = false; | const airdropFlag = false; | ||||
const airdropTokenId = null; | const airdropTokenId = null; | ||||
▲ Show 20 Lines • Show All 1,168 Lines • Show Last 20 Lines |