33 RequestType ,
44 Signatures ,
55 UpdateEnvelope ,
6- ReadStateEnvelope ,
76 RequestEnvelope ,
7+ HttpCanisterUpdate ,
88} from './iface' ;
99import utils from './utils' ;
1010import assert from 'assert' ;
@@ -30,54 +30,48 @@ export class SignedTransactionBuilder {
3030 const unsignedTransaction = utils . cborDecode (
3131 utils . blobFromHex ( combineRequest . unsigned_transaction )
3232 ) as CborUnsignedTransaction ;
33- assert ( combineRequest . signatures . length === unsignedTransaction . ingress_expiries . length * 2 ) ;
33+ // at present we expect only one request type and one signature
34+ assert ( combineRequest . signatures . length === 1 ) ;
3435 assert ( unsignedTransaction . updates . length === 1 ) ;
3536 const envelopes = this . getEnvelopes ( unsignedTransaction , signatureMap ) ;
36- const envelopRequests = { requests : envelopes } ;
37- const signedTransaction = utils . cborEncode ( envelopRequests ) ;
37+ const requestEnvelopeArray = envelopes [ 0 ] as [ RequestEnvelope [ ] ] ;
38+ const requestEnvelopes = requestEnvelopeArray [ 0 ] as RequestEnvelope [ ] ;
39+ assert ( requestEnvelopes . length === 1 ) ;
40+ const requestEnvelope = requestEnvelopes [ 0 ] as RequestEnvelope ;
41+ const signedTransaction = utils . cborEncode ( requestEnvelope . update as UpdateEnvelope ) ;
3842 return signedTransaction ;
3943 }
4044
4145 getEnvelopes (
4246 unsignedTransaction : CborUnsignedTransaction ,
4347 signatureMap : Map < string , Signatures >
44- ) : [ string , RequestEnvelope [ ] ] [ ] {
45- const envelopes : [ string , RequestEnvelope [ ] ] [ ] = [ ] ;
46- for ( const [ reqType , update ] of unsignedTransaction . updates ) {
48+ ) : [ RequestEnvelope [ ] ] [ ] {
49+ const envelopes : [ RequestEnvelope [ ] ] [ ] = [ ] ;
50+ for ( const [ , update ] of unsignedTransaction . updates as unknown as [ string , HttpCanisterUpdate ] [ ] ) {
4751 const requestEnvelopes : RequestEnvelope [ ] = [ ] ;
48- for ( const ingressExpiry of unsignedTransaction . ingress_expiries ) {
49- update . ingress_expiry = ingressExpiry ;
50-
51- const readState = utils . makeReadStateFromUpdate ( update ) ;
52- const transactionSignature = utils . getTransactionSignature ( signatureMap , update ) ;
53- if ( ! transactionSignature ) {
54- throw new Error ( 'Transaction signature is invalid' ) ;
55- }
52+ if ( unsignedTransaction . ingress_expiries . length != 1 ) {
53+ throw new Error ( 'ingress expiry can have only one entry' ) ;
54+ }
55+ const ingressExpiry = unsignedTransaction . ingress_expiries [ 0 ] ;
56+ update . ingress_expiry = ingressExpiry ;
5657
57- const readStateSignature = utils . getReadStateSignature ( signatureMap , readState ) ;
58- if ( ! readStateSignature ) {
59- throw new Error ( 'read state signature is invalid' ) ;
60- }
58+ const transactionSignature = utils . getTransactionSignature ( signatureMap , update ) ;
59+ if ( ! transactionSignature ) {
60+ throw new Error ( 'Transaction signature is invalid' ) ;
61+ }
6162
62- const pk_der = utils . getPublicKeyInDERFormat ( transactionSignature . public_key . hex_bytes ) ;
63- const updateEnvelope : UpdateEnvelope = {
64- content : { request_type : RequestType . CALL , ...update } ,
65- sender_pubkey : pk_der ,
66- sender_sig : utils . blobFromHex ( transactionSignature . hex_bytes ) ,
67- } ;
63+ const pk_der = utils . getPublicKeyInDERFormat ( transactionSignature . public_key . hex_bytes ) ;
64+ const updateEnvelope : UpdateEnvelope = {
65+ content : { request_type : RequestType . CALL , ...update } ,
66+ sender_pubkey : pk_der ,
67+ sender_sig : utils . blobFromHex ( transactionSignature . hex_bytes ) ,
68+ } ;
6869
69- const readStateEnvelope : ReadStateEnvelope = {
70- content : { request_type : RequestType . READ_STATE , ...readState } ,
71- sender_pubkey : pk_der ,
72- sender_sig : utils . blobFromHex ( readStateSignature . hex_bytes ) ,
73- } ;
70+ requestEnvelopes . push ( {
71+ update : updateEnvelope ,
72+ } ) ;
7473
75- requestEnvelopes . push ( {
76- update : updateEnvelope ,
77- read_state : readStateEnvelope ,
78- } ) ;
79- }
80- envelopes . push ( [ reqType , requestEnvelopes ] ) ;
74+ envelopes . push ( [ requestEnvelopes ] ) ;
8175 }
8276 return envelopes ;
8377 }
0 commit comments