Skip to content

Commit

Permalink
feat: Modify the HTTP API amount parameter to the smallest unit of bi…
Browse files Browse the repository at this point in the history
…tcoin. Prevent loss of precision.
  • Loading branch information
cevin committed Jan 17, 2024
1 parent ba8008d commit 8cb1c5e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 28 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ POST /transaction/create
"pay_to_addresses": [
{
"address": "any bitcoin address",
"amount": float
"amount": int (satoshi)
}
]
}
Expand Down Expand Up @@ -269,7 +269,7 @@ POST /transaction/sign
"wif": "WIF private key",
"redeem-script": "multisig redeem script",
"segwit": bool,
"amount": float64 (optional, when segwit transaction required)
"amount": int (satoshi) (optional, when segwit transaction required)
}
]
}
Expand Down Expand Up @@ -297,13 +297,13 @@ Response
"wif": "WIF private key",
"redeem-script": "multisig redeem script",
"segwit": bool,
"amount": float64 (optional, when segwit transaction required)
"amount": int (satoshi) (optional, when segwit transaction required)
}
],
"pay_to_addresses": [
{
"address": "any Bitcoin address",
"amount": float
"amount": int (satoshi)
}
]
}
Expand Down
10 changes: 5 additions & 5 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ tx=0100000001fe75a438b72fdc302b80cc216d66d5e3bbb0359bce3bb4cecf743f5fda1f4eb1010
>
> 如:地址A有1BTC,想发送0.1给地址B,想要支付0.0000001BTC作为交易手续费,则pay_to_addresses可能为:
>
> [{"address":地址B, "amount":0.1}, {"address":地址A, "amount":0.8999999]
> [{"address":地址B, "amount":10000000}, {"address":地址A, "amount":99999990]
```text
POST /transaction/create
Expand All @@ -242,7 +242,7 @@ POST /transaction/create
"pay_to_addresses": [
{
"address": "任意正确的比特币地址",
"amount": float,最大支持精确到小数点后8位
"amount": int satoshi,比特币最小单位
}
]
}
Expand Down Expand Up @@ -275,7 +275,7 @@ POST /transaction/sign
"wif": "WIF格式的私钥",
"redeem-script": "多重签名地址的兑付脚本(redeem script)16进制编码",
"segwit": bool 是否来自bech32格式的地址的交易
"amount": float64 (可选 segwit 时必须)
"amount": int satoshi,比特币最小单位 (可选 segwit 时必须)
}
]
}
Expand Down Expand Up @@ -303,13 +303,13 @@ POST /transaction/sign
"wif": "WIF格式的私钥",
"redeem-script": "多重签名地址的兑付脚本(redeem script)16进制编码",
"segwit": bool 是否来自bech32格式的地址的交易,
"amount": float64 (可选 segwit 时必须)
"amount": int satoshi,比特币最小单位 (可选 segwit 时必须)
}
],
"pay_to_addresses": [
{
"address": "任意正确的比特币地址",
"amount": float,最大支持精确到小数点后8位
"amount": int satoshi,比特币最小单位,最大支持精确到小数点后8位
}
]
}
Expand Down
11 changes: 2 additions & 9 deletions utils/btc/btc.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,7 @@ func CreateRawTransaction(inputs Inputs, outputs Outputs) (*wire.MsgTx, error) {
}

for _, output := range outputs {

sendAmount, err := btcutil.NewAmount(output.Amount)
if err != nil {
return nil, err
}
sendAmount := output.Amount
address, err := btcutil.DecodeAddress(output.PayToAddress, mnet)
if err != nil {
return nil, err
Expand Down Expand Up @@ -339,10 +335,7 @@ func SignRawTxTransaction(tx *wire.MsgTx, inputs Inputs) (string, error) {
return "", fmt.Errorf("calc pkScript failed for %s", txId)
}

amount, err := btcutil.NewAmount(signUseInput.input.Amount)
if err != nil {
return "", fmt.Errorf(errTmpl, idx, "invalid amount, "+err.Error())
}
amount := signUseInput.input.Amount

signUseInputsMap[txId] = signUseInput
prevOutFetcher.AddPrevOut(
Expand Down
4 changes: 2 additions & 2 deletions utils/btc/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ type Input struct {
WIF string
RedeemScript string
SegWit bool
Amount float64
Amount int64
}

type Output struct {
PayToAddress string
Amount float64
Amount int64
}

type Inputs []Input
Expand Down
16 changes: 8 additions & 8 deletions web/req.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ type createAndSignTransactionReq struct {
PayToAddresses []payToAddress `json:"pay_to_addresses"`
}
type txin struct {
TxId string `json:"txid"`
VOut *int `json:"vout,omitempty,-"`
WIF string `json:"wif,omitempty,-"`
RedeemScript string `json:"redeem-script,omitempty,-"`
SegWit bool `json:"segwit,omitempty,-"`
Amount float64 `json:"amount,omitempty,-"`
TxId string `json:"txid"`
VOut *int `json:"vout,omitempty,-"`
WIF string `json:"wif,omitempty,-"`
RedeemScript string `json:"redeem-script,omitempty,-"`
SegWit bool `json:"segwit,omitempty,-"`
Amount int64 `json:"amount,omitempty,-"`
}
type payToAddress struct {
Address string `json:"address"`
Amount float64 `json:"amount"`
Address string `json:"address"`
Amount int64 `json:"amount"`
}

0 comments on commit 8cb1c5e

Please sign in to comment.