Skip to content

Commit

Permalink
Merge pull request #356 from alephium/decode-txscript
Browse files Browse the repository at this point in the history
Add TxScript Codec
  • Loading branch information
polarker authored May 24, 2024
2 parents 9e9ab70 + 88971b9 commit bcca3fc
Show file tree
Hide file tree
Showing 40 changed files with 156 additions and 47 deletions.
4 changes: 3 additions & 1 deletion .project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"fullNodeVersion": "v2.14.0",
"fullNodeVersion": "v2.14.7",
"compilerOptionsUsed": {
"ignoreUnusedConstantsWarnings": false,
"ignoreUnusedVariablesWarnings": false,
Expand Down Expand Up @@ -186,6 +186,8 @@
"bytecodeDebugPatch": "=6-2+73=2-2+d8=2-2+69=11-1+a=50+7a7e0214696e73657274206174206d617020706174683a2000=56+7a7e0214696e73657274206174206d617020706174683a2000=227-1+9=124+7a7e021472656d6f7665206174206d617020706174683a2000=46+7a7e021472656d6f7665206174206d617020706174683a2000=6",
"codeHashDebug": "034dec32e1107787800ec476a31b11ed2f5c201c06aa9360d1a1a074d6b29098",
"warnings": [
"No external caller check for function \"MapTest.remove\". Please use \"checkCaller!(...)\" in the function or its callees, or disable it with \"@using(checkExternalCaller = false)\".",
"No external caller check for function \"MapTest.update\". Please use \"checkCaller!(...)\" in the function or its callees, or disable it with \"@using(checkExternalCaller = false)\".",
"No external caller check for function \"MapTest.insert\". Please use \"checkCaller!(...)\" in the function or its callees, or disable it with \"@using(checkExternalCaller = false)\"."
]
},
Expand Down
2 changes: 1 addition & 1 deletion artifacts/add/Add.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.14.0",
"version": "v2.14.7",
"name": "Add",
"bytecode": "020412403b4060406e010002040205d34fbb20db1600160100010200000202021605160016015f06160016015fa00016002a16012aa100a000160016010e0dce00010002010304060011d319adf50e1300641600130164170517041603d1a21601160216041605c118010201010003d320f98f621600b0",
"codeHash": "22b684b29b60a12a9094fa221b86cbab12e0547113c8c5806401f7797a9c809b",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/add/DestroyAdd.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.14.0",
"version": "v2.14.7",
"name": "DestroyAdd",
"bytecodeTemplate": "01010300000005{1}0d0c{0}0103",
"fieldsSig": {
Expand Down
2 changes: 1 addition & 1 deletion artifacts/add/Main.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.14.0",
"version": "v2.14.7",
"name": "Main",
"bytecodeTemplate": "0101030001000a{0}17000e0d0e0e160001001818",
"fieldsSig": {
Expand Down
2 changes: 1 addition & 1 deletion artifacts/greeter/Greeter.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.14.0",
"version": "v2.14.7",
"name": "Greeter",
"bytecode": "19010c010000000105030c7bce0002",
"codeHash": "3813cf61a6e0f126463190119cd861a14ca9c2f92839e193c4f9934517b02477",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/greeter/GreeterMain.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.14.0",
"version": "v2.14.7",
"name": "GreeterMain",
"bytecodeTemplate": "01010300020014{0}17000c0d160001000d2f0c7b{0}17010c0d160101000d2f0c7b",
"fieldsSig": {
Expand Down
2 changes: 1 addition & 1 deletion artifacts/nft/DeprecatedNFTTest1.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.14.0",
"version": "v2.14.7",
"name": "DeprecatedNFTTest1",
"bytecode": "02010e010000000103d3ee6a5cd5ce0102",
"codeHash": "cc6928c9c6777077abcb5b9c4f7c5d620d6cae07ec6f00f5e8b0efe6a7b913c4",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/nft/DeprecatedNFTTest2.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.14.0",
"version": "v2.14.7",
"name": "DeprecatedNFTTest2",
"bytecode": "02020e1c010000000103d3ee6a5cd5ce0102010000000103d33e65d93dce0002",
"codeHash": "ade9aee476ee752050a1e9e1b19039f05261cb3f53941152617174faf9eae572",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/nft/DeprecatedNFTTest3.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.14.0",
"version": "v2.14.7",
"name": "DeprecatedNFTTest3",
"bytecode": "02020e19010000000103d3ee6a5cd5ce0102010000000001d34d12f529",
"codeHash": "465bc3739cd1649e58e0470971bd2fabf21363ab9fc2c15052fb2440dd06ada5",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/nft/DeprecatedNFTTest4.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.14.0",
"version": "v2.14.7",
"name": "DeprecatedNFTTest4",
"bytecode": "02020e1b010000000103d3ee6a5cd5ce0102010000000103d35f9a418a0402",
"codeHash": "a5de0fa0b3580303ac63423f09ce5ed95fccbf789679b32130a53c26fef182e9",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/nft/DeprecatedNFTTest5.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.14.0",
"version": "v2.14.7",
"name": "DeprecatedNFTTest5",
"bytecode": "02020e1e010000000103d3ee6a5cd5ce0102010000000305d35c9ec8a3ce000c0c02",
"codeHash": "8b1374f39db98c485af3dd05d0b0ce861e9528f290ca9dc0d7108e8b48b50161",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/nft/DeprecatedNFTTest6.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.14.0",
"version": "v2.14.7",
"name": "DeprecatedNFTTest6",
"bytecode": "02020e1c010000000103d3ee6a5cd5ce0102010000000204d30e0a3ac10c0d02",
"codeHash": "8bc0d39f0607d4a771ec70ae1057b71dbcde404177cb3b25fd7d93d553a2b8cd",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/nft/DeprecatedNFTTest7.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.14.0",
"version": "v2.14.7",
"name": "DeprecatedNFTTest7",
"bytecode": "02020e1d010000000103d3ee6a5cd5ce0102010000000204d3289dd321ce000b02",
"codeHash": "b95c9acf088b090f5d9d34f28ab079cf22b9e53af8ae6864113c71172231ef4c",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/nft/MintNFTTest.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.14.0",
"version": "v2.14.7",
"name": "MintNFTTest",
"bytecodeTemplate": "01010300000015{2}4c0ab413c40de0b6b3a7640000a2{1}0d0d{0}0107184a09b413c40de0b6b3a7640000a2{1}0d0d{0}010418",
"fieldsSig": {
Expand Down
2 changes: 1 addition & 1 deletion artifacts/nft/NFTCollectionTest.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.14.0",
"version": "v2.14.7",
"name": "NFTCollectionTest",
"bytecode": "04050912402d4040408a010000000102ce0102010000000102a0000201000102010f16000001310c7b160040cb17011601c50d7b16010201000203000816010002170216001602410e7b01030105011fd3b8b591eeb41701b1a00016001406414c5048000313046413006417031702160113c40de0b6b3a7640000a2a00040ce00160216030d1601c91704a0000d2aa100160402",
"codeHash": "087f9292bb326a4d39a6fac09928cb25edf2837718f830f3a166a937f8724779",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/nft/NFTCollectionWithRoyaltyTest.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.14.0",
"version": "v2.14.7",
"name": "NFTCollectionWithRoyaltyTest",
"bytecode": "06080912402d40404050405b406c40b6010000000102ce0102010000000102a0000201000102010f16000001310c7b160040cb17011601c50d7b16010201000203000816010002170216001602410e7b0100020201061601ce032c1367102d0201010202000316001601a9010202020008b4ce02450d7b16001601a801030105011fd3b8b591eeb41701b1a00016001406414c5048000313046413006417031702160113c40de0b6b3a7640000a2a00040ce00160216030d1601c91704a0000d2aa100160402",
"codeHash": "919808beff802a8043d62c5ff23249f22dbfdbd2fae56264f5f108af59eb171e",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/nft/NFTTest.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.14.0",
"version": "v2.14.7",
"name": "NFTTest",
"bytecode": "04020914010000000102ce0202010000000203ce00ce0102",
"codeHash": "4897086210869e612d82995b765a447c5319a55a56e8a0c3c07b4d9ca81e15b1",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/nft/WithdrawNFTCollectionTest.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.14.0",
"version": "v2.14.7",
"name": "WithdrawNFTCollectionTest",
"bytecodeTemplate": "01010300000006b4{1}0e0c{0}0106",
"fieldsSig": {
Expand Down
2 changes: 1 addition & 1 deletion artifacts/nft/WrongNFTTest.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.14.0",
"version": "v2.14.7",
"name": "WrongNFTTest",
"bytecode": "04020917010000000102ce0202010000000206040c7bce00ce0102",
"codeHash": "7dd2ed643a98b2a1a52a9b9e536fcdae60d961b583b8109f777d846bfdfcae8d",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/sub/Sub.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.14.0",
"version": "v2.14.7",
"name": "Sub",
"bytecode": "01011e01000202010dd321939f9e05160016015fa00016002a16012ba100a00002",
"codeHash": "3461ebfaca02ad0a3f587a5b67a461c0cbd82d14261407b1d9277ed4ad129234",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/test/Assert.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.14.0",
"version": "v2.14.7",
"name": "Assert",
"bytecode": "000110010000000006d362d460120d0e2f0f7b",
"codeHash": "46dc5e3835be6551dacbf81565912ec67575aa77522312ceed88472817735d6b",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/test/Debug.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.14.0",
"version": "v2.14.7",
"name": "Debug",
"bytecode": "00010b010000000001d38681d619",
"codeHash": "cc0e1966e6847f2c03384e78df1dcd9a0c2b5db1b7c1e901d66e26e2b000eb2c",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/test/InsertIntoMap.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.14.0",
"version": "v2.14.7",
"name": "InsertIntoMap",
"bytecodeTemplate": "01010300020010{2}{3}17011700{1}d10e2ca2{1}160016010f0c{0}0100",
"fieldsSig": {
Expand Down
2 changes: 1 addition & 1 deletion artifacts/test/MapTest.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.14.0",
"version": "v2.14.7",
"name": "MapTest",
"bytecode": "0003404140a64105010303030016d3a9cdcc691600d1a2140a5f5f6d61705f5f305f5f160047441601b11602d202011600d1a2140a5f5f6d61705f5f315f5f16014044b11602d201010100010400402bd3c50ed2bb0c0d0d140a5f5f6d61705f5f305f5f16004744cb1703160301000c0d0d160301011702170116020d2a0c0e0c140a5f5f6d61705f5f305f5f16004744cb010216020d2a0c0e0c140a5f5f6d61705f5f315f5f16014044cb010201000104004025d392ac08660c0d0d140a5f5f6d61705f5f305f5f16004744cb1703160301000c0d0d160301011702170116000d0c140a5f5f6d61705f5f305f5f16004744cb010316000d0c140a5f5f6d61705f5f315f5f16014044cb0103",
"codeHash": "fa153233f4924fc2fd3e98b43209b838e569154481211662acdade8e20bb48c0",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/test/MetaData.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.14.0",
"version": "v2.14.7",
"name": "MetaData",
"bytecode": "000319402c4033010300000006d38d0b3636b4b413c40de0b6b3a7640000a702000200000004b213c40de0b6b3a7640000a90200000000000102",
"codeHash": "af0b3119565ef5f833f7b3514005a6b00ac9957d5a7779765edfc6e6ce6321c5",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/test/OwnerOnly.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.14.0",
"version": "v2.14.7",
"name": "OwnerOnly",
"bytecode": "010111010000000006d3bf853dbdb4ce00450c7b",
"codeHash": "c8ecfd7b7e1f3d0169d80e0abb59702516eeff301d47e0e7be70a631bd9414ca",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/test/RemoveFromMap.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.14.0",
"version": "v2.14.7",
"name": "RemoveFromMap",
"bytecodeTemplate": "01010300000005{1}0d0c{0}0102",
"fieldsSig": {
Expand Down
2 changes: 1 addition & 1 deletion artifacts/test/TemplateArrayVar.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.14.0",
"version": "v2.14.7",
"name": "TemplateArrayVar",
"bytecodeTemplate": "010103000e00408d{1}{2}{3}{4}1703170217011700{6}{7}{8}170617051704{0}18{5}1816030f2f16020e2f1a16010d2f1a16000c2f1a0c7b160016011708170716070c2f16080d2f1a0c7b16021603170a170916090e2f160a0f2f1a0c7b16060e2f16050d2f1a16040c2f1a0c7b0c170b160b0e314c40260c170c160c0e314c1b160b7a0e314d0e2c160c7a0e314d2a78160b0e2c160c2a2f0c7b160c0d2a170c4a21160b0d2a170b4a7fd60c170d160d0f314c11160d7a0f314d102a78160d2f0c7b160d0d2a170d4a2b",
"fieldsSig": {
Expand Down
2 changes: 1 addition & 1 deletion artifacts/test/TestAssert.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.14.0",
"version": "v2.14.7",
"name": "TestAssert",
"bytecodeTemplate": "010103000000040c0c{0}0100",
"fieldsSig": {
Expand Down
2 changes: 1 addition & 1 deletion artifacts/test/UpdateMapValue.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.14.0",
"version": "v2.14.7",
"name": "UpdateMapValue",
"bytecodeTemplate": "01010300000005{1}0d0c{0}0101",
"fieldsSig": {
Expand Down
2 changes: 1 addition & 1 deletion artifacts/test/UpdateUserAccount.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.14.0",
"version": "v2.14.7",
"name": "UpdateUserAccount",
"bytecodeTemplate": "01010300040015{1}{2}{3}{4}17031702170117001600160116021603100c{0}0100{5}0d0c{0}0101",
"fieldsSig": {
Expand Down
2 changes: 1 addition & 1 deletion artifacts/test/UserAccount.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.14.0",
"version": "v2.14.7",
"name": "UserAccount",
"bytecode": "080340284037404d010004040013d388b067e71600ce01410c7b1602ce02410c7b1601a1021603a103160116032aa101010001010003d3185a39e11600a100010000000507d384cc0995a001ce01a002ce02a00302",
"codeHash": "4e9f7eac1b76eaa2268b5af6ebb5640252892dc170aad6c1ee7b639131a55816",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/test/Warnings.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.14.0",
"version": "v2.14.7",
"name": "Warnings",
"bytecode": "02010c010002020002d31bbce4a602",
"codeHash": "873e095edb39cdb4b11b1157003daeacad06d259a938cd270e22b8e89b75feea",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/token/FakeTokenTest.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.14.0",
"version": "v2.14.7",
"name": "FakeTokenTest",
"bytecode": "010509121b4024402f010000000103044d18010000000103044d18010000000103044d18010000000103044d18010000000001d38d0b3636",
"codeHash": "52f971cb44d54a5353e94dc8db991d2726f76760af782e79bd8a66a9b5b294b7",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/token/TokenTest.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.14.0",
"version": "v2.14.7",
"name": "TokenTest",
"bytecode": "050409121b4024010000000102ce0002010000000102ce0102010000000102ce0202010000000102ce0302",
"codeHash": "a2800413eb2c5c23d48068db23df5f8eeaba04653e12c8ed59d589720d96dadd",
Expand Down
2 changes: 1 addition & 1 deletion docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ services:
condition: service_healthy

alephium:
image: alephium/alephium:v2.14.0
image: alephium/alephium:v2.14.7
restart: unless-stopped
ports:
- 19973:19973/tcp
Expand Down
93 changes: 91 additions & 2 deletions packages/web3/src/codec/script-codec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,24 @@ You should have received a copy of the GNU Lesser General Public License
along with the library. If not, see <http://www.gnu.org/licenses/>.
*/

import {
AddressConst,
AssertWithErrorCode,
ByteConst,
CallExternal,
LoadLocal,
U256Const0,
U256Const1,
U256Eq,
StoreLocal
} from './instr-codec'
import { Buffer } from 'buffer/'
import { web3, Fields, FieldsSig, buildScriptByteCode } from '@alephium/web3'
import { randomContractId } from '@alephium/web3-test'
import { Script, bs58, web3, Fields, FieldsSig, buildScriptByteCode } from '@alephium/web3'
import { randomContractId, testAddress } from '@alephium/web3-test'
import { scriptCodec } from './script-codec'
import { Method } from './method-codec'
import { LockupScript } from './lockup-script-codec'
import { DestroyAdd, GreeterMain } from '../../../../artifacts/ts'

describe('Encode & decode scripts', function () {
beforeAll(() => {
Expand Down Expand Up @@ -144,6 +158,64 @@ describe('Encode & decode scripts', function () {
)
})

it('should encode & decode TxScript from the project', () => {
const contractId = randomContractId()
const decodedTestAddress = Buffer.from(bs58.decode(testAddress))
const lockupScript = {
scriptType: decodedTestAddress[0],
script: {
publicKeyHash: decodedTestAddress.slice(1)
}
} as LockupScript
const contractIdByteString = {
length: { mode: 64, rest: Buffer.from(['32']) },
value: Buffer.from(contractId, 'hex')
}

testScript(DestroyAdd.script, { add: contractId, caller: testAddress }, [
{
isPublic: true,
assetModifier: 3,
argsLength: 0,
localsLength: 0,
returnLength: 0,
instrs: [AddressConst(lockupScript), U256Const1, U256Const0, ByteConst(contractIdByteString), CallExternal(3)]
}
])

testScript(GreeterMain.script, { greeterContractId: contractId }, [
{
isPublic: true,
assetModifier: 3,
argsLength: 0,
localsLength: 2,
returnLength: 0,
instrs: [
ByteConst(contractIdByteString),
StoreLocal(0),
U256Const0,
U256Const1,
LoadLocal(0),
CallExternal(0),
U256Const1,
U256Eq,
U256Const0,
AssertWithErrorCode,
ByteConst(contractIdByteString),
StoreLocal(1),
U256Const0,
U256Const1,
LoadLocal(1),
CallExternal(0),
U256Const1,
U256Eq,
U256Const0,
AssertWithErrorCode
]
}
])
})

async function testScriptCode(
scriptCode: string,
fields: Fields = {},
Expand All @@ -156,4 +228,21 @@ describe('Encode & decode scripts', function () {
const encoded = scriptCodec.encode(decoded)
expect(scriptBytecode).toEqual(encoded.toString('hex'))
}

function testScript(script: Script, fields: Fields, methods: Method[]) {
const txScriptBytecode = script.buildByteCodeToDeploy(fields)
const decodedTxScript = scriptCodec.decodeScript(Buffer.from(txScriptBytecode, 'hex'))

expect(scriptCodec.encodeScript(decodedTxScript).toString('hex')).toEqual(txScriptBytecode)

expect(decodedTxScript.methods.length).toEqual(methods.length)
decodedTxScript.methods.map((decodedMethod, index) => {
expect(decodedMethod.isPublic).toEqual(methods[index].isPublic)
expect(decodedMethod.assetModifier).toEqual(methods[index].assetModifier)
expect(decodedMethod.argsLength).toEqual(methods[index].argsLength)
expect(decodedMethod.localsLength).toEqual(methods[index].localsLength)
expect(decodedMethod.returnLength).toEqual(methods[index].returnLength)
expect(decodedMethod.instrs).toEqual(methods[index].instrs)
})
}
})
Loading

0 comments on commit bcca3fc

Please sign in to comment.