Files
admin 3ad3f30929 docs(tools): 国密链真链复现脚本与证据入库,避免散落 /tmp 后丢失
- 新增 tools/fisco-realnode/:真链实证期用过的 Python SDK 发送器
  (fj-sdk/)、Java JNI 独立交叉验证探针 (jni/)、TARS 编解码辅助 (tars/),
  作为审计轨迹留档,便于回看签名口径根因。
- 新增 example.chain.json(非密钥,可入库)与 README.md 索引:明确
  规范入口 (selftest-fisco-contract.py 主门禁第22条) 与审计轨迹的分工、
  复现所需环境变量、下载项、密钥与诚实边界。
- .gitignore 屏蔽私钥 (*.pem/*.key)、下载型二进制 (jar/tar.gz) 与大证据
  blob,防止密钥或体积大的文件进 git 历史。
2026-11-08 14:53:51 +08:00

115 lines
3.6 KiB
Python

#!/usr/bin/env python3
"""按 TARS/JCE 规则解码 FISCO TransactionData 字节,并对照 oracle 样本。"""
import json, sys
# TARS 类型枚举(TarsStruct.cs 权威)
BYTE, SHORT, INT, LONG, FLOAT, DOUBLE = 0, 1, 2, 3, 4, 5
STRING1, STRING4, MAP, LIST = 6, 7, 8, 9
STRUCT_BEGIN, STRUCT_END, ZERO_TAG, SIMPLE_LIST = 10, 11, 12, 13
TN = {0:'BYTE',1:'SHORT',2:'INT',3:'LONG',4:'FLOAT',5:'DOUBLE',6:'STRING1',7:'STRING4',
8:'MAP',9:'LIST',10:'STRUCT_BEGIN',11:'STRUCT_END',12:'ZERO',13:'SIMPLE_LIST'}
FIELDS = {
1: ('version','int'), 2: ('chainID','string'), 3: ('groupID','string'),
4: ('blockLimit','long'), 5: ('nonce','string'), 6: ('to','string'),
7: ('input','bytes'), 8: ('abi','string'), 9: ('value','string'),
10: ('gasPrice','string'), 11: ('gasLimit','long'),
12: ('maxFeePerGas','string'), 13: ('maxPriorityFeePerGas','string'),
14: ('extension','bytes'),
}
class R:
def __init__(self, b): self.b = b; self.p = 0
def u8(self):
v = self.b[self.p]; self.p += 1; return v
def u16(self):
v = int.from_bytes(self.b[self.p:self.p+2],'big'); self.p += 2; return v
def u32(self):
v = int.from_bytes(self.b[self.p:self.p+4],'big'); self.p += 4; return v
def raw(self, n):
v = self.b[self.p:self.p+n]; self.p += n; return v
def read_head(r):
"""返回 (tag, type)。tag<15: 1 字节 (tag<<4)|type; 15<=tag<256: 0xF0|type + 1 字节 tag。"""
h = r.u8()
typ = h & 0xF
tag = h >> 4
if tag == 15:
tag = r.u8()
return tag, typ
def read_value(r, typ):
if typ == BYTE:
v = r.u8(); return v
if typ == ZERO_TAG:
return 0
if typ == SHORT:
return r.u16()
if typ == INT:
return r.u32()
if typ == LONG:
return int.from_bytes(r.raw(8),'big')
if typ == FLOAT:
return r.raw(4).hex()
if typ == DOUBLE:
return r.raw(8).hex()
if typ == STRING1:
n = r.u8(); return r.raw(n)
if typ == STRING4:
n = r.u32(); return r.raw(n)
if typ == SIMPLE_LIST:
# head(type,0) then Write(len,0)
t2, ty2 = read_head(r) # 元素类型头 (tag=0)
n = read_int_sized(r, ty2)
return ('list', ty2, r.raw(n))
raise ValueError(f'unhandled type {typ}')
def read_int_sized(r, typ):
if typ == BYTE: return r.u8()
if typ == ZERO_TAG: return 0
if typ == SHORT: return r.u16()
if typ == INT: return r.u32()
if typ == LONG: return int.from_bytes(r.raw(8),'big')
raise ValueError(typ)
def decode_fields(buf):
r = R(buf)
out = []
while r.p < len(buf):
tag, typ = read_head(r)
if typ == STRUCT_BEGIN:
out.append((tag,'STRUCT_BEGIN',None)); continue
if typ == STRUCT_END:
out.append((tag,'STRUCT_END',None)); continue
v = read_value(r, typ)
name = FIELDS.get(tag, ('?',''))[0]
out.append((tag, name, v))
return out
def show(v):
if isinstance(v, bytes):
try:
s = v.decode('utf8')
if all(32 <= c < 127 or c in (10,13,9) for c in v):
return repr(s)
except Exception:
pass
return 'hex:' + v.hex()
return repr(v)
for line in open(sys.argv[1]):
line = line.strip()
if not line.startswith('{'):
continue
o = json.loads(line)
if o.get('kind') == 'SIGNED':
continue
b = bytes.fromhex(o['enc'])
print(f"===== {o['kind']} enc={len(b)}B hash={o['hash']}")
try:
for tag, name, v in decode_fields(b):
print(f" tag={tag:<3} {name:<24} {show(v)}")
except Exception as e:
print(" ERR:", e)
print()