You are viewing a single comment's thread from:

RE: Test title

in #topic15 years ago (edited)

{
"id": "ssc-testnet1",
"json": {
"contractName": "contract",
"contractAction": "update",
"contractPayload": {
"name": "witnesses",
"params": "",
"code": "LyogZXNsaW50LWRpc2FibGUgbm8tYXdhaXQtaW4tbG9vcCAqLwovKiBnbG9iYWwgYWN0aW9ucywgYXBpICovCgpjb25zdCBOQl9BUFBST1ZBTFNfQUxMT1dFRCA9IDMwOwpjb25zdCBOQl9UT1BfV0lUTkVTU0VTID0gNDsKY29uc3QgTkJfQkFDS1VQX1dJVE5FU1NFUyA9IDE7CmNvbnN0IE5CX1dJVE5FU1NFUyA9IE5CX1RPUF9XSVRORVNTRVMgKyBOQl9CQUNLVVBfV0lUTkVTU0VTOwpjb25zdCBOQl9XSVRORVNTRVNfU0lHTkFUVVJFU19SRVFVSVJFRCA9IDM7CmNvbnN0IE1BWF9ST1VORFNfTUlTU0VEX0lOX0FfUk9XID0gMzsgLy8gYWZ0ZXIgdGhhdCB0aGUgd2l0bmVzcyBpcyBkaXNhYmxlZApjb25zdCBNQVhfUk9VTkRfUFJPUE9TSVRJT05fV0FJVElOR19QRVJJT0QgPSA0MDsgLy8gMjAgYmxvY2tzCmNvbnN0IE5CX1RPS0VOU19UT19SRVdBUkQgPSAnMC4wMTkwMjU4Nyc7CmNvbnN0IE5CX1RPS0VOU19ORUVERURfQkVGT1JFX1JFV0FSRElORyA9ICcwLjA5NTEyOTM1JzsKLy8gZXNsaW50LWRpc2FibGUtbmV4dC1saW5lIG5vLXRlbXBsYXRlLWN1cmx5LWluLXN0cmluZwpjb25zdCBVVElMSVRZX1RPS0VOX1NZTUJPTCA9ICJTU0MiOwovLyBlc2xpbnQtZGlzYWJsZS1uZXh0LWxpbmUgbm8tdGVtcGxhdGUtY3VybHktaW4tc3RyaW5nCmNvbnN0IFVUSUxJVFlfVE9LRU5fUFJFQ0lTSU9OID0gODsKLy8gZXNsaW50LWRpc2FibGUtbmV4dC1saW5lIG5vLXRlbXBsYX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gICAgICB3aXRuZXNzLnNpZ25pbmdLZXkgPSBzaWduaW5nS2V5OwogICAgICAgICAgd2l0bmVzcy5lbmFibGVkID0gZW5hYmxlZDsKICAgICAgICAgIGF3YWl0IGFwaS5kYi51cGRhdGUoJ3dpdG5lc3NlcycsIHdpdG5lc3MpOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICB3aXRuZXNzID0gewogICAgICAgICAgICBhY2NvdW50OiBhcGkuc2VuZGVyLAogICAgICAgICAgICBhcHByb3ZhbFdlaWdodDogeyAkbnVtYmVyRGVjaW1hbDogJzAnIH0sCiAgICAgICAgICAgIHNpZ25pbmdLZXksCiAgICAgICAgICAgIElQLAogICAgICAgICAgICBSUENQb3J0LAogICAgICAgICAgICBQMlBQb3J0LAogICAgICAgICAgICBlbmFibGVkLAogICAgICAgICAgICBtaXNzZWRSb3VuZHM6IDAsCiAgICAgICAgICAgIG1pc3NlZFJvdW5kc0luQVJvdzogMCwKICAgICAgICAgICAgdmVyaWZpZWRSb3VuZHM6IDAsCiAgICAgICAgICAgIGxhc3RSb3VuZFZlcmlmaWVkOiBudWxsLAogICAgICAgICAgICBsYXN0QmxvY2tWZXJpZmllZDogbnVsbCwKICAgICAgICAgIH07CiAgICAgICAgICBhd2FpdCBhcGkuZGIuaW5zZXJ0KCd3aXRuZXNzZXMnLCB3aXRuZXNzKTsKICAgICAgICB9CiAgICAgIH0KICAgIH0KICB9Cn07CgphY3Rpb25zLmFwcHJvdmUgPSBhc3luYyAocGF5bG9hZCkgPT4gewogIGNvbnN0IHsgd2l0bmVzcyB9ID0gcGF5bG9hZDsKCiAgaWYgKGFwaS5hc3NlcnQod2l0bmVzcyAmJiB0eXBlb2Ygd2l0bmVzcyA9PT0gJ3N0cmluZycgJiYgd2l0bmVzcy5sZW5ndGggPj0gMyAmJiB3aXRuZXNzLmxlbmd0aCA8PSAxNiwgJ2ludmFsaWQgd2l0bmVzcyBhY2NvdW50JykpIHsKICAgIC8vIGNoZWNrIGlmIHdpdG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1iZXIodG90YWxBcHByb3ZhbFdlaWdodCkKICAgICAgICAgICAgICAubWludXMobWluKQogICAgICAgICAgICAgIC50aW1lcyhyYW5kb20pCiAgICAgICAgICAgICAgLnBsdXMobWluKQogICAgICAgICAgICAgIC50b0ZpeGVkKFVUSUxJVFlfVE9LRU5fUFJFQ0lTSU9OKTsKICAgICAgICAgIH0KCiAgICAgICAgICBhY2NXZWlnaHQgPSBhcGkuQmlnTnVtYmVyKGFjY1dlaWdodCkKICAgICAgICAgICAgLnBsdXMod2l0bmVzcy5hcHByb3ZhbFdlaWdodC4kbnVtYmVyRGVjaW1hbCkKICAgICAgICAgICAgLnRvRml4ZWQoVVRJTElUWV9UT0tFTl9QUkVDSVNJT04pOwoKICAgICAgICAgIC8vIGlmIHRoZSB3aXRuZXNzIGlzIGVuYWJsZWQKICAgICAgICAgIGlmICh3aXRuZXNzLmVuYWJsZWQgPT09IHRydWUpIHsKICAgICAgICAgICAgLy8gaWYgd2UgaGF2ZW4ndCBmb3VuZCBhbGwgdGhlIHRvcCB3aXRuZXNzZXMgeWV0CiAgICAgICAgICAgIGlmIChzY2hlZHVsZS5sZW5ndGggPCBOQl9UT1BfV0lUTkVTU0VTCiAgICAgICAgICAgICAgfHwgYXBpLkJpZ051bWJlcihyYW5kb21XZWlnaHQpLmx0ZShhY2NXZWlnaHQpKSB7CiAgICAgICAgICAgICAgc2NoZWR1bGUucHVzaCh7CiAgICAgICAgICAgICAgICB3aXRuZXNzOiB3aXRuZXNzLmFjY291bnQsCiAgICAgICAgICAgICAgICBibG9ja051bWJlcjogbnVsbCwKICAgICAgICAgICAgICB9KTsKICAgICAgICAgICAgfQogICAgICAgICAgfQoKICAgICAgICAgIGlmIChzY2hlZHVsZS5sZW5ndGggPj0gTkJfV0lUTkVTU0VTKSB7CiAgICAgICAgICAgIGluZGV4ID0gd2l0bmVzc2VzLmxlbmd0aDsKICAgICAgICAgIH0KICAgICAgICB9CgogICAgICAgIGlmIChzY2hlZHVsZS5sZW5ndGggPCBOQl9XSVRORVNTRVMpIHsKICAgICAgICAgIG9mZnNldCArPSAxMDA7CiAgICAgICAgICB3aXRuZXNzZXMgPSBhd2FpdCBhcGkuZGIuZmluZCgKICAgICAgICAgICAgJ3dpdG5lc3NlcycsCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICBhcHByb3ZhbFdlaWdodDogewogICAgICAgICAgICAgICAgJGd0OiB7CiAgICAgICAgICAgICAgICAgICRudW1iZXJEZWNpbWFsOiAnMCcsCiAgICAgICAgICAgICAgICB9LAogICAgICAgICAgICAgIH0sCiAgICAgICAgICAgIH0sCiAgICAgICAgICAgIDEwMCwgLy8gbGltaXQKICAgICAgICAgICAgb2Zmc2V0LCAvLyBvZmZzZXQKICAgICAgICAgICAgWwogICAgICAgICAgICAgIHsgaW5kZXg6ICdhcHByb3ZhbFdlaWdodCcsIGRlc2NlbmRpbmc6IHRydWUgfSwKICAgICAgICAgICAgXSwKICAgICAgICAgICk7CiAgICAgICAgfQogICAgICB9IHdoaWxlICh3aXRuZXNzZXMubGVuZ3RoID4gMCAmJiBzY2hlZHVsZS5sZW5ndGggPCBOQl9XSVRORVNTRVMpOwogICAgfQoKICAgIC8vIGlmIHRoZXJlIGFyZSBlbm91Z2ggd2l0bmVzc2VzIHNjaGVkdWxlZAogICAgaWYgKHNjaGVkdWxlLmxlbmd0aCA9PT0gTkJfV0lUTkVTU0VTKSB7CiAgICAgIC8vIHNodWZmbGUgdGhlIHdpdG5lc3NlcwogICAgICBsZXQgajsgbGV0IHg7CiAgICAgIGZvciAobGV0IGkgPSBzY2hlZHVsZS5sZW5ndGggLSAxOyBpID4gMDsgaSAtPSAxKSB7CiAgICAgICAgY29uc3QgcmFuZG9tID0gYXBpLnJhbmRvbSgpOwogICAgICAgIGogPSBNYXRoLmZsb29yKHJhbmRvbSAqIChpICsgMSkpOwogICAgICAgIHggPSBzY2hlZHVsZVtpXTsKICAgICAgICBzY2hlZHVsZVtpXSA9IHNjaGVkdWxlW2pdOwogICA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0bmVzcy5hY2NvdW50KTsKICAgICAgICAgICAgaWYgKHNpZ25hdHVyZSkgewogICAgICAgICAgICAgIGlmIChhcGkuY2hlY2tTaWduYXR1cmUoCiAgICAgICAgICAgICAgICBjYWxjdWxhdGVkUm91bmRIYXNoLCBzaWduYXR1cmVbMV0sIHdpdG5lc3Muc2lnbmluZ0tleSwgdHJ1ZSwKICAgICAgICAgICAgICApKSB7CiAgICAgICAgICAgICAgICBhcGkuZGVidWcoYHdpdG5lc3MgJHt3aXRuZXNzLmFjY291bnR9IHNpZ25lZCByb3VuZCAke3JvdW5kfWApOwogICAgICAgICAgICAgICAgc2lnbmF0dXJlc0NoZWNrZWQgKz0gMTsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KCiAgICAgICAgICAgIC8vIHRoZSBjdXJyZW50IHdpdG5lc3Mgd2lsbCBzaG93IGFzIHRoZSB3aXRuZXNzIHRoYXQgdmVyaWZpZWQgdGhlIGJsb2NrcyBmcm9tIHRoZSByb3VuZAogICAgICAgICAgICB2ZXJpZmllZEJsb2NrSW5mb3JtYXRpb24ucHVzaCgKICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBibG9ja051bWJlcjogc2NoZWR1bGVkV2l0bmVzcy5ibG9ja051bWJlciwKICAgICAgICAgICAgICAgIHdpdG5lc3M6IGN1cnJlbnRXaXRuZXNzLAogICAgICAgICAgICAgICAgc2lnbmluZ0tleTogY3VycmVudFdpdG5lc3NJbmZvLnNpZ25pbmdLZXksCiAgICAgICAgICAgICAgICByb3VuZFNpZ25hdHVyZTogY3VycmVudFdpdG5lc3NTaWduYXR1cmVbMV0sCiAgICAgICAgICAgICAgICByb3VuZCwKICAgICAgICAgICAgICAgIHJvdW5kSGFzaCwKICAgICAgICAgICAgICB9LAogICAgICAgICAgICApOwogICAgICAgICAgfQogICAgICAgIH0KCiAgICAgICAgaWYgKHNpZ25hdHVyZXNDaGVja2VkID49IE5CX1dJVE5FU1NFU19TSUdOQVRVUkVTX1JFUV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