fcl-js
fcl-js copied to clipboard
RLP decode transaction back to flow transaction object
Instructions
It will be great that FCL can decode a RLP encoded message back to transaction object, when there is transaction relating with multi-sign then RLP will be reliable than the plain tx object.
Issue To Be Solved
lilico have a JS service which will sign and pay all user's transaction fee, but you know we can't blind sign everything, we need do some checks, like the payer is only for payer role, not proposer not authors. Current way we are verifying the original transaction object first, then we encode it and compare it with the dApp encoded message. But the issue is that even transaction object is same, but the 2 encoded message can't match each other might be due to different platform or version of fcl version between our end and dApp end.
For Example : One fcl sdk encode like:
{"type":"Array","value":[{"type":"UFix64","value":"1.00000000"}]}
another fcl sdk encode like:
{"type":"Array","value":[{"type":"UFix64","value":"1.0"}]}
or
{"type":"Array","value":[{"value":"1.00000000","type":"UFix64"}]}
Or even we received a incorrect RLP encode message from dApp end (see below example) .
Other example
- Then user go to EmeraId ID: https://id.ecdao.org/lilico
- Choose lilico
- Create Emerald id with lilico
- Transaction was failed ( now lilico fixed it on server end )
But here is the original RLP message we got from emeraId ID:
464c4f572d56302e302d7472616e73616374696f6e0000000000000000000000f9032bf9029ab9021c696d706f727420456d6572616c644964656e746974794c696c69636f2066726f6d203078333965343263363763633835316366620a0a202020207472616e73616374696f6e28646973636f726449443a20537472696e6729207b0a2020202020202020707265706172652861646d696e3a20417574684163636f756e742c20757365723a20417574684163636f756e7429207b0a2020202020202020202020206c65742061646d696e6973747261746f72203d2061646d696e2e626f72726f773c26456d6572616c644964656e746974794c696c69636f2e41646d696e6973747261746f723e2866726f6d3a20456d6572616c644964656e746974794c696c69636f2e41646d696e6973747261746f7253746f7261676550617468290a202020202020202020202020202020202020202020202020202020202020202020202020202020203f3f2070616e69632822436f756c64206e6f7420626f72726f77207468652061646d696e6973747261746f7222290a20202020202020202020202061646d696e6973747261746f722e637265617465456d6572616c644944286163636f756e743a20757365722e616464726573732c20646973636f726449443a20646973636f72644944290a20202020202020207d0a0a202020202020202065786563757465207b0a2020202020202020202020206c6f6728224372656174656420456d6572616c64494422290a20202020202020207d0a202020207defae7b2274797065223a22537472696e67222c2276616c7565223a22353930373430313339363638363638343137227da07ead6974ad6f7603b623e8b54890a469504503f9c85b979303eadbfa72534fbe82012c8884221fe0294044d7800588319e67f2ef9d937fd28839e42c67cc851cfb8884221fe0294044d7f88cf8440280b8408cfd45d4cbf217a77c042ae4baf42e14914824c6a0cc2ea3e6869279693d1194da0efe29571d5d40738aceed699690ca2fcc77f92a3797e5ca07533534954271f8448080b840df50ced31fb3b3e8ccccab6d5389e10d70334cbf9ec55e6f08a91479efff6ac07f58b440132de02b2e19fefa59525f067f9483f634d5a392c1da8a534f407cea
After encode the message, I can see the signer Index is incorrect here.
Suggest A Solution
function removeTag(address: string): string | null {
if (address == null) return null
return address.replace(/^464c4f572d56302e302d7472616e73616374696f6e0000000000000000000000/, "0x")
}
function arrToStringArr(arr: any): any {
return arr.map((a: any) => {
if (Array.isArray(a)) {
return arrToStringArr(a)
}
return withPrefix(Buffer.from(a).toString('hex'))
})
}
const RLPEncodedTX = '464c4f57......'
const encodedMessage = removeTag(RLPEncodedTX)
const decoded = arrToStringArr(RLP.decode(encodedMessage))
const payloadDecode = decoded[0];
const sigDecode = decoded[1];
const roles: Roles = {
proposal: payloadDecode[4],
payer: payloadDecode[7],
authorizers: payloadDecode[8] as string[],
}
// TODO: convert decoded back to transaction object