Skip to content

Commit

Permalink
Add external message bit to signed message part
Browse files Browse the repository at this point in the history
  • Loading branch information
Skydev0h committed Jan 30, 2024
1 parent 43eba0d commit 167347a
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 24 deletions.
7 changes: 4 additions & 3 deletions Specification.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,10 @@ Authentication modes:
```tl-b
signed_request$_
signature: bits512 // 512
wallet_id: (## 80) // 512+80
valid_until: (## 32) // 512+80+32
msg_seqno: (## 32) // 512+80+32+32 = 656
is_external: (## 1) // 512+1
wallet_id: (## 80) // 512+1+80
valid_until: (## 32) // 512+1+80+32
msg_seqno: (## 32) // 512+1+80+32+32 = 657
inner: InnerRequest = SignedRequest;
internal_signed#73696e74 signed:SignedRequest = InternalMsgBody;
Expand Down
43 changes: 25 additions & 18 deletions contracts/wallet_v5.fc
Original file line number Diff line number Diff line change
Expand Up @@ -140,22 +140,15 @@ cell verify_actions(cell c5) inline {
return ();
}

() dispatch_extension_request(slice cs, var dummy1) impure inline {
if (count_leading_zeroes(cs)) { ;; starts with bit 0
return set_actions(cs.preload_ref().verify_actions());
}
;; <<<<<<<<<<---------- Simple primary cases gas evaluation ends here ---------->>>>>>>>>>
;;
dummy1~impure_touch(); ;; DROP merged to 2DROP!
dispatch_complex_request(cs);
}
;; ------------------------------------------------------------------------------------------------

;; Verifies signed request, prevents replays and proceeds with `dispatch_request`.
() process_signed_request(slice body) impure inline {
() process_signed_request_from_external_message(slice body) impure inline {
;; The precise order of operations here is VERY important. Any other order results in unneccessary stack shuffles.
var signature = body~load_bits(512);

var cs = body;
throw_unless(38, cs~load_uint(1)); ;; signed external messages must begin with 1 prefix
var (subwallet_id, valid_until, msg_seqno) = (cs~load_uint(size::subwallet_id), cs~load_uint(size::valid_until), cs~load_uint(size::msg_seqno));

var ds = get_data().begin_parse();
Expand Down Expand Up @@ -193,15 +186,35 @@ cell verify_actions(cell c5) inline {
dispatch_complex_request(cs);
}

() recv_external(slice body) impure inline {
;; 0x7369676E ("sign") external message authenticated by signature
body = enforce_and_remove_sign_prefix(body);
process_signed_request_from_external_message(body);
return();
}

;; ------------------------------------------------------------------------------------------------

() dispatch_extension_request(slice cs, var dummy1) impure inline {
if (count_leading_zeroes(cs)) { ;; starts with bit 0
return set_actions(cs.preload_ref().verify_actions());
}
;; <<<<<<<<<<---------- Simple primary cases gas evaluation ends here ---------->>>>>>>>>>
;;
dummy1~impure_touch(); ;; DROP merged to 2DROP!
dispatch_complex_request(cs);
}

;; Same logic as above function but with return_* instead of throw_* and additional checks to prevent bounces
() process_signed_request_from_internal_message(slice body) impure inline {
;; Additional check to make sure that there are enough bits for reading (+1 for actual actions flag)
return_if(body.slice_bits() < 512 + size::subwallet_id + size::valid_until + size::msg_seqno + 1);
return_if(body.slice_bits() < 512 + 1 + size::subwallet_id + size::valid_until + size::msg_seqno + 1);

;; The precise order of operations here is VERY important. Any other order results in unneccessary stack shuffles.
var signature = body~load_bits(512);

var cs = body;
return_if(cs~load_uint(1)); ;; signed internal messages must begin with 0 prefix
var (subwallet_id, valid_until, msg_seqno) = (cs~load_uint(size::subwallet_id), cs~load_uint(size::valid_until), cs~load_uint(size::msg_seqno));

var ds = get_data().begin_parse();
Expand Down Expand Up @@ -237,13 +250,6 @@ cell verify_actions(cell c5) inline {
dispatch_complex_request(cs);
}

() recv_external(slice body) impure inline {
;; 0x7369676E ("sign") external message authenticated by signature
body = enforce_and_remove_sign_prefix(body);
process_signed_request(body);
return();
}

() recv_internal(cell full_msg, slice body) impure inline {

;; return right away if there are no references
Expand Down Expand Up @@ -295,6 +301,7 @@ cell verify_actions(cell c5) inline {

}

;; ------------------------------------------------------------------------------------------------
;; Get methods

int seqno() method_id {
Expand Down
1 change: 1 addition & 0 deletions tests/wallet-v5-extensions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ describe('Wallet V5 extensions auth', () => {

function createBody(actionsList: Cell) {
const payload = beginCell()
.storeUint(0, 1)
.storeUint(WALLET_ID.serialized, 80)
.storeUint(validUntil(), 32)
.storeUint(seqno, 32) // seqno
Expand Down
1 change: 1 addition & 0 deletions tests/wallet-v5-external.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ describe('Wallet V5 sign auth external', () => {

function createBody(actionsList: Cell) {
const payload = beginCell()
.storeUint(1, 1)
.storeUint(WALLET_ID.serialized, 80)
.storeUint(validUntil(), 32)
.storeUint(seqno, 32) // seqno
Expand Down
2 changes: 2 additions & 0 deletions tests/wallet-v5-internal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ describe('Wallet V5 sign auth internal', () => {

function createBody(actionsList: Cell) {
const payload = beginCell()
.storeUint(0, 1)
.storeUint(WALLET_ID.serialized, 80)
.storeUint(validUntil(), 32)
.storeUint(seqno, 32) // seqno
Expand Down Expand Up @@ -658,6 +659,7 @@ describe('Wallet V5 sign auth internal', () => {
const actionsList = packActionsList([new ActionSendMsg(SendMode.PAY_GAS_SEPARATELY, msg)]);

const payload = beginCell()
.storeUint(0, 1)
.storeUint(WALLET_ID.serialized, 80)
.storeUint(validUntil(), 32)
.storeUint(seqno, 32) // seqno
Expand Down
7 changes: 4 additions & 3 deletions types.tlb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ action_set_signature_auth_allowed#20cbb95a allowed:(## 1) = ExtendedAction;

signed_request$_
signature: bits512 // 512
wallet_id: (## 80) // 512+80
valid_until: (## 32) // 512+80+32
msg_seqno: (## 32) // 512+80+32+32 = 656
is_external: (## 1) // 512+1
wallet_id: (## 80) // 512+1+80
valid_until: (## 32) // 512+1+80+32
msg_seqno: (## 32) // 512+1+80+32+32 = 657
inner: InnerRequest = SignedRequest;

internal_signed#73696e74 signed:SignedRequest = InternalMsgBody;
Expand Down

0 comments on commit 167347a

Please sign in to comment.