1

I 'd like to build a smart contract verification service like etherscan. While trying some verfication test on etherscan, I get to know that deployed contract blockchain bytecode differs from verification bytecode and the different part is cbor encoded metadata hash.

It seems like etherscan only does verification using runtime bytecode when we select solidity(single file) or solidity(multi-part files) option. So I 'd like to distinguish the runtime bytecode and metadata hash for verification. But the metadata hash pattern is different by solidity compiler version. i.e; Some starts with 0xa264, some starts with 0xa265 and some starts with 0xa165 I heard that to properly parse metadata hash, it is needed to use cbor decoding libraries and I think it is cbor module in npm.

The problem is there is a limit to decode encoded data.

I was able to decode data less than 16KB using below code.

const cbor = require("cbor");

const bytecode =
  "a265627a7a72315820fff71e99591635249f5accf8c30eb5471092d1f9968bdd68eafc0de58687dc8e64736f6c63430005100032";

console.log(cbor.decodeAllSync(bytecode));

and got a result like this.

[
  {
    bzzr1: <Buffer ff f7 1e 99 59 16 35 24 9f 5a cc f8 c3 0e b5 47 10 92 d1 f9 96 8b dd 68 ea fc 0d e5 86 87 dc 8e>,
    solc: <Buffer 00 05 10>
  },
  0,
  -19
]

But when I am trying to decode whole bytecode the above code doesn't work.

This is encoded data what I have tried.

608060405234801561001057600080fd5b50604051612be0380380612be08339818101604052602081101561003357600080fd5b5051600180546001600160a01b039283166001600160a01b031991821617918290556000805490911691909216179055612b6e806100726000396000f3fe608060405234801561001057600080fd5b50600436106100885760003560e01c8063a2e74af61161005b578063a2e74af6146100f0578063c9c6539614610118578063e6a4390514610146578063f46901ed1461017457610088565b8063017e7e581461008d578063094b7415146100b15780631e3dd18b146100b9578063574f2ba3146100d6575b600080fd5b61009561019a565b604080516001600160a01b039092168252519081900360200190f35b6100956101a9565b610095600480360360208110156100cf57600080fd5b50356101b8565b6100de6101df565b60408051918252519081900360200190f35b6101166004803603602081101561010657600080fd5b50356001600160a01b03166101e5565b005b6100956004803603604081101561012e57600080fd5b506001600160a01b0381358116916020013516610266565b6100956004803603604081101561015c57600080fd5b506001600160a01b038135811691602001351661062f565b6101166004803603602081101561018a57600080fd5b50356001600160a01b0316610655565b6000546001600160a01b031681565b6001546001600160a01b031681565b600381815481106101c557fe5b6000918252602090912001546001600160a01b0316905081565b60035490565b6001546001600160a01b03163314610244576040805162461bcd60e51b815260206004820181905260248201527f53706163656772696d6553776170466163746f72793a20464f5242494444454e604482015290519081900360640190fd5b600180546001600160a01b0319166001600160a01b0392909216919091179055565b6000816001600160a01b0316836001600160a01b031614156102b95760405162461bcd60e51b815260040180806020018281038252602a815260200180612acb602a913960400191505060405180910390fd5b600080836001600160a01b0316856001600160a01b0316106102dc5783856102df565b84845b90925090506001600160a01b0382166103295760405162461bcd60e51b8152600401808060200182810382526023815260200180612af56023913960400191505060405180910390fd5b6001600160a01b0382811660009081526002602090815260408083208585168452909152902054161561038d5760405162461bcd60e51b8152600401808060200182810382526022815260200180612b186022913960400191505060405180910390fd5b60606040518060200161039f906106d6565b601f1982820381018352601f9091011660408181528251602084810191909120835290519293507fdda46087f37cd4c2135846abab15f32ba8fdf277cc07f584d13736bfbf895b9892918290030190a17fafabcf2dd47e06a477a89e49c03f8ebe8e0a7e94f775b25bbb24227c9d0110b2816040518080602001828103825283818151815260200191508051906020019080838360005b8381101561044e578181015183820152602001610436565b50505050905090810190601f16801561047b5780820380516001836020036101000a031916815260200191505b509250505060405180910390a16000838360405160200180836001600160a01b03166001600160a01b031660601b8152601401826001600160a01b03166001600160a01b031660601b815260140192505050604051602081830303815290604052805190602001209050808251602084016000f56040805163485cc95560e01b81526001600160a01b038781166004830152868116602483015291519297509087169163485cc9559160448082019260009290919082900301818387803b15801561054557600080fd5b505af1158015610559573d6000803e3d6000fd5b505050506001600160a01b0384811660008181526002602081815260408084208987168086529083528185208054978d166001600160a01b031998891681179091559383528185208686528352818520805488168517905560038054600181018255958190527fc2575a0e9e593c00f959f8c92f12db2869c3395a3b0502d05e2516446f71f85b90950180549097168417909655925483519283529082015281517f0d3648bd0f6ba80134a33ba9275ac585d9d315f0ad8355cddefde31afa28d0e9929181900390910190a35050505092915050565b60026020908152600092835260408084209091529082529020546001600160a01b031681565b6001546001600160a01b031633146106b4576040805162461bcd60e51b815260206004820181905260248201527f53706163656772696d6553776170466163746f72793a20464f5242494444454e604482015290519081900360640190fd5b600080546001600160a01b0319166001600160a01b0392909216919091179055565b6123e7806106e48339019056fe60806040526001600c5534801561001557600080fd5b506040514690806052612395823960408051918290036052018220828201825260098352685370616365204c507360b81b6020938401528151808301835260018152603160f81b908401528151808401919091527f20d7b97cc875b9a58b8b41ad4f9518828293a9e94523d656a61f2a1b4aad6cb6818301527fc89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc6606082015260808101949094523060a0808601919091528151808603909101815260c09094019052825192019190912060035550600580546001600160a01b03191633179055612291806101046000396000f3fe608060405234801561001057600080fd5b50600436106101a95760003560e01c80636d9a640a116100f9578063ba9a7a5611610097578063d21220a711610071578063d21220a7146104da578063d505accf146104e2578063dd62ed3e14610533578063fff6cae914610561576101a9565b8063ba9a7a56146104a4578063bc25cf77146104ac578063c45a0155146104d2576101a9565b80637ecebe00116100d35780637ecebe001461040b57806389afcb441461043157806395d89b4114610470578063a9059cbb14610478576101a9565b80636d9a640a146103ab57806370a08231146103dd5780637464fc3d14610403576101a9565b806330adf81f11610166578063485cc95511610140578063485cc955146103455780635909c0d5146103755780635a3d54931461037d5780636a62784214610385576101a9565b806330adf81f14610317578063313ce5671461031f5780633644e5151461033d576101a9565b806306fdde03146101ae5780630902f1ac1461022b578063095ea7b3146102635780630dfe1681146102a357806318160ddd146102c757806323b872dd146102e1575b600080fd5b6101b6610569565b6040805160208082528351818301528351919283929083019185019080838360005b838110156101f05781810151838201526020016101d8565b50505050905090810190601f16801561021d5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b61023361058e565b604080516001600160701b03948516815292909316602083015263ffffffff168183015290519081900360600190f35b61028f6004803603604081101561027957600080fd5b506001600160a01b0381351690602001356105b8565b604080519115158252519081900360200190f35b6102ab6105cf565b604080516001600160a01b039092168252519081900360200190f35b6102cf6105de565b60408051918252519081900360200190f35b61028f600480360360608110156102f757600080fd5b506001600160a01b038135811691602081013590911690604001356105e4565b6102cf61067e565b6103276106a2565b6040805160ff9092168252519081900360200190f35b6102cf6106a7565b6103736004803603604081101561035b57600080fd5b506001600160a01b03813581169160200135166106ad565b005b6102cf61073a565b6102cf610740565b6102cf6004803603602081101561039b57600080fd5b50356001600160a01b0316610746565b610373600480360360608110156103c157600080fd5b50803590602081013590604001356001600160a01b0316610a4c565b6102cf600480360360208110156103f357600080fd5b50356001600160a01b0316610ec9565b6102cf610edb565b6102cf6004803603602081101561042157600080fd5b50356001600160a01b0316610ee1565b6104576004803603602081101561044757600080fd5b50356001600160a01b0316610ef3565b6040805192835260208301919091528051918290030190f35b6101b6611293565b61028f6004803603604081101561048e57600080fd5b506001600160a01b0381351690602001356112b2565b6102cf6112bf565b610373600480360360208110156104c257600080fd5b50356001600160a01b03166112c5565b6102ab61142a565b6102ab611439565b610373600480360360e08110156104f857600080fd5b506001600160a01b03813581169160208101359091169060408101359060608101359060ff6080820135169060a08101359060c00135611448565b6102cf6004803603604081101561054957600080fd5b506001600160a01b038135811691602001351661163f565b61037361165c565b604051806040016040528060098152602001685370616365204c507360b81b81525081565b6008546001600160701b0380821692600160701b830490911691600160e01b900463ffffffff1690565b60006105c53384846117b8565b5060015b92915050565b6006546001600160a01b031681565b60005481565b6001600160a01b038316600090815260026020908152604080832033845290915281205460001914610669576001600160a01b0384166000908152600260209081526040808320338452909152902054610644908363ffffffff61181a16565b6001600160a01b03851660009081526002602090815260408083203384529091529020555b610674848484611863565b5060019392505050565b7f6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c981565b601281565b60035481565b6005546001600160a01b0316331461070c576040805162461bcd60e51b815260206004820152601d60248201527f53706163656772696d6553776170506169723a20464f5242494444454e000000604482015290519081900360640190fd5b600680546001600160a01b039384166001600160a01b03199182161790915560078054929093169116179055565b60095481565b600a5481565b6000600c5460011461078d576040805162461bcd60e51b815260206004820152601a60248201526000805160206121e5833981519152604482015290519081900360640190fd5b6000600c8190558061079d61058e565b50600654604080516370a0823160e01b815230600482015290519395509193506000926001600160a01b03909116916370a08231916024808301926020929190829003018186803b1580156107f157600080fd5b505afa158015610805573d6000803e3d6000fd5b505050506040513d602081101561081b57600080fd5b5051600754604080516370a0823160e01b815230600482015290519293506000926001600160a01b03909216916370a0823191602480820192602092909190829003018186803b15801561086e57600080fd5b505afa158015610882573d6000803e3d6000fd5b505050506040513d602081101561089857600080fd5b5051905060006108b7836001600160701b03871663ffffffff61181a16565b905060006108d4836001600160701b03871663ffffffff61181a16565b905060006108e2878761191d565b6000549091508061092b576109176103e861090b610906878763ffffffff611a7b16565b611ad4565b9063ffffffff61181a16565b985061092660006103e8611b26565b61097a565b6109776001600160701b038916610948868463ffffffff611a7b16565b8161094f57fe5b046001600160701b03891661096a868563ffffffff611a7b16565b8161097157fe5b04611bbc565b98505b600089116109b95760405162461bcd60e51b815260040180806020018281038252603181526020018061214a6031913960400191505060405180910390fd5b6109c38a8a611b26565b6109cf86868a8a611bd2565b81156109ff576008546109fb906001600160701b0380821691600160701b90041663ffffffff611a7b16565b600b555b6040805185815260208101859052815133927f4c209b5fc8ad50758f13e2e1088ba56a560dff690a1c6fef26394f4c03821c4f928290030190a250506001600c5550949695505050505050565b600c54600114610a91576040805162461bcd60e51b815260206004820152601a60248201526000805160206121e5833981519152604482015290519081900360640190fd5b6000600c5582151580610aa45750600082115b610adf5760405162461bcd60e51b815260040180806020018281038252602e81526020018061222f602e913960400191505060405180910390fd5b600080610aea61058e565b5091509150816001600160701b031685108015610b0f5750806001600160701b031684105b610b4a5760405162461bcd60e51b815260040180806020018281038252602a815260200180612205602a913960400191505060405180910390fd5b60065460075460009182916001600160a01b03918216919081169087168214801590610b885750806001600160a01b0316876001600160a01b031614155b610bd9576040805162461bcd60e51b815260206004820152601e60248201527f53706163656772696d6553776170506169723a20494e56414c49445f544f0000604482015290519081900360640190fd5b8815610bea57610bea82888b611da1565b8715610bfb57610bfb81888a611da1565b604080516370a0823160e01b815230600482015290516001600160a01b038416916370a08231916024808301926020929190829003018186803b158015610c4157600080fd5b505afa158015610c55573d6000803e3d6000fd5b505050506040513d6020811015610c6b57600080fd5b5051604080516370a0823160e01b815230600482015290519195506001600160a01b038316916370a0823191602480820192602092909190829003018186803b158015610cb757600080fd5b505afa158015610ccb573d6000803e3d6000fd5b505050506040513d6020811015610ce157600080fd5b5051925060009150506001600160701b0385168890038311610d04576000610d13565b87856001600160701b03160383035b9050600087856001600160701b0316038311610d30576000610d3f565b87856001600160701b03160383035b90506000821180610d505750600081115b610d8b5760405162461bcd60e51b815260040180806020018281038252602d81526020018061211d602d913960400191505060405180910390fd5b6000610db3610da184600363ffffffff611a7b16565b61090b876103e863ffffffff611a7b16565b90506000610dcb610da184600363ffffffff611a7b16565b9050610dfc620f4240610df06001600160701b038b8116908b1663ffffffff611a7b16565b9063ffffffff611a7b16565b610e0c838363ffffffff611a7b16565b1015610e57576040805162461bcd60e51b815260206004820152601560248201527453706163656772696d6553776170506169723a204b60581b604482015290519081900360640190fd5b5050610e6584848888611bd2565b60408051838152602081018390528082018b9052606081018a905290516001600160a01b0389169133917fd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d8229181900360800190a350506001600c5550505050505050565b60016020526000908152604090205481565b600b5481565b60046020526000908152604090205481565b600080600c54600114610f3b576040805162461bcd60e51b815260206004820152601a60248201526000805160206121e5833981519152604482015290519081900360640190fd5b6000600c81905580610f4b61058e565b50600654600754604080516370a0823160e01b815230600482015290519496509294506001600160a01b039182169391169160009184916370a08231916024808301926020929190829003018186803b158015610fa757600080fd5b505afa158015610fbb573d6000803e3d6000fd5b505050506040513d6020811015610fd157600080fd5b5051604080516370a0823160e01b815230600482015290519192506000916001600160a01b038516916370a08231916024808301926020929190829003018186803b15801561101f57600080fd5b505afa158015611033573d6000803e3d6000fd5b505050506040513d602081101561104957600080fd5b505130600090815260016020526040812054919250611068888861191d565b6000549091508061107f848763ffffffff611a7b16565b8161108657fe5b049a508061109a848663ffffffff611a7b16565b816110a157fe5b04995060008b1180156110b4575060008a115b6110ef5760405162461bcd60e51b81526004018080602001828103825260318152602001806120ec6031913960400191505060405180910390fd5b6110f93084611f25565b611104878d8d611da1565b61110f868d8c611da1565b604080516370a0823160e01b815230600482015290516001600160a01b038916916370a08231916024808301926020929190829003018186803b15801561115557600080fd5b505afa158015611169573d6000803e3d6000fd5b505050506040513d602081101561117f57600080fd5b5051604080516370a0823160e01b815230600482015290519196506001600160a01b038816916370a0823191602480820192602092909190829003018186803b1580156111cb57600080fd5b505afa1580156111df573d6000803e3d6000fd5b505050506040513d60208110156111f557600080fd5b5051935061120585858b8b611bd2565b811561123557600854611231906001600160701b0380821691600160701b90041663ffffffff611a7b16565b600b555b604080518c8152602081018c905281516001600160a01b038f169233927fdccd412f0b1252819cb1fd330b93224ca42612892bb3f4f789976e6d81936496929081900390910190a35050505050505050506001600c81905550915091565b604051806040016040528060038152602001620534c560ec1b81525081565b60006105c5338484611863565b6103e881565b600c5460011461130a576040805162461bcd60e51b815260206004820152601a60248201526000805160206121e5833981519152604482015290519081900360640190fd5b6000600c55600654600754600854604080516370a0823160e01b815230600482015290516001600160a01b0394851694909316926113b992859287926113b4926001600160701b03169185916370a0823191602480820192602092909190829003018186803b15801561137c57600080fd5b505afa158015611390573d6000803e3d6000fd5b505050506040513d60208110156113a657600080fd5b50519063ffffffff61181a16565b611da1565b600854604080516370a0823160e01b8152306004820152905161142092849287926113b492600160701b90046001600160701b0316916001600160a01b038616916370a0823191602480820192602092909190829003018186803b15801561137c57600080fd5b50506001600c5550565b6005546001600160a01b031681565b6007546001600160a01b031681565b4284101561149d576040805162461bcd60e51b815260206004820152601c60248201527f53706163656772696d655377617042455032303a204558504952454400000000604482015290519081900360640190fd5b6003546001600160a01b0380891660008181526004602090815260408083208054600180820190925582517f6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c98186015280840196909652958d166060860152608085018c905260a085019590955260c08085018b90528151808603909101815260e08501825280519083012061190160f01b6101008601526101028501969096526101228085019690965280518085039096018652610142840180825286519683019690962095839052610162840180825286905260ff89166101828501526101a284018890526101c28401879052519193926101e280820193601f1981019281900390910190855afa1580156115b8573d6000803e3d6000fd5b5050604051601f1901519150506001600160a01b038116158015906115ee5750886001600160a01b0316816001600160a01b0316145b6116295760405162461bcd60e51b815260040180806020018281038252602681526020018061217b6026913960400191505060405180910390fd5b6116348989896117b8565b505050505050505050565b600260209081526000928352604080842090915290825290205481565b600c546001146116a1576040805162461bcd60e51b815260206004820152601a60248201526000805160206121e5833981519152604482015290519081900360640190fd5b6000600c55600654604080516370a0823160e01b815230600482015290516117b1926001600160a01b0316916370a08231916024808301926020929190829003018186803b1580156116f257600080fd5b505afa158015611706573d6000803e3d6000fd5b505050506040513d602081101561171c57600080fd5b5051600754604080516370a0823160e01b815230600482015290516001600160a01b03909216916370a0823191602480820192602092909190829003018186803b15801561176957600080fd5b505afa15801561177d573d6000803e3d6000fd5b505050506040513d602081101561179357600080fd5b50516008546001600160701b0380821691600160701b900416611bd2565b6001600c55565b6001600160a01b03808416600081815260026020908152604080832094871680845294825291829020859055815185815291517f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b9259281900390910190a3505050565b600061185c83836040518060400160405280601e81526020017f536166654d6174683a207375627472616374696f6e206f766572666c6f770000815250611fc3565b9392505050565b6001600160a01b03831660009081526001602052604090205461188c908263ffffffff61181a16565b6001600160a01b0380851660009081526001602052604080822093909355908416815220546118c1908263ffffffff61205a16565b6001600160a01b0380841660008181526001602090815260409182902094909455805185815290519193928716927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef92918290030190a3505050565b600080600560009054906101000a90046001600160a01b03166001600160a01b031663017e7e586040518163ffffffff1660e01b815260040160206040518083038186803b15801561196e57600080fd5b505afa158015611982573d6000803e3d6000fd5b505050506040513d602081101561199857600080fd5b5051600b546001600160a01b038216158015945091925090611a67578015611a625760006119db6109066001600160701b0388811690881663ffffffff611a7b16565b905060006119e883611ad4565b905080821115611a5f576000611a16611a07848463ffffffff61181a16565b6000549063ffffffff611a7b16565b90506000611a3b83611a2f86600563ffffffff611a7b16565b9063ffffffff61205a16565b90506000818381611a4857fe5b0490508015611a5b57611a5b8782611b26565b5050505b50505b611a73565b8015611a73576000600b555b505092915050565b600082611a8a575060006105c9565b82820282848281611a9757fe5b041461185c5760405162461bcd60e51b81526004018080602001828103825260218152602001806121a16021913960400191505060405180910390fd5b60006003821115611b17575080600160028204015b81811015611b1157809150600281828581611b0057fe5b040181611b0957fe5b049050611ae9565b50611b21565b8115611b21575060015b919050565b600054611b39908263ffffffff61205a16565b60009081556001600160a01b038316815260016020526040902054611b64908263ffffffff61205a16565b6001600160a01b03831660008181526001602090815260408083209490945583518581529351929391927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9281900390910190a35050565b6000818310611bcb578161185c565b5090919050565b6001600160701b038411801590611bf057506001600160701b038311155b611c41576040805162461bcd60e51b815260206004820152601c60248201527f53706163656772696d6553776170506169723a204f564552464c4f5700000000604482015290519081900360640190fd5b60085463ffffffff42811691600160e01b90048116820390811615801590611c7157506001600160701b03841615155b8015611c8557506001600160701b03831615155b15611cf6578063ffffffff16611cb385611c9e866120b4565b6001600160e01b03169063ffffffff6120c616565b600980546001600160e01b03929092169290920201905563ffffffff8116611cde84611c9e876120b4565b600a80546001600160e01b0392909216929092020190555b600880546dffffffffffffffffffffffffffff19166001600160701b03888116919091176dffffffffffffffffffffffffffff60701b1916600160701b8883168102919091176001600160e01b0316600160e01b63ffffffff871602179283905560408051848416815291909304909116602082015281517f1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1929181900390910190a1505050505050565b604080518082018252601981527f7472616e7366657228616464726573732c75696e74323536290000000000000060209182015281516001600160a01b0385811660248301526044808301869052845180840390910181526064909201845291810180516001600160e01b031663a9059cbb60e01b1781529251815160009460609489169392918291908083835b60208310611e4e5780518252601f199092019160209182019101611e2f565b6001836020036101000a0380198251168184511680821785525050505050509050019150506000604051808303816000865af19150503d8060008114611eb0576040519150601f19603f3d011682016040523d82523d6000602084013e611eb5565b606091505b5091509150818015611ee3575080511580611ee35750808060200190516020811015611ee057600080fd5b50515b611f1e5760405162461bcd60e51b81526004018080602001828103825260238152602001806121c26023913960400191505060405180910390fd5b5050505050565b6001600160a01b038216600090815260016020526040902054611f4e908263ffffffff61181a16565b6001600160a01b03831660009081526001602052604081209190915554611f7b908263ffffffff61181a16565b60009081556040805183815290516001600160a01b038516917fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef919081900360200190a35050565b600081848411156120525760405162461bcd60e51b81526004018080602001828103825283818151815260200191508051906020019080838360005b83811015612017578181015183820152602001611fff565b50505050905090810190601f1680156120445780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b505050900390565b60008282018381101561185c576040805162461bcd60e51b815260206004820152601b60248201527f536166654d6174683a206164646974696f6e206f766572666c6f770000000000604482015290519081900360640190fd5b6001600160701b0316600160701b0290565b60006001600160701b0382166001600160e01b038416816120e357fe5b04939250505056fe53706163656772696d6553776170506169723a20494e53554646494349454e545f4c49515549444954595f4255524e454453706163656772696d6553776170506169723a20494e53554646494349454e545f494e5055545f414d4f554e5453706163656772696d6553776170506169723a20494e53554646494349454e545f4c49515549444954595f4d494e54454453706163656772696d655377617042455032303a20494e56414c49445f5349474e4154555245536166654d6174683a206d756c7469706c69636174696f6e206f766572666c6f7753706163656772696d6553776170506169723a205452414e534645525f4641494c454453706163656772696d6553776170506169723a204c4f434b454400000000000053706163656772696d6553776170506169723a20494e53554646494349454e545f4c495155494449545953706163656772696d6553776170506169723a20494e53554646494349454e545f4f55545055545f414d4f554e54a265627a7a723158205a4c63b733a19a608e83725aab7de48db6a29ae55c2ac3d587e23e777f8598b564736f6c63430005100032454950373132446f6d61696e28737472696e67206e616d652c737472696e672076657273696f6e2c75696e7432353620636861696e49642c6164647265737320766572696679696e67436f6e74726163742953706163656772696d6553776170466163746f72793a204944454e544943414c5f41444452455353455353706163656772696d6553776170466163746f72793a205a45524f5f4144445245535353706163656772696d6553776170466163746f72793a20504149525f455849535453a265627a7a7231582014f652fc9ac07d61b1bef5f39f732f161e9e9f9d899c8811b8c7ef6109fb3c7c64736f6c63430005100032000000000000000000000000a3c84058503a4226f637c285beeaf6d424b1b0ec

This encoded data size is 23KB.

To sum up,

  1. Am I right to distinguish runtime bytecode and metadata hash like this?
  2. If right, how to distinguish runtime bytecode and metadata hash and if not right, how to get runtime bytecode for all solidity versions?

1 Answer 1

2

The bytecode is not CBOR-encoded as a whole. Only the part representing the metadata hash at the end is and you seem to be decoding it correctly. As to why it can start with 0xa264, 0xa265 or something else see my earlier answer for "How to verify smart contracts on different solidity versions".

Am I right to distinguish runtime bytecode and metadata hash like this

Well, that's the approach you'll have to take to be able to verify some contracts (ones with flattened source or produced by early versions of the compiler) but it's best if you can verify the whole bytecode, including the metadata hash. To reproduce it to that level you should require the user submit either the Standard JSON input used to build the contract or sources+complete metadata (the JSON data the hash of which is appended to the bytecode). Verification that ignores metadata is not as strict and will happily accept source modified in many ways - having renamed contracts or variables, changing parts that get optimized out, with altered comments, licensing info, etc. See Contract Metadata > Usage for Source Code Verification.

If right, how to distinguish runtime bytecode and metadata hash and if not right, how to get runtime bytecode for all solidity versions?

I'm not sure if by "runtime bytecode" you actually mean the runtime bytecode as opposed to creation bytecode or just creation code, which is what you get from the compiler. I'm assuming you mean the latter because the actual runtime code is dynamically generated and in a general case you have to execute the creation code to obtain it.

If you mean creation code then that everything except the CBOR-encoded metadata hashes at the end. If we're talking about bytecode you get from transaction data (and not compiler output) there are also constructor parameters appended after the CBOR data.

2
  • In the answer of this article, you mentioned how to get starting point of metadata hash like this. Use a heuristic to detect where it starts but then use a proper CBOR parser to decode it. Can you explain in more detail what the "heuristic" means and how can I achieve this?
    – kimiro
    Oct 23, 2021 at 16:41
  • 1
    Basically, just encode the possible keys I listed in that answer as CBOR and look for the patterns they create in the bytecode to find the beginning of the metadata. For for example 0xa265627a7a7231 would be one of the patterns but not the only one. Try decoding at all of those places and check if they're valid CBOR. Hopefully you get only one that's valid. If not, you could try limiting the search to something like the top 1kB of the bytecode.
    – cameel
    Oct 24, 2021 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.