Skip to content

Commit

Permalink
Change b"" for bytes literal to b''
Browse files Browse the repository at this point in the history
  • Loading branch information
xhliu committed May 5, 2020
1 parent 3f3aa18 commit fb222bb
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 40 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
## sCrypt 0.1.0
#### 05/05/20
* Change `b""` for `bytes` literal to `b''`

#### 02/14/20

* JavaScript bindings for the compiler: [scryptc @npm](https://www.npmjs.com/package/scryptc)
Expand Down
2 changes: 1 addition & 1 deletion docs/contracts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ As an example, contract ``CheckLockTimeVerify`` ensures coins are timelocked and
function fromLEUnsigned(bytes b) returns (int) {
// append positive sign byte. This does not hurt even when sign bit is already positive
return unpack(b ++ b"00");
return unpack(b ++ b'00');
}
}
Expand Down
12 changes: 6 additions & 6 deletions docs/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ bytes Operations

.. code-block:: solidity
bytes b = b"0011223344556677";
// b[3:6] == b"334455"
// b[:4] == b"00112233"
// b[5:] = b"556677"
bytes b = b'0011223344556677';
// b[3:6] == b'334455'
// b[:4] == b'00112233'
// b[5:] = b'556677'
* ``b1 ++ b2``

Expand All @@ -146,8 +146,8 @@ bytes Operations

.. code-block:: solidity
// returns b"6cfeea2d7a1d51249f0624ee98151bfa259d095642e253d8e2dce1e79df33f79"
reverseBytes32(b"793ff39de7e1dce2d853e24256099d25fa1b1598ee24069f24511d7a2deafe6c")
// returns b'6cfeea2d7a1d51249f0624ee98151bfa259d095642e253d8e2dce1e79df33f79'
reverseBytes32(b'793ff39de7e1dce2d853e24256099d25fa1b1598ee24069f24511d7a2deafe6c')
* ``bytes num2bin(int num, int size)``

Expand Down
20 changes: 10 additions & 10 deletions docs/multipartyhashpuzzles.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ extended to multiple parties so that multiple preimages have to be provided such
.. code-block:: solidity
contract MultiPartyHashPuzzles {
Sha256 hash1; // hash1 = b"136523B9FEA2B7321817B28E254A81A683D319D715CEE2360D051360A272DD4C"
Sha256 hash2; // hash2 = b"E222E30CF5C982E5F6251D755B0B16F608ACE631EB3BA9BDAF624FF1651ABF98"
Sha256 hash3; // hash3 = b"2A79F5D9F8B3770A59F91E0E9B4C379F7C7A32353AA6450065E43A8616EF5722"
Sha256 hash1; // hash1 = b'136523B9FEA2B7321817B28E254A81A683D319D715CEE2360D051360A272DD4C'
Sha256 hash2; // hash2 = b'E222E30CF5C982E5F6251D755B0B16F608ACE631EB3BA9BDAF624FF1651ABF98'
Sha256 hash3; // hash3 = b'2A79F5D9F8B3770A59F91E0E9B4C379F7C7A32353AA6450065E43A8616EF5722'
// preimage1: e.g., "bsv" -> b"627376"
// preimage2: e.g., "sCrypt" -> b"734372797074"
// preimage3: e.g., "IDE" -> b"494445"
// preimage1: e.g., "bsv" -> b'627376'
// preimage2: e.g., "sCrypt" -> b'734372797074'
// preimage3: e.g., "IDE" -> b'494445'
public function unlock(bytes preimage1, bytes preimage2, bytes preimage3) {
require(sha256(preimage1) == this.hash1);
require(sha256(preimage2) == this.hash2);
Expand All @@ -30,11 +30,11 @@ Instead, we can combine all ``y``'s into a single y such that ``y = H(H(y1 || y2
contract MultiPartyHashPuzzlesCompact {
// only 1 hash needs to go into the locking script, saving space
Sha256 combinedHash; // combinedHash = b"C9392767AB23CEFF09D207B9223C0C26F01A7F81F8C187A821A4266F8020064D"
Sha256 combinedHash; // combinedHash = b'C9392767AB23CEFF09D207B9223C0C26F01A7F81F8C187A821A4266F8020064D'
// preimage1: e.g., "bsv" -> b"627376"
// preimage2: e.g., "sCrypt" -> b"734372797074"
// preimage3: e.g., "IDE" -> b"494445"
// preimage1: e.g., "bsv" -> b'627376'
// preimage2: e.g., "sCrypt" -> b'734372797074'
// preimage3: e.g., "IDE" -> b'494445'
public function unlock(bytes preimage1, bytes preimage2, bytes preimage3) {
Sha256 hash1 = sha256(preimage1);
Sha256 hash2 = sha256(preimage2);
Expand Down
2 changes: 1 addition & 1 deletion docs/rabin.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ Rabin Signature
function fromLEUnsigned(bytes b) returns (int) {
// append positive sign byte. This does not hurt even when sign bit is already positive
return unpack(b ++ b"00");
return unpack(b ++ b'00');
}
}
4 changes: 2 additions & 2 deletions docs/rpuzzle.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ One crucial step in R-Puzzle is to extract ``r`` from `DER`_ encoded signature.
.. code-block:: solidity
contract RPuzzle {
Sig s; // s = b"3045022100948c67a95f856ae875a48a2d104df9d232189897a811178a715617d4b090a7e90220616f6ced5ab219fe1bfcf9802994b3ce72afbb2db0c4b653a74c9f03fb99323f01"
Sig s; // s = b'3045022100948c67a95f856ae875a48a2d104df9d232189897a811178a715617d4b090a7e90220616f6ced5ab219fe1bfcf9802994b3ce72afbb2db0c4b653a74c9f03fb99323f01'
function getSigR(Sig sig) returns (bytes) {
bytes lenBytes = sig[3:4];
Expand All @@ -19,7 +19,7 @@ One crucial step in R-Puzzle is to extract ``r`` from `DER`_ encoded signature.
return r;
}
// r = b"00948c67a95f856ae875a48a2d104df9d232189897a811178a715617d4b090a7e9"
// r = b'00948c67a95f856ae875a48a2d104df9d232189897a811178a715617d4b090a7e9'
public function unlock(bytes r) {
require(r == this.getSigR(this.s));
}
Expand Down
40 changes: 20 additions & 20 deletions docs/syntax.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,19 @@ Basic Types

.. code-block:: solidity
bytes b1 = b"ffee1234";
bytes b2 = b"414136d08c5ed2bf3ba048afe6dcaebafeffffffffffffffffffffffffffffff00";
bytes b1 = b'ffee1234';
bytes b2 = b'414136d08c5ed2bf3ba048afe6dcaebafeffffffffffffffffffffffffffffff00';
``bytes`` can be converted to ``int`` using function ``unpack``. Little-endian `sign-magnitude representation <https://www.tutorialspoint.com/sign-magnitude-notation>`_ is used,
where the most significant bit indicates the sign (``0`` for positive, ``1`` for negative). ``int`` can be converted to ``bytes`` with ``pack``.

.. code-block:: solidity
int a1 = unpack(b"36"); // 54 decimal
int a2 = unpack(b"b6"); // -54
int a3 = unpack(b"e803"); // 1000
int a4 = unpack(b"e883"); // -1000
bytes b = pack(a4); // b"e883"
int a1 = unpack(b'36'); // 54 decimal
int a2 = unpack(b'b6'); // -54
int a3 = unpack(b'e803'); // 1000
int a4 = unpack(b'e883'); // -1000
bytes b = pack(a4); // b'e883'
Subtypes of ``bytes``
Expand All @@ -84,56 +84,56 @@ To cast a supertype ``bytes`` to them, a function of the type name must be expli

.. code-block:: solidity
PubKey pubKey = PubKey(b"0200112233445566778899aabbccddeeffffeeddccbbaa99887766554433221100");
PubKey pubKey = PubKey(b'0200112233445566778899aabbccddeeffffeeddccbbaa99887766554433221100');
* **PrivKey** - a private key type.

.. code-block:: solidity
PrivKey privKey = PrivKey(b"00112233445566778899aabbccddeeffffeeddccbbaa99887766554433221100");
PrivKey privKey = PrivKey(b'00112233445566778899aabbccddeeffffeeddccbbaa99887766554433221100');
* **Sig** - a signature type in `DER <https://docs.moneybutton.com/docs/bsv-signature.html>`_ format, including `signature hash type <https://github.com/libbitcoin/libbitcoin-system/wiki/Sighash-and-TX-Signing>`_, which is ``SIGHASH_ALL | SIGHASH_FORKID`` (``0x41``) in the below example.

.. code-block:: solidity
Sig sig = Sig(b"3045022100b71be3f1dc001e0a1ad65ed84e7a5a0bfe48325f2146ca1d677cf15e96e8b80302206d74605e8234eae3d4980fcd7b2fdc1c5b9374f0ce71dea38707fccdbd28cf7e41");
Sig sig = Sig(b'3045022100b71be3f1dc001e0a1ad65ed84e7a5a0bfe48325f2146ca1d677cf15e96e8b80302206d74605e8234eae3d4980fcd7b2fdc1c5b9374f0ce71dea38707fccdbd28cf7e41');
* **Ripemd160** - a RIPEMD-160 hash type.

.. code-block:: solidity
Ripemd160 r = hash160(b"0011223344556677889999887766554433221100");
Ripemd160 r = hash160(b'0011223344556677889999887766554433221100');
* **Sha1** - a SHA-1 hash type.

.. code-block:: solidity
Sha1 s = sha1(b"0011223344556677889999887766554433221100");
Sha1 s = sha1(b'0011223344556677889999887766554433221100');
* **Sha256** - a SHA-256 hash type.

.. code-block:: solidity
Sha256 s = hash256(b"00112233445566778899aabbccddeeffffeeddccbbaa99887766554433221100");
Sha256 s = hash256(b'00112233445566778899aabbccddeeffffeeddccbbaa99887766554433221100');
``if`` statement
================
``if`` condition can be of type ``int`` and ``bytes``, besides ``bool``. They are implicitly converted to ``bool`` as in C and Javascript.
An ``int`` expression is evaluated to ``false`` if and only if it is ``0`` (including negative ``0``).
A ``bytes`` expression is evaluated to ``false`` if and only if every of its byte is ``b"00"`` (including empty ``bytes`` ``b""``).
A ``bytes`` expression is evaluated to ``false`` if and only if every of its byte is ``b'00'`` (including empty ``bytes`` ``b''``).

.. code-block:: solidity
int cond = 25; // true
int cond = 0; // false
int cond = unpack(b"80") // false since it is negative 0
int cond = unpack(b"000080") // false since it is negative 0
int cond = unpack(b'80') // false since it is negative 0
int cond = unpack(b'000080') // false since it is negative 0
if (cond) {} // equivalent to if (cond != 0) {}
bytes cond = b"00"; // false
bytes cond = b""; // false
bytes cond = b"80"; // true. Note b"80" is treated as false if converted to int
bytes cond = b"10" & b"73"; // true since it evaluates to b"10"
bytes cond = b'00'; // false
bytes cond = b''; // false
bytes cond = b'80'; // true. Note b'80' is treated as false if converted to int
bytes cond = b'10' & b'73'; // true since it evaluates to b'10'
if (cond) {}
Expand Down

0 comments on commit fb222bb

Please sign in to comment.