@hwlt/era-connect 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -9
- package/bch/package.json +6 -0
- package/dist/bch-CK6dKGQA.d.ts +74 -0
- package/dist/bch-CpLACx0j.cjs +139 -0
- package/dist/bch-CpLACx0j.cjs.map +1 -0
- package/dist/bch-DHsIVJqK.d.cts +74 -0
- package/dist/bch-dMESlQfv.js +134 -0
- package/dist/bch-dMESlQfv.js.map +1 -0
- package/dist/bch.cjs +13 -0
- package/dist/bch.d.cts +26 -0
- package/dist/bch.d.ts +26 -0
- package/dist/bch.js +4 -0
- package/dist/{btc-B_Nq2H4T.js → btc-BmVgvvBd.js} +2 -2
- package/dist/btc-BmVgvvBd.js.map +1 -0
- package/dist/{btc-ByzjzUKs.cjs → btc-BxNww6kW.cjs} +2 -2
- package/dist/btc-BxNww6kW.cjs.map +1 -0
- package/dist/{btc-B8LfSEkv.d.ts → btc-uzt0uC-Q.d.ts} +6 -4
- package/dist/{btc-C4dVkxHb.d.cts → btc-wogJ4INA.d.cts} +6 -4
- package/dist/btc.cjs +1 -1
- package/dist/btc.d.cts +1 -1
- package/dist/btc.d.ts +1 -1
- package/dist/btc.js +1 -1
- package/dist/cashaddr-BIzawizX.js +134 -0
- package/dist/cashaddr-BIzawizX.js.map +1 -0
- package/dist/cashaddr-Ci0_8ITN.cjs +151 -0
- package/dist/cashaddr-Ci0_8ITN.cjs.map +1 -0
- package/dist/{derive-CK9Nc9Jc.js → derive-00JUOkmn.js} +7 -2
- package/dist/derive-00JUOkmn.js.map +1 -0
- package/dist/{derive-BayrpfJk.cjs → derive-BuNsfWlg.cjs} +12 -1
- package/dist/derive-BuNsfWlg.cjs.map +1 -0
- package/dist/gzip-CLzDX7AZ.cjs +79 -0
- package/dist/gzip-CLzDX7AZ.cjs.map +1 -0
- package/dist/gzip-DyZLtgJJ.js +68 -0
- package/dist/gzip-DyZLtgJJ.js.map +1 -0
- package/dist/index.cjs +44 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +28 -3
- package/dist/index.d.ts +28 -3
- package/dist/index.js +43 -4
- package/dist/index.js.map +1 -1
- package/dist/{messages-DbnXy3dC.js → messages--XbVef_k.js} +38 -2
- package/dist/messages--XbVef_k.js.map +1 -0
- package/dist/{messages-BZXDY29v.cjs → messages-orAQ52CI.cjs} +43 -1
- package/dist/messages-orAQ52CI.cjs.map +1 -0
- package/dist/{tron-fufBDNuQ.js → tron-BpUEYTPT.js} +4 -67
- package/dist/tron-BpUEYTPT.js.map +1 -0
- package/dist/{tron-B9wXdzlA.cjs → tron-DYPOg5cm.cjs} +5 -68
- package/dist/{tron-fufBDNuQ.js.map → tron-DYPOg5cm.cjs.map} +1 -1
- package/dist/tron.cjs +2 -2
- package/dist/tron.js +2 -2
- package/dist/verify.cjs +224 -17
- package/dist/verify.cjs.map +1 -1
- package/dist/verify.d.cts +67 -1
- package/dist/verify.d.ts +67 -1
- package/dist/verify.js +222 -18
- package/dist/verify.js.map +1 -1
- package/package.json +13 -1
- package/dist/btc-B_Nq2H4T.js.map +0 -1
- package/dist/btc-ByzjzUKs.cjs.map +0 -1
- package/dist/derive-BayrpfJk.cjs.map +0 -1
- package/dist/derive-CK9Nc9Jc.js.map +0 -1
- package/dist/messages-BZXDY29v.cjs.map +0 -1
- package/dist/messages-DbnXy3dC.js.map +0 -1
- package/dist/tron-B9wXdzlA.cjs.map +0 -1
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"messages-BZXDY29v.cjs","names":["utf8Encode","concatBytes","EraSdkError","EraSdkError","utf8Decode","hexToBytes"],"sources":["../src/tron-proto/wire.ts","../src/tron-proto/messages.ts"],"sourcesContent":["import { concatBytes, utf8Encode } from '../core/bytes';\nimport { EraSdkError } from '../core/errors';\n\n/**\n * Minimal protobuf wire codec for the Tron signing envelope. Varint and\n * length-delimited wire types only — the schema uses nothing else.\n *\n * Writer semantics mirror the reference implementation: EXPLICITLY SET fields\n * are written even when they hold default values (a `timestamp` of 0 is\n * `tag, 0x00` on the wire), and fields are emitted in ascending field-number\n * order. Byte-exactness against the golden fixtures depends on both.\n */\n\nexport class ProtoWriter {\n private readonly parts: Uint8Array[] = [];\n\n varintField(field: number, value: number | bigint): this {\n this.parts.push(tag(field, 0), varint(value));\n return this;\n }\n\n stringField(field: number, value: string): this {\n const bytes = utf8Encode(value);\n this.parts.push(tag(field, 2), varint(bytes.length), bytes);\n return this;\n }\n\n bytesField(field: number, value: Uint8Array): this {\n this.parts.push(tag(field, 2), varint(value.length), value);\n return this;\n }\n\n messageField(field: number, encoded: Uint8Array): this {\n this.parts.push(tag(field, 2), varint(encoded.length), encoded);\n return this;\n }\n\n finish(): Uint8Array {\n return concatBytes(...this.parts);\n }\n}\n\nfunction tag(field: number, wireType: number): Uint8Array {\n return varint((field << 3) | wireType);\n}\n\nexport function varint(value: number | bigint): Uint8Array {\n let v = typeof value === 'number' ? BigInt(value) : value;\n if (v < 0n) throw new EraSdkError('protobuf-error', 'negative varint');\n const out: number[] = [];\n do {\n let byte = Number(v & 0x7fn);\n v >>= 7n;\n if (v > 0n) byte |= 0x80;\n out.push(byte);\n } while (v > 0n);\n return new Uint8Array(out);\n}\n\nexport interface ProtoField {\n readonly field: number;\n readonly wireType: number;\n /** Set for wire type 0. */\n readonly value: bigint;\n /** Set for wire type 2 (verbatim slice of the input). */\n readonly bytes: Uint8Array;\n}\n\n/**\n * Hardened field reader: varint shift capped, lengths bounds-checked before\n * slicing, unknown fields skippable by wire type, group wire types refused.\n */\nexport function readFields(bytes: Uint8Array): ProtoField[] {\n const fields: ProtoField[] = [];\n let offset = 0;\n\n const readVarint = (): bigint => {\n let result = 0n;\n let shift = 0n;\n for (;;) {\n const byte = bytes[offset];\n if (byte === undefined) throw new EraSdkError('protobuf-error', 'truncated varint');\n offset += 1;\n result |= BigInt(byte & 0x7f) << shift;\n if ((byte & 0x80) === 0) return result;\n shift += 7n;\n if (shift > 63n) throw new EraSdkError('protobuf-error', 'varint exceeds 64 bits');\n }\n };\n\n while (offset < bytes.length) {\n const key = readVarint();\n const field = Number(key >> 3n);\n const wireType = Number(key & 7n);\n if (field === 0) throw new EraSdkError('protobuf-error', 'field number 0');\n switch (wireType) {\n case 0: {\n fields.push({ field, wireType, value: readVarint(), bytes: EMPTY });\n break;\n }\n case 1: {\n if (offset + 8 > bytes.length) throw new EraSdkError('protobuf-error', 'truncated fixed64');\n fields.push({ field, wireType, value: 0n, bytes: bytes.slice(offset, offset + 8) });\n offset += 8;\n break;\n }\n case 2: {\n const length = readVarint();\n if (length > BigInt(bytes.length - offset)) {\n throw new EraSdkError('protobuf-error', 'length-delimited field exceeds input');\n }\n const len = Number(length);\n fields.push({ field, wireType, value: 0n, bytes: bytes.slice(offset, offset + len) });\n offset += len;\n break;\n }\n case 5: {\n if (offset + 4 > bytes.length) throw new EraSdkError('protobuf-error', 'truncated fixed32');\n fields.push({ field, wireType, value: 0n, bytes: bytes.slice(offset, offset + 4) });\n offset += 4;\n break;\n }\n default:\n throw new EraSdkError('protobuf-error', `unsupported wire type ${wireType}`);\n }\n }\n return fields;\n}\n\nconst EMPTY = new Uint8Array(0);\n\n/** First occurrence of a length-delimited field, or null. */\nexport function firstBytes(fields: ProtoField[], field: number): Uint8Array | null {\n for (const f of fields) {\n if (f.field === field && f.wireType === 2) return f.bytes;\n }\n return null;\n}\n\n/** First occurrence of a varint field, or null. */\nexport function firstVarint(fields: ProtoField[], field: number): bigint | null {\n for (const f of fields) {\n if (f.field === field && f.wireType === 0) return f.value;\n }\n return null;\n}\n","import { hexToBytes, utf8Decode } from '../core/bytes';\nimport { EraSdkError } from '../core/errors';\nimport { firstBytes, ProtoWriter, readFields } from './wire';\n\n/**\n * The Tron signing envelope (\"QrCode Protocol\"):\n * `Base -> Payload -> SignTransaction -> TronTx`, gzip-compressed and wrapped\n * in a `keystone-sign-request` (6101) CBOR map. Schemas are vendored under\n * `proto/tron/`; this codec hand-writes the five messages the wallet side\n * speaks, with reference-exact field emission (explicitly-set defaults ARE\n * written; ascending field order).\n */\n\nexport interface TronLatestBlock {\n /** FULL 64-hex block id of a live now-block (the device slices ref_block_hash from it). */\n readonly hash: string;\n readonly number: number;\n readonly timestamp: number;\n}\n\nexport interface TronSignRequestProto {\n /** Lowercase, ZERO-PADDED 8-hex source fingerprint (the wire demands the string form). */\n readonly xfpHex: string;\n /** Hyphenated UUID string; echoed by the device as the only reply binding. */\n readonly signId: string;\n readonly hdPath: string;\n readonly timestamp: number;\n readonly decimals: number;\n readonly token: string;\n readonly contractAddress?: string | undefined;\n readonly from?: string | undefined;\n readonly to?: string | undefined;\n readonly memo?: string | undefined;\n readonly value?: string | undefined;\n readonly fee?: number | undefined;\n readonly latestBlock: TronLatestBlock;\n /** Serialized `Transaction.raw_data` — the signing source of truth. */\n readonly rawData: Uint8Array;\n}\n\nconst PAYLOAD_TYPE_SIGN_TX = 2;\n\nexport function encodeSignRequestProto(req: TronSignRequestProto): Uint8Array {\n if (req.fee !== undefined && (req.fee < 0 || req.fee > 0x7fffffff)) {\n throw new EraSdkError('invalid-props', 'tron fee must fit a positive int32');\n }\n const latestBlock = new ProtoWriter()\n .stringField(1, req.latestBlock.hash)\n .varintField(2, req.latestBlock.number)\n .varintField(3, req.latestBlock.timestamp)\n .finish();\n\n const tronTx = new ProtoWriter().stringField(1, req.token);\n if (req.contractAddress !== undefined) tronTx.stringField(2, req.contractAddress);\n if (req.from !== undefined) tronTx.stringField(3, req.from);\n if (req.to !== undefined) tronTx.stringField(4, req.to);\n if (req.memo !== undefined) tronTx.stringField(5, req.memo);\n if (req.value !== undefined) tronTx.stringField(6, req.value);\n tronTx.messageField(7, latestBlock);\n if (req.fee !== undefined) tronTx.varintField(9, req.fee);\n tronTx.bytesField(10, req.rawData);\n\n const signTx = new ProtoWriter()\n .stringField(1, 'TRON')\n .stringField(2, req.signId)\n .stringField(3, req.hdPath)\n .varintField(4, req.timestamp)\n .varintField(5, req.decimals)\n .messageField(8, tronTx.finish())\n .finish();\n\n const payload = new ProtoWriter()\n .varintField(1, PAYLOAD_TYPE_SIGN_TX)\n .stringField(2, req.xfpHex)\n .messageField(4, signTx)\n .finish();\n\n return new ProtoWriter()\n .varintField(1, 2) // Base.version\n .stringField(2, 'QrCode Protocol')\n .messageField(3, payload)\n .finish();\n}\n\nexport interface TronSignResultProto {\n readonly signId: string;\n readonly txId: string;\n readonly rawTx: string;\n}\n\n/** Parse a `keystone-sign-result` Base protobuf. Unknown fields are skipped. */\nexport function decodeSignResultProto(bytes: Uint8Array): TronSignResultProto {\n const base = readFields(bytes);\n const payloadBytes = firstBytes(base, 3);\n if (!payloadBytes) {\n throw new EraSdkError('malformed-reply', 'keystone-sign-result carries no payload');\n }\n const payload = readFields(payloadBytes);\n const resultBytes = firstBytes(payload, 7); // Payload.signTxResult\n if (!resultBytes) {\n return { signId: '', txId: '', rawTx: '' };\n }\n const result = readFields(resultBytes);\n return {\n signId: text(firstBytes(result, 1)),\n txId: text(firstBytes(result, 2)),\n rawTx: text(firstBytes(result, 3)),\n };\n}\n\nfunction text(bytes: Uint8Array | null): string {\n if (!bytes) return '';\n try {\n return utf8Decode(bytes);\n } catch {\n throw new EraSdkError('malformed-reply', 'protobuf string field is not valid UTF-8');\n }\n}\n\nexport interface SignedTronTx {\n /** Verbatim `raw_data` slice — the digest is sha256 over the device's own serialization. */\n readonly rawData: Uint8Array;\n readonly signatures: readonly Uint8Array[];\n}\n\n/**\n * Split a signed Tron network `Transaction` frame (`{1: raw_data, 2: signature*}`)\n * from the reply's `rawTx` hex. Top-level fields must be length-delimited —\n * anything else is not a transaction frame.\n */\nexport function splitSignedTronTx(rawTxHex: string): SignedTronTx {\n let frame: Uint8Array;\n try {\n frame = hexToBytes(rawTxHex);\n } catch {\n throw new EraSdkError('malformed-reply', 'signed Tron transaction is not hex');\n }\n const fields = readFields(frame);\n let rawData: Uint8Array | null = null;\n const signatures: Uint8Array[] = [];\n for (const f of fields) {\n if (f.wireType !== 2) {\n throw new EraSdkError('malformed-reply', 'unexpected wire type in signed Tron transaction');\n }\n if (f.field === 1 && rawData === null) rawData = f.bytes;\n if (f.field === 2) signatures.push(f.bytes);\n }\n if (rawData === null) {\n throw new EraSdkError('malformed-reply', 'signed Tron transaction carries no raw_data');\n }\n return { rawData, signatures };\n}\n"],"mappings":";;;;;;;;;;;AAaA,IAAa,cAAb,MAAyB;;EACvB,KAAiB,QAAsB,CAAC;;CAExC,YAAY,OAAe,OAA8B;EACvD,KAAK,MAAM,KAAK,IAAI,OAAO,CAAC,GAAG,OAAO,KAAK,CAAC;EAC5C,OAAO;CACT;CAEA,YAAY,OAAe,OAAqB;EAC9C,MAAM,QAAQA,eAAAA,WAAW,KAAK;EAC9B,KAAK,MAAM,KAAK,IAAI,OAAO,CAAC,GAAG,OAAO,MAAM,MAAM,GAAG,KAAK;EAC1D,OAAO;CACT;CAEA,WAAW,OAAe,OAAyB;EACjD,KAAK,MAAM,KAAK,IAAI,OAAO,CAAC,GAAG,OAAO,MAAM,MAAM,GAAG,KAAK;EAC1D,OAAO;CACT;CAEA,aAAa,OAAe,SAA2B;EACrD,KAAK,MAAM,KAAK,IAAI,OAAO,CAAC,GAAG,OAAO,QAAQ,MAAM,GAAG,OAAO;EAC9D,OAAO;CACT;CAEA,SAAqB;EACnB,OAAOC,eAAAA,YAAY,GAAG,KAAK,KAAK;CAClC;AACF;AAEA,SAAS,IAAI,OAAe,UAA8B;CACxD,OAAO,OAAQ,SAAS,IAAK,QAAQ;AACvC;AAEA,SAAgB,OAAO,OAAoC;CACzD,IAAI,IAAI,OAAO,UAAU,WAAW,OAAO,KAAK,IAAI;CACpD,IAAI,IAAI,IAAI,MAAM,IAAIC,eAAAA,YAAY,kBAAkB,iBAAiB;CACrE,MAAM,MAAgB,CAAC;CACvB,GAAG;EACD,IAAI,OAAO,OAAO,IAAI,IAAK;EAC3B,MAAM;EACN,IAAI,IAAI,IAAI,QAAQ;EACpB,IAAI,KAAK,IAAI;C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Q,CAAC,CAAC;EAChC,OAAO,KAAK,WAAW,QAAQ,CAAC,CAAC;CACnC;AACF;AAEA,SAAS,KAAK,OAAkC;CAC9C,IAAI,CAAC,OAAO,OAAO;CACnB,IAAI;EACF,OAAOC,eAAAA,WAAW,KAAK;CACzB,QAAQ;EACN,MAAM,IAAID,eAAAA,YAAY,mBAAmB,0CAA0C;CACrF;AACF;;;;;;AAaA,SAAgB,kBAAkB,UAAgC;CAChE,IAAI;CACJ,IAAI;EACF,QAAQE,eAAAA,WAAW,QAAQ;CAC7B,QAAQ;EACN,MAAM,IAAIF,eAAAA,YAAY,mBAAmB,oCAAoC;CAC/E;CACA,MAAM,SAAS,WAAW,KAAK;CAC/B,IAAI,UAA6B;CACjC,MAAM,aAA2B,CAAC;CAClC,KAAK,MAAM,KAAK,QAAQ;EACtB,IAAI,EAAE,aAAa,GACjB,MAAM,IAAIA,eAAAA,YAAY,mBAAmB,iDAAiD;EAE5F,IAAI,EAAE,UAAU,KAAK,YAAY,MAAM,UAAU,EAAE;EACnD,IAAI,EAAE,UAAU,GAAG,WAAW,KAAK,EAAE,KAAK;CAC5C;CACA,IAAI,YAAY,MACd,MAAM,IAAIA,eAAAA,YAAY,mBAAmB,6CAA6C;CAExF,OAAO;EAAE;EAAS;CAAW;AAC/B"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"messages-DbnXy3dC.js","names":[],"sources":["../src/tron-proto/wire.ts","../src/tron-proto/messages.ts"],"sourcesContent":["import { concatBytes, utf8Encode } from '../core/bytes';\nimport { EraSdkError } from '../core/errors';\n\n/**\n * Minimal protobuf wire codec for the Tron signing envelope. Varint and\n * length-delimited wire types only — the schema uses nothing else.\n *\n * Writer semantics mirror the reference implementation: EXPLICITLY SET fields\n * are written even when they hold default values (a `timestamp` of 0 is\n * `tag, 0x00` on the wire), and fields are emitted in ascending field-number\n * order. Byte-exactness against the golden fixtures depends on both.\n */\n\nexport class ProtoWriter {\n private readonly parts: Uint8Array[] = [];\n\n varintField(field: number, value: number | bigint): this {\n this.parts.push(tag(field, 0), varint(value));\n return this;\n }\n\n stringField(field: number, value: string): this {\n const bytes = utf8Encode(value);\n this.parts.push(tag(field, 2), varint(bytes.length), bytes);\n return this;\n }\n\n bytesField(field: number, value: Uint8Array): this {\n this.parts.push(tag(field, 2), varint(value.length), value);\n return this;\n }\n\n messageField(field: number, encoded: Uint8Array): this {\n this.parts.push(tag(field, 2), varint(encoded.length), encoded);\n return this;\n }\n\n finish(): Uint8Array {\n return concatBytes(...this.parts);\n }\n}\n\nfunction tag(field: number, wireType: number): Uint8Array {\n return varint((field << 3) | wireType);\n}\n\nexport function varint(value: number | bigint): Uint8Array {\n let v = typeof value === 'number' ? BigInt(value) : value;\n if (v < 0n) throw new EraSdkError('protobuf-error', 'negative varint');\n const out: number[] = [];\n do {\n let byte = Number(v & 0x7fn);\n v >>= 7n;\n if (v > 0n) byte |= 0x80;\n out.push(byte);\n } while (v > 0n);\n return new Uint8Array(out);\n}\n\nexport interface ProtoField {\n readonly field: number;\n readonly wireType: number;\n /** Set for wire type 0. */\n readonly value: bigint;\n /** Set for wire type 2 (verbatim slice of the input). */\n readonly bytes: Uint8Array;\n}\n\n/**\n * Hardened field reader: varint shift capped, lengths bounds-checked before\n * slicing, unknown fields skippable by wire type, group wire types refused.\n */\nexport function readFields(bytes: Uint8Array): ProtoField[] {\n const fields: ProtoField[] = [];\n let offset = 0;\n\n const readVarint = (): bigint => {\n let result = 0n;\n let shift = 0n;\n for (;;) {\n const byte = bytes[offset];\n if (byte === undefined) throw new EraSdkError('protobuf-error', 'truncated varint');\n offset += 1;\n result |= BigInt(byte & 0x7f) << shift;\n if ((byte & 0x80) === 0) return result;\n shift += 7n;\n if (shift > 63n) throw new EraSdkError('protobuf-error', 'varint exceeds 64 bits');\n }\n };\n\n while (offset < bytes.length) {\n const key = readVarint();\n const field = Number(key >> 3n);\n const wireType = Number(key & 7n);\n if (field === 0) throw new EraSdkError('protobuf-error', 'field number 0');\n switch (wireType) {\n case 0: {\n fields.push({ field, wireType, value: readVarint(), bytes: EMPTY });\n break;\n }\n case 1: {\n if (offset + 8 > bytes.length) throw new EraSdkError('protobuf-error', 'truncated fixed64');\n fields.push({ field, wireType, value: 0n, bytes: bytes.slice(offset, offset + 8) });\n offset += 8;\n break;\n }\n case 2: {\n const length = readVarint();\n if (length > BigInt(bytes.length - offset)) {\n throw new EraSdkError('protobuf-error', 'length-delimited field exceeds input');\n }\n const len = Number(length);\n fields.push({ field, wireType, value: 0n, bytes: bytes.slice(offset, offset + len) });\n offset += len;\n break;\n }\n case 5: {\n if (offset + 4 > bytes.length) throw new EraSdkError('protobuf-error', 'truncated fixed32');\n fields.push({ field, wireType, value: 0n, bytes: bytes.slice(offset, offset + 4) });\n offset += 4;\n break;\n }\n default:\n throw new EraSdkError('protobuf-error', `unsupported wire type ${wireType}`);\n }\n }\n return fields;\n}\n\nconst EMPTY = new Uint8Array(0);\n\n/** First occurrence of a length-delimited field, or null. */\nexport function firstBytes(fields: ProtoField[], field: number): Uint8Array | null {\n for (const f of fields) {\n if (f.field === field && f.wireType === 2) return f.bytes;\n }\n return null;\n}\n\n/** First occurrence of a varint field, or null. */\nexport function firstVarint(fields: ProtoField[], field: number): bigint | null {\n for (const f of fields) {\n if (f.field === field && f.wireType === 0) return f.value;\n }\n return null;\n}\n","import { hexToBytes, utf8Decode } from '../core/bytes';\nimport { EraSdkError } from '../core/errors';\nimport { firstBytes, ProtoWriter, readFields } from './wire';\n\n/**\n * The Tron signing envelope (\"QrCode Protocol\"):\n * `Base -> Payload -> SignTransaction -> TronTx`, gzip-compressed and wrapped\n * in a `keystone-sign-request` (6101) CBOR map. Schemas are vendored under\n * `proto/tron/`; this codec hand-writes the five messages the wallet side\n * speaks, with reference-exact field emission (explicitly-set defaults ARE\n * written; ascending field order).\n */\n\nexport interface TronLatestBlock {\n /** FULL 64-hex block id of a live now-block (the device slices ref_block_hash from it). */\n readonly hash: string;\n readonly number: number;\n readonly timestamp: number;\n}\n\nexport interface TronSignRequestProto {\n /** Lowercase, ZERO-PADDED 8-hex source fingerprint (the wire demands the string form). */\n readonly xfpHex: string;\n /** Hyphenated UUID string; echoed by the device as the only reply binding. */\n readonly signId: string;\n readonly hdPath: string;\n readonly timestamp: number;\n readonly decimals: number;\n readonly token: string;\n readonly contractAddress?: string | undefined;\n readonly from?: string | undefined;\n readonly to?: string | undefined;\n readonly memo?: string | undefined;\n readonly value?: string | undefined;\n readonly fee?: number | undefined;\n readonly latestBlock: TronLatestBlock;\n /** Serialized `Transaction.raw_data` — the signing source of truth. */\n readonly rawData: Uint8Array;\n}\n\nconst PAYLOAD_TYPE_SIGN_TX = 2;\n\nexport function encodeSignRequestProto(req: TronSignRequestProto): Uint8Array {\n if (req.fee !== undefined && (req.fee < 0 || req.fee > 0x7fffffff)) {\n throw new EraSdkError('invalid-props', 'tron fee must fit a positive int32');\n }\n const latestBlock = new ProtoWriter()\n .stringField(1, req.latestBlock.hash)\n .varintField(2, req.latestBlock.number)\n .varintField(3, req.latestBlock.timestamp)\n .finish();\n\n const tronTx = new ProtoWriter().stringField(1, req.token);\n if (req.contractAddress !== undefined) tronTx.stringField(2, req.contractAddress);\n if (req.from !== undefined) tronTx.stringField(3, req.from);\n if (req.to !== undefined) tronTx.stringField(4, req.to);\n if (req.memo !== undefined) tronTx.stringField(5, req.memo);\n if (req.value !== undefined) tronTx.stringField(6, req.value);\n tronTx.messageField(7, latestBlock);\n if (req.fee !== undefined) tronTx.varintField(9, req.fee);\n tronTx.bytesField(10, req.rawData);\n\n const signTx = new ProtoWriter()\n .stringField(1, 'TRON')\n .stringField(2, req.signId)\n .stringField(3, req.hdPath)\n .varintField(4, req.timestamp)\n .varintField(5, req.decimals)\n .messageField(8, tronTx.finish())\n .finish();\n\n const payload = new ProtoWriter()\n .varintField(1, PAYLOAD_TYPE_SIGN_TX)\n .stringField(2, req.xfpHex)\n .messageField(4, signTx)\n .finish();\n\n return new ProtoWriter()\n .varintField(1, 2) // Base.version\n .stringField(2, 'QrCode Protocol')\n .messageField(3, payload)\n .finish();\n}\n\nexport interface TronSignResultProto {\n readonly signId: string;\n readonly txId: string;\n readonly rawTx: string;\n}\n\n/** Parse a `keystone-sign-result` Base protobuf. Unknown fields are skipped. */\nexport function decodeSignResultProto(bytes: Uint8Array): TronSignResultProto {\n const base = readFields(bytes);\n const payloadBytes = firstBytes(base, 3);\n if (!payloadBytes) {\n throw new EraSdkError('malformed-reply', 'keystone-sign-result carries no payload');\n }\n const payload = readFields(payloadBytes);\n const resultBytes = firstBytes(payload, 7); // Payload.signTxResult\n if (!resultBytes) {\n return { signId: '', txId: '', rawTx: '' };\n }\n const result = readFields(resultBytes);\n return {\n signId: text(firstBytes(result, 1)),\n txId: text(firstBytes(result, 2)),\n rawTx: text(firstBytes(result, 3)),\n };\n}\n\nfunction text(bytes: Uint8Array | null): string {\n if (!bytes) return '';\n try {\n return utf8Decode(bytes);\n } catch {\n throw new EraSdkError('malformed-reply', 'protobuf string field is not valid UTF-8');\n }\n}\n\nexport interface SignedTronTx {\n /** Verbatim `raw_data` slice — the digest is sha256 over the device's own serialization. */\n readonly rawData: Uint8Array;\n readonly signatures: readonly Uint8Array[];\n}\n\n/**\n * Split a signed Tron network `Transaction` frame (`{1: raw_data, 2: signature*}`)\n * from the reply's `rawTx` hex. Top-level fields must be length-delimited —\n * anything else is not a transaction frame.\n */\nexport function splitSignedTronTx(rawTxHex: string): SignedTronTx {\n let frame: Uint8Array;\n try {\n frame = hexToBytes(rawTxHex);\n } catch {\n throw new EraSdkError('malformed-reply', 'signed Tron transaction is not hex');\n }\n const fields = readFields(frame);\n let rawData: Uint8Array | null = null;\n const signatures: Uint8Array[] = [];\n for (const f of fields) {\n if (f.wireType !== 2) {\n throw new EraSdkError('malformed-reply', 'unexpected wire type in signed Tron transaction');\n }\n if (f.field === 1 && rawData === null) rawData = f.bytes;\n if (f.field === 2) signatures.push(f.bytes);\n }\n if (rawData === null) {\n throw new EraSdkError('malformed-reply', 'sign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,IAAI;CACJ,IAAI;EACF,QAAQ,WAAW,QAAQ;CAC7B,QAAQ;EACN,MAAM,IAAI,YAAY,mBAAmB,oCAAoC;CAC/E;CACA,MAAM,SAAS,WAAW,KAAK;CAC/B,IAAI,UAA6B;CACjC,MAAM,aAA2B,CAAC;CAClC,KAAK,MAAM,KAAK,QAAQ;EACtB,IAAI,EAAE,aAAa,GACjB,MAAM,IAAI,YAAY,mBAAmB,iDAAiD;EAE5F,IAAI,EAAE,UAAU,KAAK,YAAY,MAAM,UAAU,EAAE;EACnD,IAAI,EAAE,UAAU,GAAG,WAAW,KAAK,EAAE,KAAK;CAC5C;CACA,IAAI,YAAY,MACd,MAAM,IAAI,YAAY,mBAAmB,6CAA6C;CAExF,OAAO;EAAE;EAAS;CAAW;AAC/B"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"tron-B9wXdzlA.cjs","names":["gzipSync","EraSdkError","Gunzip","concatBytes","crc32","resolveContext","resolveRequestId","normalizeXfp","EraSdkError","encodeSignRequestProto","xfpToHex","uuidStringify","UrValue","cborEncode","cbMap","cbBytes","cbText","makeSignRequest","toUr","normalizeRequestId","requireReplyMap","asBytes","mapGet","decodeSignResultProto","splitSignedTronTx"],"sources":["../src/tron-proto/gzip.ts","../src/chains/tron.ts"],"sourcesContent":["import { Gunzip, gzipSync } from 'fflate';\nimport { concatBytes } from '../core/bytes';\nimport { EraSdkError } from '../core/errors';\nimport { crc32 } from '../ur/crc32';\n\n/** Smallest possible gzip stream: 10-byte header + 8-byte trailer. */\nconst MIN_GZIP_BYTES = 18;\n\n/** Bytes fed to the inflater per step; bounds the overshoot past the cap. */\nconst SLICE_BYTES = 1024;\n\n/** Deterministic gzip (fixed level, zeroed mtime) for reproducible request bytes. */\nexport function gzipCompress(data: Uint8Array): Uint8Array {\n return gzipSync(data, { level: 9, mtime: 0 });\n}\n\n/**\n * Inflate with a hard output ceiling.\n *\n * A one-shot gunzip allocates the entire output before anyone can refuse it —\n * gzip reaches ~1000:1 in practice, so a few hundred scanned bytes could ask\n * for an arbitrary allocation. The decoder is therefore driven in slices and\n * the output counted as it arrives; the moment the total would pass the cap,\n * feeding stops and the reply is refused.\n *\n * The trailer's ISIZE (declared inflated length) is used twice: as a cheap\n * refusal of honest bombs before any work, and as a truncation check at the\n * end — an inflater hands back a partial buffer for a truncated stream\n * without erroring, and a genuine device reply always declares honestly.\n */\nexport function gunzipCapped(data: Uint8Array, maxOutputBytes: number): Uint8Array {\n if (data.length < MIN_GZIP_BYTES) {\n throw new EraSdkError('gzip-error', 'compressed payload is too short to be a gzip stream');\n }\n if (data[0] !== 0x1f || data[1] !== 0x8b) {\n throw new EraSdkError('gzip-error', 'compressed payload is not a gzip stream');\n }\n const n = data.length;\n const isize =\n (data[n - 4]! | (data[n - 3]! << 8) | (data[n - 2]! << 16) | (data[n - 1]! << 24)) >>> 0;\n if (isize > maxOutputBytes) {\n throw new EraSdkError(\n 'gzip-error',\n `compressed payload declares ${isize} bytes, over the ${maxOutputBytes} byte ceiling`,\n );\n }\n\n const chunks: Uint8Array[] = [];\n let total = 0;\n let overflowed = false;\n let malformed: string | null = null;\n\n const gunzip = new Gunzip((chunk) => {\n if (overflowed) return;\n if (total + chunk.length > maxOutputBytes) {\n overflowed = true;\n return;\n }\n chunks.push(chunk);\n total += chunk.length;\n });\n\n try {\n for (let offset = 0; offset < n && !overflowed; offset += SLICE_BYTES) {\n const end = Math.min(offset + SLICE_BYTES, n);\n const isLast = end === n;\n gunzip.push(data.slice(offset, end), isLast);\n }\n } catch (e) {\n malformed = (e as Error).message ?? 'inflate error';\n }\n\n if (overflowed) {\n throw new EraSdkError(\n 'gzip-error',\n `compressed payload inflates past the ${maxOutputBytes} byte ceiling`,\n );\n }\n if (malformed !== null) {\n throw new EraSdkError('gzip-error', `compressed payload is malformed: ${malformed}`);\n }\n const out = concatBytes(...chunks);\n if (out.length !== isize) {\n throw new EraSdkError(\n 'gzip-error',\n `compressed payload inflated to ${out.length} bytes but declares ${isize} — truncated or malformed`,\n );\n }\n // The trailer CRC32 (little-endian, bytes n-8..n-5) must cover the inflated\n // output. The streaming inflater does not verify it, and the reference\n // implementation's native decoder does — without this check a corrupted\n // stream, or a CONCATENATED multi-member stream (whose final member's CRC\n // cannot cover the whole output), would be accepted here and refused there.\n const declaredCrc =\n (data[n - 8]! | (data[n - 7]! << 8) | (data[n - 6]! << 16) | (data[n - 5]! << 24)) >>> 0;\n if (crc32(out) !== declaredCrc) {\n throw new EraSdkError('gzip-error', 'compressed payload is malformed: CRC mismatch');\n }\n return out;\n}\n","import { cborEncode } from '../cbor/encode';\nimport { asBytes, cbBytes, cbMap, cbText, mapGet } from '../cbor/model';\nimport { EraSdkError } from '../core/errors';\nimport { normalizeRequestId, uuidStringify } from '../core/rand';\nimport { normalizeXfp, parsePath, xfpToHex } from '../registry/keypath';\nimport { gunzipCapped, gzipCompress } from '../tron-proto/gzip';\nimport type { SignedTronTx, TronLatestBlock } from '../tron-proto/messages';\nimport {\n decodeSignResultProto,\n encodeSignRequestProto,\n splitSignedTronTx,\n} from '../tron-proto/messages';\nimport type { Ur } from '../ur/ur';\nimport { Ur as UrValue } from '../ur/ur';\nimport type { ChainContext, EraConnectConfig, ExpectedReply, SignRequest } from './shared';\nimport {\n makeSignRequest,\n requireReplyMap,\n requireUrType,\n resolveContext,\n resolveRequestId,\n toUr,\n} from './shared';\n\nexport type { TronLatestBlock } from '../tron-proto/messages';\n\nexport interface TronSignRequestProps {\n readonly requestId?: Uint8Array | string;\n /**\n * Serialized `Transaction.raw_data` — THE signing source of truth. The\n * device signs `sha256(rawData) = txID` and returns the transaction with\n * `raw_data` unmodified.\n */\n readonly rawData: Uint8Array;\n /** Full signing path, e.g. `m/44'/195'/0'/0/0`. */\n readonly path: string;\n readonly xfp: string | number;\n /**\n * Reference block context. Source it from a LIVE now-block query and pass\n * the FULL 64-hex block id.\n */\n readonly latestBlock: TronLatestBlock;\n /** On-device display only; safe to omit for opaque dApp transactions. */\n readonly display?: {\n readonly token?: string;\n readonly contractAddress?: string;\n readonly from?: string;\n readonly to?: string;\n readonly value?: string;\n readonly memo?: string;\n readonly fee?: number;\n readonly decimals?: number;\n };\n readonly timestamp?: number;\n readonly origin?: string;\n}\n\nexport interface TronSignatureResult {\n readonly requestId: Uint8Array;\n /** `sha256(raw_data)` hex, as computed by the device. */\n readonly txId: string;\n /** Hex of the fully assembled signed transaction — broadcast as-is. */\n readonly rawTx: string;\n /** The signed frame split into `raw_data` + signatures (65-byte r||s||recovery each). */\n readonly signedTx: SignedTronTx;\n}\n\n/**\n * Ceilings on the gzip blob a `keystone-sign-result` may carry. Tron is the\n * only chain whose reply is compressed, so it is the only one where a few\n * hundred scanned bytes can ask for an arbitrary allocation. Generous\n * multiples of the largest real device reply.\n */\nconst MAX_COMPRESSED_BYTES = 8 * 1024;\nconst MAX_INFLATED_BYTES = 64 * 1024;\n\nconst REPLY_TYPES = ['keystone-sign-result'] as const;\n\n/**\n * Tron signing rides the structured `keystone-sign-request` (6101) envelope —\n * a gzip-compressed protobuf inside CBOR `{1: gzip(protobuf), 2: origin}`.\n * The registry's generic `tron-sign-request` (5101) is NOT accepted by the\n * device and gets no response; do not emit it.\n */\nexport class TronChain {\n private readonly context: ChainContext;\n\n constructor(config?: EraConnectConfig) {\n this.context = resolveContext(config);\n }\n\n /** Build a `keystone-sign-request` (6101). Reply: `keystone-sign-result` (6102). */\n generateSignRequest(props: TronSignRequestProps): SignRequest<TronSignatureResult> {\n const requestId = resolveRequestId(this.context, props.requestId);\n parsePath(props.path); // validate shape; the wire carries the string form\n const xfp = normalizeXfp(props.xfp);\n if (props.rawData.length === 0) {\n throw new EraSdkError('invalid-props', 'rawData must not be empty');\n }\n if (!/^[0-9a-fA-F]{64}$/.test(props.latestBlock.hash)) {\n throw new EraSdkError(\n 'invalid-props',\n 'latestBlock.hash must be the FULL 64-hex block id (the device slices ref_block_hash from it)',\n );\n }\n\n const proto = encodeSignRequestProto({\n // Zero-padded to eight characters: the firmware parses this string with\n // a hex reader that yields 0 for anything shorter than 4 bytes, and a\n // zero fingerprint fails validation — a wallet whose fingerprint starts\n // with a zero byte (1 in 256) could not sign at all without the pad.\n xfpHex: xfpToHex(xfp),\n signId: uuidStringify(requestId),\n hdPath: props.path,\n timestamp: props.timestamp ?? 0,\n decimals: props.display?.decimals ?? 6,\n token: props.display?.token ?? '',\n contractAddress: props.display?.contractAddress,\n from: props.display?.from,\n to: props.display?.to,\n memo: props.display?.memo,\n value: props.display?.value,\n fee: props.display?.fee,\n latestBlock: props.latestBlock,\n rawData: props.rawData,\n });\n\n const ur = new UrValue(\n 'keystone-sign-request',\n cborEncode(\n cbMap([\n [1, cbBytes(gzipCompress(proto))],\n [2, cbText(props.origin ?? this.context.origin)],\n ]),\n ),\n );\n return makeSignRequest({\n ur,\n requestId,\n replyTypes: REPLY_TYPES,\n context: this.context,\n parse: (reply) => parseTronSignature(reply, requestId),\n });\n }\n\n /**\n * Parse a `keystone-sign-result` standalone. Tron carries the request id\n * INSIDE the protobuf (`signId`); passing `expect.requestId` is what makes\n * the echo check possible here — prefer `SignRequest.scanner().parse()`.\n */\n parseSignature(input: Ur | string, expect?: ExpectedReply): TronSignatureResult {\n return parseTronSignature(\n toUr(input),\n expect?.requestId === undefined ? undefined : normalizeRequestId(expect.requestId),\n );\n }\n}\n\nfunction parseTronSignature(\n ur: Ur,\n expectedRequestId: Uint8Array | undefined,\n): TronSignatureResult {\n requireUrType(ur, [...REPLY_TYPES], 'keystone-sign-result');\n const map = requireReplyMap(ur, 'keystone-sign-result');\n const compressed = asBytes(mapGet(map, 1));\n if (!compressed) {\n throw new EraSdkError('malformed-reply', 'keystone-sign-result is missing the payload (key 1)');\n }\n if (compressed.length > MAX_COMPRESSED_BYTES) {\n throw new EraSdkError(\n 'limit-exceeded',\n `keystone-sign-result payload is ${compressed.length} bytes, over the ${MAX_COMPRESSED_BYTES} byte ceiling`,\n );\n }\n const result = decodeSignResultProto(gunzipCapped(compressed, MAX_INFLATED_BYTES));\n\n // The signId echo is the ONLY anti-replay binding on this chain — the\n // device's own bytes are broadcast verbatim, so a stale reply that skipped\n // this check would finalize a payment the user did not approve now.\n if (expectedRequestId !== undefined) {\n const expected = uuidStringify(expectedRequestId).toLowerCase();\n if (result.signId.toLowerCase() !== expected) {\n throw new EraSdkError(\n 'request-id-mismatch',\n result.signId === ''\n ? 'keystone-sign-result does not echo the request id (signId)'\n : 'keystone-sign-result echoes a different request id — it answers another sign request, not this one',\n );\n }\n }\n if (result.rawTx === '') {\n throw new EraSdkError('malformed-reply', 'keystone-sign-result has no signed transaction');\n }\n const signedTx = splitSignedTronTx(result.rawTx);\n const requestId = expectedRequestId ?? signIdToBytes(result.signId);\n return { requestId, txId: result.txId, rawTx: result.rawTx, signedTx };\n}\n\nfunction signIdToBytes(signId: string): Uint8Array {\n try {\n return normalizeRequestId(signId);\n } catch {\n return new Uint8Array(16);\n }\n}\n"],"mappings":";;;;;;AAMA,MAAM,iBAAiB;;AAGvB,MAAM,cAAc;;AAGpB,SAAgB,aAAa,MAA8B;CACzD,QAAA,GAAOA,OAAAA,SAAAA,CAAS,MAAM;EAAE,OAAO;EAAG,OAAO;CAAE,CAAC;AAC9C;;;;;;;;;;;;;;;AAgBA,SAAgB,aAAa,MAAkB,gBAAoC;CACjF,IAAI,KAAK,SAAS,gBAChB,MAAM,IAAIC,eAAAA,YAAY,cAAc,qDAAqD;CAE3F,IAAI,KAAK,OAAO,MAAQ,KAAK,OAAO,KAClC,MAAM,IAAIA,eAAAA,YAAY,cAAc,yCAAyC;CAE/E,MAAM,IAAI,KAAK;CACf,MAAM,SACH,KAAK,IAAI,KAAO,KAAK,IAAI,MAAO,IAAM,KAAK,IAAI,MAAO,KAAO,KAAK,IAAI,MAAO,QAAS;CACzF,IAAI,QAAQ,gBACV,MAAM,IAAIA,eAAAA,YACR,cACA,+BAA+B,MAAM,mBAAmB,eAAe,cACzE;CAGF,MAAM,SAAuB,CAAC;CAC9B,IAAI,QAAQ;CACZ,IAAI,aA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