Skip to content

Commit

Permalink
chore!: update rlp to 3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Feb 11, 2022
1 parent 1a8d287 commit bb32b77
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 30 deletions.
41 changes: 28 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -51,7 +51,7 @@
"cross-fetch": "^3.1.5",
"events": "^3.3.0",
"process": "^0.11.10",
"rlp": "^2.2.7",
"rlp": "^3.0.0",
"sha.js": "^2.4.11",
"stream-browserify": "^3.0.0",
"swagger-client": "^3.18.4",
Expand Down
4 changes: 2 additions & 2 deletions src/tx/builder/helpers.js
Expand Up @@ -199,7 +199,7 @@ export function writeId (hashId) {
* @return {String} Encoided hash string with prefix
*/
export function readId (buf) {
const tag = buf.readUIntBE(0, 1)
const tag = Buffer.from(buf).readUIntBE(0, 1)
const prefix = ID_TAG_PREFIX[tag]
if (!prefix) throw new PrefixNotFoundError(tag)
return encode(buf.slice(1, buf.length), prefix)
Expand All @@ -224,7 +224,7 @@ export function writeInt (val) {
* @return {String} Buffer Buffer from number(BigEndian)
*/
export function readInt (buf = Buffer.from([])) {
return BigNumber(buf.toString('hex'), 16).toString(10)
return new BigNumber(Buffer.from(buf).toString('hex'), 16).toString(10)
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/tx/tx-object.js
Expand Up @@ -41,7 +41,7 @@ const buildTransaction = (type, params, options = {}) => {
/**
* Unpack transaction from RLP encoded binary or base64c string
* @alias module:@aeternity/aepp-sdk/es/tx/tx-object
* @param {Buffer|String} tx RLP encoded binary or base64c(rlpBinary) string
* @param {Uint8Array|String} tx RLP encoded binary or base64c(rlpBinary) string
* @throws {Error} Arguments validation error's
* @return {{
* encodedTx: String, binary: Array<Buffer>, rlpEncoded: Buffer, type: String, params: Object
Expand All @@ -53,10 +53,11 @@ const unpackTransaction = (tx) => {
const { txType: type, tx: params, rlpEncoded, binary } = unpackTx(tx)
return { encodedTx: tx, type, params, rlpEncoded, binary }
}
if (Buffer.isBuffer(tx)) {
if (tx instanceof Uint8Array) {
const { txType: type, tx: params, rlpEncoded, binary } = unpackTx(tx, true)
return { encodedTx: encode(tx, 'tx'), type, params, rlpEncoded, binary }
}
throw new InvalidTxError(`"tx" should be a string or Uint8Array, got ${tx} instead`)
}

/**
Expand Down
19 changes: 7 additions & 12 deletions test/unit/tx-object.js
Expand Up @@ -38,6 +38,10 @@ describe('TxObject', () => {
expect(() => TxObject()).to.throw(InvalidTxError, 'Invalid TxObject arguments. Please provide one of { tx: "tx_asdasd23..." } or { type: "spendTx", params: {...} }')
})

it('Invalid "tx"', () => {
expect(() => TxObject({ tx: {} })).to.throw(InvalidTxError, '"tx" should be a string or Uint8Array, got [object Object] instead')
})

it('Invalid "params"', () => {
expect(() => TxObject({ params: true, type: TX_TYPE.spend })).to.throw(TypeError, '"params" should be an object')
})
Expand Down Expand Up @@ -67,23 +71,14 @@ describe('TxObject', () => {
})
signedTx = await MemoryAccount({ keypair: keyPair, networkId: 'ae_mainnet' }).signTransaction(txObject.encodedTx)
txObject.encodedTx.should.be.a('string')
Buffer.isBuffer(txObject.rlpEncoded).should.be.equal(true)
expect(txObject.rlpEncoded).to.be.an.instanceOf(Uint8Array)
txObject.binary.should.be.a('Array')
txObject.params.should.be.a('object')
})

it('Unpack transaction from string/rlp', () => {
const txFromString = TxObject.fromString(txObject.encodedTx)
txFromString.rlpEncoded.equals(txObject.rlpEncoded).should.be.equal(true)
Buffer.from(txFromString.binary).equals(Buffer.from(txObject.binary)).should.be.equal(true)
txFromString.encodedTx.should.be.equal(txObject.encodedTx)
txFromString.params.should.be.deep.include(txObject.params)
const rtxFromRlpBinary = TxObject.fromRlp(txObject.rlpEncoded)
rtxFromRlpBinary.rlpEncoded.equals(txObject.rlpEncoded).should.be.equal(true)
Buffer.from(rtxFromRlpBinary.binary).equals(Buffer.from(txObject.binary))
.should.be.equal(true)
rtxFromRlpBinary.encodedTx.should.be.equal(txObject.encodedTx)
rtxFromRlpBinary.params.should.be.deep.include(txObject.params)
expect(TxObject.fromString(txObject.encodedTx)).to.eql(txObject)
expect(TxObject.fromRlp(txObject.rlpEncoded)).to.eql(txObject)
})

it('Unpack signed transaction', () => {
Expand Down

0 comments on commit bb32b77

Please sign in to comment.