@omisego/omg-js-util
Advanced tools
Comparing version 3.0.0-alpha.12 to 3.0.0-alpha.13
{ | ||
"name": "@omisego/omg-js-util", | ||
"version": "3.0.0-alpha.12", | ||
"version": "3.0.0-alpha.13", | ||
"description": "OMG util module", | ||
@@ -33,3 +33,3 @@ "main": "src/index.js", | ||
}, | ||
"gitHead": "9e38443a3e8d9b8aedf56155acf4bb0eb3c71e4b" | ||
"gitHead": "8224089703ac81d887d479898a79d15cf4b18fcc" | ||
} |
@@ -76,5 +76,6 @@ /* | ||
* @typedef {Object} TransactionBody | ||
* @property {number} transactionType | ||
* @property {number} txType | ||
* @property {UTXO[]} inputs | ||
* @property {Output[]} outputs | ||
* @property {number} txData | ||
* @property {string} metadata | ||
@@ -81,0 +82,0 @@ */ |
@@ -75,2 +75,4 @@ /* | ||
txArray.push(outputArray) | ||
txArray.push(typedDataMessage.txData) | ||
txArray.push(typedDataMessage.metadata) | ||
@@ -92,3 +94,3 @@ | ||
const encoded = transaction.encode({ | ||
transactionType: 1, | ||
txType: 1, | ||
inputs: [], | ||
@@ -101,2 +103,3 @@ outputs: [{ | ||
}], | ||
txData: 0, | ||
metadata: typedData.NULL_METADATA | ||
@@ -133,4 +136,4 @@ }) | ||
*/ | ||
encode: function ({ transactionType, inputs, outputs, metadata, signatures }, { signed = true } = {}) { | ||
const txArray = [transactionType] | ||
encode: function ({ txType, inputs, outputs, txData, metadata, signatures }, { signed = true } = {}) { | ||
const txArray = [txType] | ||
signatures && signed && txArray.unshift(signatures) | ||
@@ -153,2 +156,3 @@ const inputArray = [] | ||
txArray.push(outputArray) | ||
txArray.push(txData) | ||
txArray.push(metadata) | ||
@@ -166,12 +170,12 @@ return hexPrefix(rlp.encode(txArray).toString('hex')) | ||
decodeTxBytes: function (tx) { | ||
let inputs, outputs, transactionType, metadata, sigs | ||
let txType, inputs, outputs, txData, metadata, sigs | ||
const rawTx = Buffer.isBuffer(tx) ? tx : Buffer.from(tx.replace('0x', ''), 'hex') | ||
const decoded = rlp.decode(rawTx) | ||
switch (decoded.length) { | ||
case 4: | ||
[transactionType, inputs, outputs, metadata] = decoded | ||
break | ||
case 5: | ||
[sigs, transactionType, inputs, outputs, metadata] = decoded | ||
[txType, inputs, outputs, txData, metadata] = decoded | ||
break | ||
case 6: | ||
[sigs, txType, inputs, outputs, txData, metadata] = decoded | ||
break | ||
default: | ||
@@ -182,11 +186,13 @@ throw new Error(`error decoding txBytes, got ${decoded}`) | ||
...(sigs && { sigs: sigs.map(parseString) }), | ||
transactionType: parseNumber(transactionType), | ||
txType: parseNumber(txType), | ||
inputs: inputs.map(input => transaction.decodeUtxoPos(parseString(input))), | ||
outputs: outputs.map(output => { | ||
const outputData = output[1] | ||
const outputType = parseNumber(output[0]) | ||
const outputGuard = parseString(output[1]) | ||
const currency = parseString(output[2]) | ||
const amount = numberToBN(parseString(output[3])).toString() | ||
const outputGuard = parseString(outputData[0]) | ||
const currency = parseString(outputData[1]) | ||
const amount = numberToBN(parseString(outputData[2])).toString() | ||
return { outputType, outputGuard, currency, amount } | ||
}), | ||
txData: parseNumber(txData), | ||
metadata: parseString(metadata) | ||
@@ -280,2 +286,3 @@ } | ||
outputs: outputArr, | ||
txData: 0, | ||
metadata | ||
@@ -419,5 +426,7 @@ } | ||
output.outputType, | ||
sanitiseAddress(output.outputGuard), // must start with '0x' to be encoded properly | ||
sanitiseAddress(output.currency), // must start with '0x' to be encoded properly | ||
numberToBN(output.amount) | ||
[ | ||
sanitiseAddress(output.outputGuard), | ||
sanitiseAddress(output.currency), | ||
numberToBN(output.amount) | ||
] | ||
]) | ||
@@ -424,0 +433,0 @@ } |
@@ -19,3 +19,8 @@ /* | ||
const NULL_INPUT = { blknum: 0, txindex: 0, oindex: 0 } | ||
const NULL_OUTPUT = { outputType: 0, outputGuard: NULL_ADDRESS, currency: NULL_ADDRESS, amount: 0 } | ||
const NULL_OUTPUT = { | ||
outputType: 0, | ||
outputGuard: NULL_ADDRESS, | ||
currency: NULL_ADDRESS, | ||
amount: 0 | ||
} | ||
const NULL_METADATA = '0x0000000000000000000000000000000000000000000000000000000000000000' | ||
@@ -40,2 +45,3 @@ | ||
{ name: 'output3', type: 'Output' }, | ||
{ name: 'txData', type: 'uint256' }, | ||
{ name: 'metadata', type: 'bytes32' } | ||
@@ -94,3 +100,3 @@ ] | ||
typedData.message = { | ||
txType: 1, | ||
txType: tx.txType || 1, | ||
input0: inputs.length > 0 ? inputs[0] : NULL_INPUT, | ||
@@ -104,2 +110,3 @@ input1: inputs.length > 1 ? inputs[1] : NULL_INPUT, | ||
output3: outputs.length > 3 ? outputs[3] : NULL_OUTPUT, | ||
txData: tx.txData || 0, | ||
metadata: tx.metadata || NULL_METADATA | ||
@@ -106,0 +113,0 @@ } |
@@ -23,6 +23,6 @@ /* | ||
describe('decodeTransaction', function () { | ||
describe('Decode transaction tests', function () { | ||
it('should decode an encoded unsigned transaction with inputs and outputs', async function () { | ||
const txBody = { | ||
transactionType: 1, | ||
txType: 1, | ||
inputs: [ | ||
@@ -66,2 +66,3 @@ { | ||
], | ||
txData: 0, | ||
metadata: transaction.NULL_METADATA | ||
@@ -111,3 +112,3 @@ } | ||
const txBody = { | ||
transactionType: 1, | ||
txType: 1, | ||
inputs: [ | ||
@@ -126,2 +127,3 @@ { | ||
outputs: [], | ||
txData: 0, | ||
metadata: transaction.NULL_METADATA | ||
@@ -147,3 +149,4 @@ } | ||
'outputs', | ||
'transactionType', | ||
'txType', | ||
'txData', | ||
'metadata' | ||
@@ -156,3 +159,3 @@ ]) | ||
const txBody = { | ||
transactionType: 1, | ||
txType: 1, | ||
inputs: [ | ||
@@ -184,2 +187,3 @@ { | ||
], | ||
txData: 0, | ||
metadata: transaction.NULL_METADATA | ||
@@ -205,3 +209,4 @@ } | ||
'outputs', | ||
'transactionType', | ||
'txType', | ||
'txData', | ||
'metadata' | ||
@@ -220,3 +225,3 @@ ]) | ||
assert.deepEqual(decoded, { | ||
transactionType: 1, | ||
txType: 1, | ||
inputs: [], | ||
@@ -231,2 +236,3 @@ outputs: [ | ||
], | ||
txData: 0, | ||
metadata: | ||
@@ -233,0 +239,0 @@ '0x0000000000000000000000000000000000000000000000000000000000000000' |
@@ -19,5 +19,6 @@ /* | ||
const transaction = require('../src/transaction') | ||
describe('Transaction tests', function () { | ||
describe('Encode transaction tests', function () { | ||
it('should return an encoded transaction from 1 input and 1 output', async function () { | ||
const txBody = { | ||
txType: 1, | ||
inputs: [ | ||
@@ -41,3 +42,3 @@ { | ||
const expectedTx = | ||
'0xf85a80e1a0000000000000000000000000000000000000000000000000004640596164aa00f5f40194f4ebbe787311bb955bb353b7a4d8b97af8ed1c9b9400000000000000000000000000000000000000008806f05b59d3b2006480' | ||
'0xf85c01e1a0000000000000000000000000000000000000000000000000004640596164aa00f6f501f394f4ebbe787311bb955bb353b7a4d8b97af8ed1c9b9400000000000000000000000000000000000000008806f05b59d3b200648080' | ||
assert.equal(unsignedTx, expectedTx) | ||
@@ -48,3 +49,3 @@ }) | ||
const txBody = { | ||
transactionType: 1, | ||
txType: 1, | ||
inputs: [ | ||
@@ -71,3 +72,3 @@ { | ||
{ | ||
transactionType: 1, | ||
txType: 1, | ||
inputs: [ | ||
@@ -88,3 +89,4 @@ { | ||
], | ||
metadata: '0x0000000000000000000000000000000000000000' | ||
metadata: '0x0000000000000000000000000000000000000000', | ||
txData: 0 | ||
}, | ||
@@ -97,2 +99,3 @@ decodedTx | ||
const txBody = { | ||
txType: 1, | ||
inputs: [ | ||
@@ -145,3 +148,4 @@ { | ||
} | ||
] | ||
], | ||
txData: 0 | ||
} | ||
@@ -151,3 +155,3 @@ | ||
const expectedTx = | ||
'0xf9015280f884a0000000000000000000000000000000000000000000000000004640596164aa00a0000000000000000000000000000000000000000000000000004640599cff7400a0000000000000000000000000000000000000000000000000004640599cff7401a0000000000000000000000000000000000000000000000000004640599d00faa0f8c8f40194f4ebbe787311bb955bb353b7a4d8b97af8ed1c9b940000000000000000000000000000000000000000880de0b6b3a7640000f401943272ee86d8192f59261960c9ae186063c8c9041f940000000000000000000000000000000000000000880de0b6b3a7640000f0019499d7b5c57c16acb24a327a37356a804a2f75dade9400000000000000000000000000000000000000008405f5e100ec0194bfdf85743ef16cfb1f8d4dd1dfc74c51dc4964349400000000000000000000000000000000000000006480' | ||
'0xf9015701f884a0000000000000000000000000000000000000000000000000004640596164aa00a0000000000000000000000000000000000000000000000000004640599cff7400a0000000000000000000000000000000000000000000000000004640599cff7401a0000000000000000000000000000000000000000000000000004640599d00faa0f8ccf501f394f4ebbe787311bb955bb353b7a4d8b97af8ed1c9b940000000000000000000000000000000000000000880de0b6b3a7640000f501f3943272ee86d8192f59261960c9ae186063c8c9041f940000000000000000000000000000000000000000880de0b6b3a7640000f101ef9499d7b5c57c16acb24a327a37356a804a2f75dade9400000000000000000000000000000000000000008405f5e100ed01eb94bfdf85743ef16cfb1f8d4dd1dfc74c51dc496434940000000000000000000000000000000000000000648080' | ||
assert.equal(unsignedTx, expectedTx) | ||
@@ -158,2 +162,3 @@ }) | ||
const txBody = { | ||
txType: 1, | ||
inputs: [ | ||
@@ -166,6 +171,7 @@ { | ||
], | ||
outputs: [] | ||
outputs: [], | ||
txData: 0 | ||
} | ||
const unsignedTx = transaction.encode(txBody) | ||
const expectedTx = '0xe580e1a0000000000000000000000000000000000000000000000000004640596164aa00c080' | ||
const expectedTx = '0xe601e1a0000000000000000000000000000000000000000000000000004640596164aa00c08080' | ||
assert.equal(unsignedTx, expectedTx) | ||
@@ -179,3 +185,3 @@ }) | ||
const expected = | ||
'0xf85301c0efee0194854951e37c68a99a52d9e3ae15e0cb62184a613e94000000000000000000000000000000000000000082014da00000000000000000000000000000000000000000000000000000000000000000' | ||
'0xf85501c0f0ef01ed94854951e37c68a99a52d9e3ae15e0cb62184a613e94000000000000000000000000000000000000000082014d80a00000000000000000000000000000000000000000000000000000000000000000' | ||
@@ -194,3 +200,3 @@ const encoded = transaction.encodeDeposit( | ||
const expected = | ||
'0xf85901c0f5f4019460c5bc2de80acda5016c1e81f61620adbc730dd2940000000000000000000000000000000000000000880de0b6b3a7640000a00000000000000000000000000000000000000000000000000000000000000000' | ||
'0xf85b01c0f6f501f39460c5bc2de80acda5016c1e81f61620adbc730dd2940000000000000000000000000000000000000000880de0b6b3a764000080a00000000000000000000000000000000000000000000000000000000000000000' | ||
@@ -209,3 +215,3 @@ const encoded = transaction.encodeDeposit( | ||
const expected = | ||
'0xf85a01c0f6f5019460c5bc2de80acda5016c1e81f61620adbc730dd294000000000000000000000000000000000000000089a2a15d09519be00000a00000000000000000000000000000000000000000000000000000000000000000' | ||
'0xf85c01c0f7f601f49460c5bc2de80acda5016c1e81f61620adbc730dd294000000000000000000000000000000000000000089a2a15d09519be0000080a00000000000000000000000000000000000000000000000000000000000000000' | ||
@@ -212,0 +218,0 @@ const encoded = transaction.encodeDeposit( |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
73817
1886